xref: /openbmc/linux/drivers/gpu/drm/tegra/sor.c (revision d2912cb15bdda8ba4a5dd73396ad62641af2f520)
1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
26b6b6042SThierry Reding /*
36b6b6042SThierry Reding  * Copyright (C) 2013 NVIDIA Corporation
46b6b6042SThierry Reding  */
56b6b6042SThierry Reding 
66b6b6042SThierry Reding #include <linux/clk.h>
7b299221cSThierry Reding #include <linux/clk-provider.h>
8a82752e1SThierry Reding #include <linux/debugfs.h>
96fad8f66SThierry Reding #include <linux/gpio.h>
106b6b6042SThierry Reding #include <linux/io.h>
11459cc2c6SThierry Reding #include <linux/of_device.h>
126b6b6042SThierry Reding #include <linux/platform_device.h>
13aaff8bd2SThierry Reding #include <linux/pm_runtime.h>
14459cc2c6SThierry Reding #include <linux/regulator/consumer.h>
156b6b6042SThierry Reding #include <linux/reset.h>
16306a7f91SThierry Reding 
177232398aSThierry Reding #include <soc/tegra/pmc.h>
186b6b6042SThierry Reding 
194aa3df71SThierry Reding #include <drm/drm_atomic_helper.h>
206b6b6042SThierry Reding #include <drm/drm_dp_helper.h>
216fad8f66SThierry Reding #include <drm/drm_panel.h>
2236e90221SThierry Reding #include <drm/drm_scdc_helper.h>
236b6b6042SThierry Reding 
246b6b6042SThierry Reding #include "dc.h"
256b6b6042SThierry Reding #include "drm.h"
26fad7b806SThierry Reding #include "hda.h"
276b6b6042SThierry Reding #include "sor.h"
28932f6529SThierry Reding #include "trace.h"
296b6b6042SThierry Reding 
30459cc2c6SThierry Reding #define SOR_REKEY 0x38
31459cc2c6SThierry Reding 
32459cc2c6SThierry Reding struct tegra_sor_hdmi_settings {
33459cc2c6SThierry Reding 	unsigned long frequency;
34459cc2c6SThierry Reding 
35459cc2c6SThierry Reding 	u8 vcocap;
36c57997bcSThierry Reding 	u8 filter;
37459cc2c6SThierry Reding 	u8 ichpmp;
38459cc2c6SThierry Reding 	u8 loadadj;
39c57997bcSThierry Reding 	u8 tmds_termadj;
40c57997bcSThierry Reding 	u8 tx_pu_value;
41c57997bcSThierry Reding 	u8 bg_temp_coef;
42c57997bcSThierry Reding 	u8 bg_vref_level;
43c57997bcSThierry Reding 	u8 avdd10_level;
44c57997bcSThierry Reding 	u8 avdd14_level;
45c57997bcSThierry Reding 	u8 sparepll;
46459cc2c6SThierry Reding 
47459cc2c6SThierry Reding 	u8 drive_current[4];
48459cc2c6SThierry Reding 	u8 preemphasis[4];
49459cc2c6SThierry Reding };
50459cc2c6SThierry Reding 
51459cc2c6SThierry Reding #if 1
52459cc2c6SThierry Reding static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
53459cc2c6SThierry Reding 	{
54459cc2c6SThierry Reding 		.frequency = 54000000,
55459cc2c6SThierry Reding 		.vcocap = 0x0,
56c57997bcSThierry Reding 		.filter = 0x0,
57459cc2c6SThierry Reding 		.ichpmp = 0x1,
58459cc2c6SThierry Reding 		.loadadj = 0x3,
59c57997bcSThierry Reding 		.tmds_termadj = 0x9,
60c57997bcSThierry Reding 		.tx_pu_value = 0x10,
61c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
62c57997bcSThierry Reding 		.bg_vref_level = 0x8,
63c57997bcSThierry Reding 		.avdd10_level = 0x4,
64c57997bcSThierry Reding 		.avdd14_level = 0x4,
65c57997bcSThierry Reding 		.sparepll = 0x0,
66459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
67459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
68459cc2c6SThierry Reding 	}, {
69459cc2c6SThierry Reding 		.frequency = 75000000,
70459cc2c6SThierry Reding 		.vcocap = 0x3,
71c57997bcSThierry Reding 		.filter = 0x0,
72459cc2c6SThierry Reding 		.ichpmp = 0x1,
73459cc2c6SThierry Reding 		.loadadj = 0x3,
74c57997bcSThierry Reding 		.tmds_termadj = 0x9,
75c57997bcSThierry Reding 		.tx_pu_value = 0x40,
76c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
77c57997bcSThierry Reding 		.bg_vref_level = 0x8,
78c57997bcSThierry Reding 		.avdd10_level = 0x4,
79c57997bcSThierry Reding 		.avdd14_level = 0x4,
80c57997bcSThierry Reding 		.sparepll = 0x0,
81459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
82459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
83459cc2c6SThierry Reding 	}, {
84459cc2c6SThierry Reding 		.frequency = 150000000,
85459cc2c6SThierry Reding 		.vcocap = 0x3,
86c57997bcSThierry Reding 		.filter = 0x0,
87459cc2c6SThierry Reding 		.ichpmp = 0x1,
88459cc2c6SThierry Reding 		.loadadj = 0x3,
89c57997bcSThierry Reding 		.tmds_termadj = 0x9,
90c57997bcSThierry Reding 		.tx_pu_value = 0x66,
91c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
92c57997bcSThierry Reding 		.bg_vref_level = 0x8,
93c57997bcSThierry Reding 		.avdd10_level = 0x4,
94c57997bcSThierry Reding 		.avdd14_level = 0x4,
95c57997bcSThierry Reding 		.sparepll = 0x0,
96459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
97459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
98459cc2c6SThierry Reding 	}, {
99459cc2c6SThierry Reding 		.frequency = 300000000,
100459cc2c6SThierry Reding 		.vcocap = 0x3,
101c57997bcSThierry Reding 		.filter = 0x0,
102459cc2c6SThierry Reding 		.ichpmp = 0x1,
103459cc2c6SThierry Reding 		.loadadj = 0x3,
104c57997bcSThierry Reding 		.tmds_termadj = 0x9,
105c57997bcSThierry Reding 		.tx_pu_value = 0x66,
106c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
107c57997bcSThierry Reding 		.bg_vref_level = 0xa,
108c57997bcSThierry Reding 		.avdd10_level = 0x4,
109c57997bcSThierry Reding 		.avdd14_level = 0x4,
110c57997bcSThierry Reding 		.sparepll = 0x0,
111459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
112459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x17, 0x17, 0x17 },
113459cc2c6SThierry Reding 	}, {
114459cc2c6SThierry Reding 		.frequency = 600000000,
115459cc2c6SThierry Reding 		.vcocap = 0x3,
116c57997bcSThierry Reding 		.filter = 0x0,
117459cc2c6SThierry Reding 		.ichpmp = 0x1,
118459cc2c6SThierry Reding 		.loadadj = 0x3,
119c57997bcSThierry Reding 		.tmds_termadj = 0x9,
120c57997bcSThierry Reding 		.tx_pu_value = 0x66,
121c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
122c57997bcSThierry Reding 		.bg_vref_level = 0x8,
123c57997bcSThierry Reding 		.avdd10_level = 0x4,
124c57997bcSThierry Reding 		.avdd14_level = 0x4,
125c57997bcSThierry Reding 		.sparepll = 0x0,
126459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
127459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
128459cc2c6SThierry Reding 	},
129459cc2c6SThierry Reding };
130459cc2c6SThierry Reding #else
131459cc2c6SThierry Reding static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
132459cc2c6SThierry Reding 	{
133459cc2c6SThierry Reding 		.frequency = 75000000,
134459cc2c6SThierry Reding 		.vcocap = 0x3,
135c57997bcSThierry Reding 		.filter = 0x0,
136459cc2c6SThierry Reding 		.ichpmp = 0x1,
137459cc2c6SThierry Reding 		.loadadj = 0x3,
138c57997bcSThierry Reding 		.tmds_termadj = 0x9,
139c57997bcSThierry Reding 		.tx_pu_value = 0x40,
140c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
141c57997bcSThierry Reding 		.bg_vref_level = 0x8,
142c57997bcSThierry Reding 		.avdd10_level = 0x4,
143c57997bcSThierry Reding 		.avdd14_level = 0x4,
144c57997bcSThierry Reding 		.sparepll = 0x0,
145459cc2c6SThierry Reding 		.drive_current = { 0x29, 0x29, 0x29, 0x29 },
146459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
147459cc2c6SThierry Reding 	}, {
148459cc2c6SThierry Reding 		.frequency = 150000000,
149459cc2c6SThierry Reding 		.vcocap = 0x3,
150c57997bcSThierry Reding 		.filter = 0x0,
151459cc2c6SThierry Reding 		.ichpmp = 0x1,
152459cc2c6SThierry Reding 		.loadadj = 0x3,
153c57997bcSThierry Reding 		.tmds_termadj = 0x9,
154c57997bcSThierry Reding 		.tx_pu_value = 0x66,
155c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
156c57997bcSThierry Reding 		.bg_vref_level = 0x8,
157c57997bcSThierry Reding 		.avdd10_level = 0x4,
158c57997bcSThierry Reding 		.avdd14_level = 0x4,
159c57997bcSThierry Reding 		.sparepll = 0x0,
160459cc2c6SThierry Reding 		.drive_current = { 0x30, 0x37, 0x37, 0x37 },
161459cc2c6SThierry Reding 		.preemphasis = { 0x01, 0x02, 0x02, 0x02 },
162459cc2c6SThierry Reding 	}, {
163459cc2c6SThierry Reding 		.frequency = 300000000,
164459cc2c6SThierry Reding 		.vcocap = 0x3,
165c57997bcSThierry Reding 		.filter = 0x0,
166459cc2c6SThierry Reding 		.ichpmp = 0x6,
167459cc2c6SThierry Reding 		.loadadj = 0x3,
168c57997bcSThierry Reding 		.tmds_termadj = 0x9,
169c57997bcSThierry Reding 		.tx_pu_value = 0x66,
170c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
171c57997bcSThierry Reding 		.bg_vref_level = 0xf,
172c57997bcSThierry Reding 		.avdd10_level = 0x4,
173c57997bcSThierry Reding 		.avdd14_level = 0x4,
174c57997bcSThierry Reding 		.sparepll = 0x0,
175459cc2c6SThierry Reding 		.drive_current = { 0x30, 0x37, 0x37, 0x37 },
176459cc2c6SThierry Reding 		.preemphasis = { 0x10, 0x3e, 0x3e, 0x3e },
177459cc2c6SThierry Reding 	}, {
178459cc2c6SThierry Reding 		.frequency = 600000000,
179459cc2c6SThierry Reding 		.vcocap = 0x3,
180c57997bcSThierry Reding 		.filter = 0x0,
181459cc2c6SThierry Reding 		.ichpmp = 0xa,
182459cc2c6SThierry Reding 		.loadadj = 0x3,
183c57997bcSThierry Reding 		.tmds_termadj = 0xb,
184c57997bcSThierry Reding 		.tx_pu_value = 0x66,
185c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
186c57997bcSThierry Reding 		.bg_vref_level = 0xe,
187c57997bcSThierry Reding 		.avdd10_level = 0x4,
188c57997bcSThierry Reding 		.avdd14_level = 0x4,
189c57997bcSThierry Reding 		.sparepll = 0x0,
190459cc2c6SThierry Reding 		.drive_current = { 0x35, 0x3e, 0x3e, 0x3e },
191459cc2c6SThierry Reding 		.preemphasis = { 0x02, 0x3f, 0x3f, 0x3f },
192459cc2c6SThierry Reding 	},
193459cc2c6SThierry Reding };
194459cc2c6SThierry Reding #endif
195459cc2c6SThierry Reding 
196c57997bcSThierry Reding static const struct tegra_sor_hdmi_settings tegra186_sor_hdmi_defaults[] = {
197c57997bcSThierry Reding 	{
198c57997bcSThierry Reding 		.frequency = 54000000,
199c57997bcSThierry Reding 		.vcocap = 0,
200c57997bcSThierry Reding 		.filter = 5,
201c57997bcSThierry Reding 		.ichpmp = 5,
202c57997bcSThierry Reding 		.loadadj = 3,
203c57997bcSThierry Reding 		.tmds_termadj = 0xf,
204c57997bcSThierry Reding 		.tx_pu_value = 0,
205c57997bcSThierry Reding 		.bg_temp_coef = 3,
206c57997bcSThierry Reding 		.bg_vref_level = 8,
207c57997bcSThierry Reding 		.avdd10_level = 4,
208c57997bcSThierry Reding 		.avdd14_level = 4,
209c57997bcSThierry Reding 		.sparepll = 0x54,
210c57997bcSThierry Reding 		.drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
211c57997bcSThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
212c57997bcSThierry Reding 	}, {
213c57997bcSThierry Reding 		.frequency = 75000000,
214c57997bcSThierry Reding 		.vcocap = 1,
215c57997bcSThierry Reding 		.filter = 5,
216c57997bcSThierry Reding 		.ichpmp = 5,
217c57997bcSThierry Reding 		.loadadj = 3,
218c57997bcSThierry Reding 		.tmds_termadj = 0xf,
219c57997bcSThierry Reding 		.tx_pu_value = 0,
220c57997bcSThierry Reding 		.bg_temp_coef = 3,
221c57997bcSThierry Reding 		.bg_vref_level = 8,
222c57997bcSThierry Reding 		.avdd10_level = 4,
223c57997bcSThierry Reding 		.avdd14_level = 4,
224c57997bcSThierry Reding 		.sparepll = 0x44,
225c57997bcSThierry Reding 		.drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
226c57997bcSThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
227c57997bcSThierry Reding 	}, {
228c57997bcSThierry Reding 		.frequency = 150000000,
229c57997bcSThierry Reding 		.vcocap = 3,
230c57997bcSThierry Reding 		.filter = 5,
231c57997bcSThierry Reding 		.ichpmp = 5,
232c57997bcSThierry Reding 		.loadadj = 3,
233c57997bcSThierry Reding 		.tmds_termadj = 15,
234c57997bcSThierry Reding 		.tx_pu_value = 0x66 /* 0 */,
235c57997bcSThierry Reding 		.bg_temp_coef = 3,
236c57997bcSThierry Reding 		.bg_vref_level = 8,
237c57997bcSThierry Reding 		.avdd10_level = 4,
238c57997bcSThierry Reding 		.avdd14_level = 4,
239c57997bcSThierry Reding 		.sparepll = 0x00, /* 0x34 */
240c57997bcSThierry Reding 		.drive_current = { 0x3a, 0x3a, 0x3a, 0x37 },
241c57997bcSThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
242c57997bcSThierry Reding 	}, {
243c57997bcSThierry Reding 		.frequency = 300000000,
244c57997bcSThierry Reding 		.vcocap = 3,
245c57997bcSThierry Reding 		.filter = 5,
246c57997bcSThierry Reding 		.ichpmp = 5,
247c57997bcSThierry Reding 		.loadadj = 3,
248c57997bcSThierry Reding 		.tmds_termadj = 15,
249c57997bcSThierry Reding 		.tx_pu_value = 64,
250c57997bcSThierry Reding 		.bg_temp_coef = 3,
251c57997bcSThierry Reding 		.bg_vref_level = 8,
252c57997bcSThierry Reding 		.avdd10_level = 4,
253c57997bcSThierry Reding 		.avdd14_level = 4,
254c57997bcSThierry Reding 		.sparepll = 0x34,
255c57997bcSThierry Reding 		.drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
256c57997bcSThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
257c57997bcSThierry Reding 	}, {
258c57997bcSThierry Reding 		.frequency = 600000000,
259c57997bcSThierry Reding 		.vcocap = 3,
260c57997bcSThierry Reding 		.filter = 5,
261c57997bcSThierry Reding 		.ichpmp = 5,
262c57997bcSThierry Reding 		.loadadj = 3,
263c57997bcSThierry Reding 		.tmds_termadj = 12,
264c57997bcSThierry Reding 		.tx_pu_value = 96,
265c57997bcSThierry Reding 		.bg_temp_coef = 3,
266c57997bcSThierry Reding 		.bg_vref_level = 8,
267c57997bcSThierry Reding 		.avdd10_level = 4,
268c57997bcSThierry Reding 		.avdd14_level = 4,
269c57997bcSThierry Reding 		.sparepll = 0x34,
270c57997bcSThierry Reding 		.drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
271c57997bcSThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
272c57997bcSThierry Reding 	}
273c57997bcSThierry Reding };
274c57997bcSThierry Reding 
2759b6c14b8SThierry Reding static const struct tegra_sor_hdmi_settings tegra194_sor_hdmi_defaults[] = {
2769b6c14b8SThierry Reding 	{
2779b6c14b8SThierry Reding 		.frequency = 54000000,
2789b6c14b8SThierry Reding 		.vcocap = 0,
2799b6c14b8SThierry Reding 		.filter = 5,
2809b6c14b8SThierry Reding 		.ichpmp = 5,
2819b6c14b8SThierry Reding 		.loadadj = 3,
2829b6c14b8SThierry Reding 		.tmds_termadj = 0xf,
2839b6c14b8SThierry Reding 		.tx_pu_value = 0,
2849b6c14b8SThierry Reding 		.bg_temp_coef = 3,
2859b6c14b8SThierry Reding 		.bg_vref_level = 8,
2869b6c14b8SThierry Reding 		.avdd10_level = 4,
2879b6c14b8SThierry Reding 		.avdd14_level = 4,
2889b6c14b8SThierry Reding 		.sparepll = 0x54,
2899b6c14b8SThierry Reding 		.drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
2909b6c14b8SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
2919b6c14b8SThierry Reding 	}, {
2929b6c14b8SThierry Reding 		.frequency = 75000000,
2939b6c14b8SThierry Reding 		.vcocap = 1,
2949b6c14b8SThierry Reding 		.filter = 5,
2959b6c14b8SThierry Reding 		.ichpmp = 5,
2969b6c14b8SThierry Reding 		.loadadj = 3,
2979b6c14b8SThierry Reding 		.tmds_termadj = 0xf,
2989b6c14b8SThierry Reding 		.tx_pu_value = 0,
2999b6c14b8SThierry Reding 		.bg_temp_coef = 3,
3009b6c14b8SThierry Reding 		.bg_vref_level = 8,
3019b6c14b8SThierry Reding 		.avdd10_level = 4,
3029b6c14b8SThierry Reding 		.avdd14_level = 4,
3039b6c14b8SThierry Reding 		.sparepll = 0x44,
3049b6c14b8SThierry Reding 		.drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
3059b6c14b8SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
3069b6c14b8SThierry Reding 	}, {
3079b6c14b8SThierry Reding 		.frequency = 150000000,
3089b6c14b8SThierry Reding 		.vcocap = 3,
3099b6c14b8SThierry Reding 		.filter = 5,
3109b6c14b8SThierry Reding 		.ichpmp = 5,
3119b6c14b8SThierry Reding 		.loadadj = 3,
3129b6c14b8SThierry Reding 		.tmds_termadj = 15,
3139b6c14b8SThierry Reding 		.tx_pu_value = 0x66 /* 0 */,
3149b6c14b8SThierry Reding 		.bg_temp_coef = 3,
3159b6c14b8SThierry Reding 		.bg_vref_level = 8,
3169b6c14b8SThierry Reding 		.avdd10_level = 4,
3179b6c14b8SThierry Reding 		.avdd14_level = 4,
3189b6c14b8SThierry Reding 		.sparepll = 0x00, /* 0x34 */
3199b6c14b8SThierry Reding 		.drive_current = { 0x3a, 0x3a, 0x3a, 0x37 },
3209b6c14b8SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
3219b6c14b8SThierry Reding 	}, {
3229b6c14b8SThierry Reding 		.frequency = 300000000,
3239b6c14b8SThierry Reding 		.vcocap = 3,
3249b6c14b8SThierry Reding 		.filter = 5,
3259b6c14b8SThierry Reding 		.ichpmp = 5,
3269b6c14b8SThierry Reding 		.loadadj = 3,
3279b6c14b8SThierry Reding 		.tmds_termadj = 15,
3289b6c14b8SThierry Reding 		.tx_pu_value = 64,
3299b6c14b8SThierry Reding 		.bg_temp_coef = 3,
3309b6c14b8SThierry Reding 		.bg_vref_level = 8,
3319b6c14b8SThierry Reding 		.avdd10_level = 4,
3329b6c14b8SThierry Reding 		.avdd14_level = 4,
3339b6c14b8SThierry Reding 		.sparepll = 0x34,
3349b6c14b8SThierry Reding 		.drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
3359b6c14b8SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
3369b6c14b8SThierry Reding 	}, {
3379b6c14b8SThierry Reding 		.frequency = 600000000,
3389b6c14b8SThierry Reding 		.vcocap = 3,
3399b6c14b8SThierry Reding 		.filter = 5,
3409b6c14b8SThierry Reding 		.ichpmp = 5,
3419b6c14b8SThierry Reding 		.loadadj = 3,
3429b6c14b8SThierry Reding 		.tmds_termadj = 12,
3439b6c14b8SThierry Reding 		.tx_pu_value = 96,
3449b6c14b8SThierry Reding 		.bg_temp_coef = 3,
3459b6c14b8SThierry Reding 		.bg_vref_level = 8,
3469b6c14b8SThierry Reding 		.avdd10_level = 4,
3479b6c14b8SThierry Reding 		.avdd14_level = 4,
3489b6c14b8SThierry Reding 		.sparepll = 0x34,
3499b6c14b8SThierry Reding 		.drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
3509b6c14b8SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
3519b6c14b8SThierry Reding 	}
3529b6c14b8SThierry Reding };
3539b6c14b8SThierry Reding 
354880cee0bSThierry Reding struct tegra_sor_regs {
355880cee0bSThierry Reding 	unsigned int head_state0;
356880cee0bSThierry Reding 	unsigned int head_state1;
357880cee0bSThierry Reding 	unsigned int head_state2;
358880cee0bSThierry Reding 	unsigned int head_state3;
359880cee0bSThierry Reding 	unsigned int head_state4;
360880cee0bSThierry Reding 	unsigned int head_state5;
361880cee0bSThierry Reding 	unsigned int pll0;
362880cee0bSThierry Reding 	unsigned int pll1;
363880cee0bSThierry Reding 	unsigned int pll2;
364880cee0bSThierry Reding 	unsigned int pll3;
365880cee0bSThierry Reding 	unsigned int dp_padctl0;
366880cee0bSThierry Reding 	unsigned int dp_padctl2;
367880cee0bSThierry Reding };
368880cee0bSThierry Reding 
369459cc2c6SThierry Reding struct tegra_sor_soc {
370459cc2c6SThierry Reding 	bool supports_edp;
371459cc2c6SThierry Reding 	bool supports_lvds;
372459cc2c6SThierry Reding 	bool supports_hdmi;
373459cc2c6SThierry Reding 	bool supports_dp;
374459cc2c6SThierry Reding 
375880cee0bSThierry Reding 	const struct tegra_sor_regs *regs;
376c57997bcSThierry Reding 	bool has_nvdisplay;
377880cee0bSThierry Reding 
378459cc2c6SThierry Reding 	const struct tegra_sor_hdmi_settings *settings;
379459cc2c6SThierry Reding 	unsigned int num_settings;
38030b49435SThierry Reding 
38130b49435SThierry Reding 	const u8 *xbar_cfg;
382459cc2c6SThierry Reding };
383459cc2c6SThierry Reding 
384459cc2c6SThierry Reding struct tegra_sor;
385459cc2c6SThierry Reding 
386459cc2c6SThierry Reding struct tegra_sor_ops {
387459cc2c6SThierry Reding 	const char *name;
388459cc2c6SThierry Reding 	int (*probe)(struct tegra_sor *sor);
389459cc2c6SThierry Reding 	int (*remove)(struct tegra_sor *sor);
390459cc2c6SThierry Reding };
391459cc2c6SThierry Reding 
3926b6b6042SThierry Reding struct tegra_sor {
3936b6b6042SThierry Reding 	struct host1x_client client;
3946b6b6042SThierry Reding 	struct tegra_output output;
3956b6b6042SThierry Reding 	struct device *dev;
3966b6b6042SThierry Reding 
397459cc2c6SThierry Reding 	const struct tegra_sor_soc *soc;
3986b6b6042SThierry Reding 	void __iomem *regs;
399c57997bcSThierry Reding 	unsigned int index;
4008e2988a7SThierry Reding 	unsigned int irq;
4016b6b6042SThierry Reding 
4026b6b6042SThierry Reding 	struct reset_control *rst;
4036b6b6042SThierry Reding 	struct clk *clk_parent;
4046b6b6042SThierry Reding 	struct clk *clk_safe;
405e1335e2fSThierry Reding 	struct clk *clk_out;
406e1335e2fSThierry Reding 	struct clk *clk_pad;
4076b6b6042SThierry Reding 	struct clk *clk_dp;
4086b6b6042SThierry Reding 	struct clk *clk;
4096b6b6042SThierry Reding 
4106d6c815dSThierry Reding 	u8 xbar_cfg[5];
4116d6c815dSThierry Reding 
4129542c237SThierry Reding 	struct drm_dp_aux *aux;
4136b6b6042SThierry Reding 
414dab16336SThierry Reding 	struct drm_info_list *debugfs_files;
415459cc2c6SThierry Reding 
416459cc2c6SThierry Reding 	const struct tegra_sor_ops *ops;
417c57997bcSThierry Reding 	enum tegra_io_pad pad;
418459cc2c6SThierry Reding 
419459cc2c6SThierry Reding 	/* for HDMI 2.0 */
420459cc2c6SThierry Reding 	struct tegra_sor_hdmi_settings *settings;
421459cc2c6SThierry Reding 	unsigned int num_settings;
422459cc2c6SThierry Reding 
423459cc2c6SThierry Reding 	struct regulator *avdd_io_supply;
424459cc2c6SThierry Reding 	struct regulator *vdd_pll_supply;
425459cc2c6SThierry Reding 	struct regulator *hdmi_supply;
42636e90221SThierry Reding 
42736e90221SThierry Reding 	struct delayed_work scdc;
42836e90221SThierry Reding 	bool scdc_enabled;
4298e2988a7SThierry Reding 
430fad7b806SThierry Reding 	struct tegra_hda_format format;
4316b6b6042SThierry Reding };
4326b6b6042SThierry Reding 
433c31efa7aSThierry Reding struct tegra_sor_state {
434c31efa7aSThierry Reding 	struct drm_connector_state base;
435c31efa7aSThierry Reding 
43636e90221SThierry Reding 	unsigned int link_speed;
43736e90221SThierry Reding 	unsigned long pclk;
438c31efa7aSThierry Reding 	unsigned int bpc;
439c31efa7aSThierry Reding };
440c31efa7aSThierry Reding 
441c31efa7aSThierry Reding static inline struct tegra_sor_state *
442c31efa7aSThierry Reding to_sor_state(struct drm_connector_state *state)
443c31efa7aSThierry Reding {
444c31efa7aSThierry Reding 	return container_of(state, struct tegra_sor_state, base);
445c31efa7aSThierry Reding }
446c31efa7aSThierry Reding 
44734fa183bSThierry Reding struct tegra_sor_config {
44834fa183bSThierry Reding 	u32 bits_per_pixel;
44934fa183bSThierry Reding 
45034fa183bSThierry Reding 	u32 active_polarity;
45134fa183bSThierry Reding 	u32 active_count;
45234fa183bSThierry Reding 	u32 tu_size;
45334fa183bSThierry Reding 	u32 active_frac;
45434fa183bSThierry Reding 	u32 watermark;
4557890b576SThierry Reding 
4567890b576SThierry Reding 	u32 hblank_symbols;
4577890b576SThierry Reding 	u32 vblank_symbols;
45834fa183bSThierry Reding };
45934fa183bSThierry Reding 
4606b6b6042SThierry Reding static inline struct tegra_sor *
4616b6b6042SThierry Reding host1x_client_to_sor(struct host1x_client *client)
4626b6b6042SThierry Reding {
4636b6b6042SThierry Reding 	return container_of(client, struct tegra_sor, client);
4646b6b6042SThierry Reding }
4656b6b6042SThierry Reding 
4666b6b6042SThierry Reding static inline struct tegra_sor *to_sor(struct tegra_output *output)
4676b6b6042SThierry Reding {
4686b6b6042SThierry Reding 	return container_of(output, struct tegra_sor, output);
4696b6b6042SThierry Reding }
4706b6b6042SThierry Reding 
4715c5f1301SThierry Reding static inline u32 tegra_sor_readl(struct tegra_sor *sor, unsigned int offset)
4726b6b6042SThierry Reding {
473932f6529SThierry Reding 	u32 value = readl(sor->regs + (offset << 2));
474932f6529SThierry Reding 
475932f6529SThierry Reding 	trace_sor_readl(sor->dev, offset, value);
476932f6529SThierry Reding 
477932f6529SThierry Reding 	return value;
4786b6b6042SThierry Reding }
4796b6b6042SThierry Reding 
48028fe2076SThierry Reding static inline void tegra_sor_writel(struct tegra_sor *sor, u32 value,
4815c5f1301SThierry Reding 				    unsigned int offset)
4826b6b6042SThierry Reding {
483932f6529SThierry Reding 	trace_sor_writel(sor->dev, offset, value);
4846b6b6042SThierry Reding 	writel(value, sor->regs + (offset << 2));
4856b6b6042SThierry Reding }
4866b6b6042SThierry Reding 
48725bb2cecSThierry Reding static int tegra_sor_set_parent_clock(struct tegra_sor *sor, struct clk *parent)
48825bb2cecSThierry Reding {
48925bb2cecSThierry Reding 	int err;
49025bb2cecSThierry Reding 
49125bb2cecSThierry Reding 	clk_disable_unprepare(sor->clk);
49225bb2cecSThierry Reding 
493e1335e2fSThierry Reding 	err = clk_set_parent(sor->clk_out, parent);
49425bb2cecSThierry Reding 	if (err < 0)
49525bb2cecSThierry Reding 		return err;
49625bb2cecSThierry Reding 
49725bb2cecSThierry Reding 	err = clk_prepare_enable(sor->clk);
49825bb2cecSThierry Reding 	if (err < 0)
49925bb2cecSThierry Reding 		return err;
50025bb2cecSThierry Reding 
50125bb2cecSThierry Reding 	return 0;
50225bb2cecSThierry Reding }
50325bb2cecSThierry Reding 
504e1335e2fSThierry Reding struct tegra_clk_sor_pad {
505b299221cSThierry Reding 	struct clk_hw hw;
506b299221cSThierry Reding 	struct tegra_sor *sor;
507b299221cSThierry Reding };
508b299221cSThierry Reding 
509e1335e2fSThierry Reding static inline struct tegra_clk_sor_pad *to_pad(struct clk_hw *hw)
510b299221cSThierry Reding {
511e1335e2fSThierry Reding 	return container_of(hw, struct tegra_clk_sor_pad, hw);
512b299221cSThierry Reding }
513b299221cSThierry Reding 
514e1335e2fSThierry Reding static const char * const tegra_clk_sor_pad_parents[] = {
515b299221cSThierry Reding 	"pll_d2_out0", "pll_dp"
516b299221cSThierry Reding };
517b299221cSThierry Reding 
518e1335e2fSThierry Reding static int tegra_clk_sor_pad_set_parent(struct clk_hw *hw, u8 index)
519b299221cSThierry Reding {
520e1335e2fSThierry Reding 	struct tegra_clk_sor_pad *pad = to_pad(hw);
521e1335e2fSThierry Reding 	struct tegra_sor *sor = pad->sor;
522b299221cSThierry Reding 	u32 value;
523b299221cSThierry Reding 
524b299221cSThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
525b299221cSThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
526b299221cSThierry Reding 
527b299221cSThierry Reding 	switch (index) {
528b299221cSThierry Reding 	case 0:
529b299221cSThierry Reding 		value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
530b299221cSThierry Reding 		break;
531b299221cSThierry Reding 
532b299221cSThierry Reding 	case 1:
533b299221cSThierry Reding 		value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
534b299221cSThierry Reding 		break;
535b299221cSThierry Reding 	}
536b299221cSThierry Reding 
537b299221cSThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
538b299221cSThierry Reding 
539b299221cSThierry Reding 	return 0;
540b299221cSThierry Reding }
541b299221cSThierry Reding 
542e1335e2fSThierry Reding static u8 tegra_clk_sor_pad_get_parent(struct clk_hw *hw)
543b299221cSThierry Reding {
544e1335e2fSThierry Reding 	struct tegra_clk_sor_pad *pad = to_pad(hw);
545e1335e2fSThierry Reding 	struct tegra_sor *sor = pad->sor;
546b299221cSThierry Reding 	u8 parent = U8_MAX;
547b299221cSThierry Reding 	u32 value;
548b299221cSThierry Reding 
549b299221cSThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
550b299221cSThierry Reding 
551b299221cSThierry Reding 	switch (value & SOR_CLK_CNTRL_DP_CLK_SEL_MASK) {
552b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK:
553b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_PCLK:
554b299221cSThierry Reding 		parent = 0;
555b299221cSThierry Reding 		break;
556b299221cSThierry Reding 
557b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK:
558b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_DPCLK:
559b299221cSThierry Reding 		parent = 1;
560b299221cSThierry Reding 		break;
561b299221cSThierry Reding 	}
562b299221cSThierry Reding 
563b299221cSThierry Reding 	return parent;
564b299221cSThierry Reding }
565b299221cSThierry Reding 
566e1335e2fSThierry Reding static const struct clk_ops tegra_clk_sor_pad_ops = {
567e1335e2fSThierry Reding 	.set_parent = tegra_clk_sor_pad_set_parent,
568e1335e2fSThierry Reding 	.get_parent = tegra_clk_sor_pad_get_parent,
569b299221cSThierry Reding };
570b299221cSThierry Reding 
571e1335e2fSThierry Reding static struct clk *tegra_clk_sor_pad_register(struct tegra_sor *sor,
572b299221cSThierry Reding 					      const char *name)
573b299221cSThierry Reding {
574e1335e2fSThierry Reding 	struct tegra_clk_sor_pad *pad;
575b299221cSThierry Reding 	struct clk_init_data init;
576b299221cSThierry Reding 	struct clk *clk;
577b299221cSThierry Reding 
578e1335e2fSThierry Reding 	pad = devm_kzalloc(sor->dev, sizeof(*pad), GFP_KERNEL);
579e1335e2fSThierry Reding 	if (!pad)
580b299221cSThierry Reding 		return ERR_PTR(-ENOMEM);
581b299221cSThierry Reding 
582e1335e2fSThierry Reding 	pad->sor = sor;
583b299221cSThierry Reding 
584b299221cSThierry Reding 	init.name = name;
585b299221cSThierry Reding 	init.flags = 0;
586e1335e2fSThierry Reding 	init.parent_names = tegra_clk_sor_pad_parents;
587e1335e2fSThierry Reding 	init.num_parents = ARRAY_SIZE(tegra_clk_sor_pad_parents);
588e1335e2fSThierry Reding 	init.ops = &tegra_clk_sor_pad_ops;
589b299221cSThierry Reding 
590e1335e2fSThierry Reding 	pad->hw.init = &init;
591b299221cSThierry Reding 
592e1335e2fSThierry Reding 	clk = devm_clk_register(sor->dev, &pad->hw);
593b299221cSThierry Reding 
594b299221cSThierry Reding 	return clk;
595b299221cSThierry Reding }
596b299221cSThierry Reding 
5976b6b6042SThierry Reding static int tegra_sor_dp_train_fast(struct tegra_sor *sor,
5986b6b6042SThierry Reding 				   struct drm_dp_link *link)
5996b6b6042SThierry Reding {
6006b6b6042SThierry Reding 	unsigned int i;
6016b6b6042SThierry Reding 	u8 pattern;
60228fe2076SThierry Reding 	u32 value;
6036b6b6042SThierry Reding 	int err;
6046b6b6042SThierry Reding 
6056b6b6042SThierry Reding 	/* setup lane parameters */
6066b6b6042SThierry Reding 	value = SOR_LANE_DRIVE_CURRENT_LANE3(0x40) |
6076b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE2(0x40) |
6086b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE1(0x40) |
6096b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE0(0x40);
610a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
6116b6b6042SThierry Reding 
6126b6b6042SThierry Reding 	value = SOR_LANE_PREEMPHASIS_LANE3(0x0f) |
6136b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE2(0x0f) |
6146b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE1(0x0f) |
6156b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE0(0x0f);
616a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
6176b6b6042SThierry Reding 
618a9a9e4fdSThierry Reding 	value = SOR_LANE_POSTCURSOR_LANE3(0x00) |
619a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE2(0x00) |
620a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE1(0x00) |
621a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE0(0x00);
622a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_POSTCURSOR0);
6236b6b6042SThierry Reding 
6246b6b6042SThierry Reding 	/* disable LVDS mode */
6256b6b6042SThierry Reding 	tegra_sor_writel(sor, 0, SOR_LVDS);
6266b6b6042SThierry Reding 
627880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
6286b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU_ENABLE;
6296b6b6042SThierry Reding 	value &= ~SOR_DP_PADCTL_TX_PU_MASK;
6306b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU(2); /* XXX: don't hardcode? */
631880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
6326b6b6042SThierry Reding 
633880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
6346b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
6356b6b6042SThierry Reding 		 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0;
636880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
6376b6b6042SThierry Reding 
6386b6b6042SThierry Reding 	usleep_range(10, 100);
6396b6b6042SThierry Reding 
640880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
6416b6b6042SThierry Reding 	value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
6426b6b6042SThierry Reding 		   SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
643880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
6446b6b6042SThierry Reding 
6459542c237SThierry Reding 	err = drm_dp_aux_prepare(sor->aux, DP_SET_ANSI_8B10B);
6466b6b6042SThierry Reding 	if (err < 0)
6476b6b6042SThierry Reding 		return err;
6486b6b6042SThierry Reding 
6496b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
6506b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
6516b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_NONE |
6526b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_TRAIN1;
6536b6b6042SThierry Reding 		value = (value << 8) | lane;
6546b6b6042SThierry Reding 	}
6556b6b6042SThierry Reding 
6566b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
6576b6b6042SThierry Reding 
6586b6b6042SThierry Reding 	pattern = DP_TRAINING_PATTERN_1;
6596b6b6042SThierry Reding 
6609542c237SThierry Reding 	err = drm_dp_aux_train(sor->aux, link, pattern);
6616b6b6042SThierry Reding 	if (err < 0)
6626b6b6042SThierry Reding 		return err;
6636b6b6042SThierry Reding 
664a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
6656b6b6042SThierry Reding 	value |= SOR_DP_SPARE_SEQ_ENABLE;
6666b6b6042SThierry Reding 	value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
6676b6b6042SThierry Reding 	value |= SOR_DP_SPARE_MACRO_SOR_CLK;
668a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
6696b6b6042SThierry Reding 
6706b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
6716b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
6726b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_NONE |
6736b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_TRAIN2;
6746b6b6042SThierry Reding 		value = (value << 8) | lane;
6756b6b6042SThierry Reding 	}
6766b6b6042SThierry Reding 
6776b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
6786b6b6042SThierry Reding 
6796b6b6042SThierry Reding 	pattern = DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_2;
6806b6b6042SThierry Reding 
6819542c237SThierry Reding 	err = drm_dp_aux_train(sor->aux, link, pattern);
6826b6b6042SThierry Reding 	if (err < 0)
6836b6b6042SThierry Reding 		return err;
6846b6b6042SThierry Reding 
6856b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
6866b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
6876b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
6886b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
6896b6b6042SThierry Reding 		value = (value << 8) | lane;
6906b6b6042SThierry Reding 	}
6916b6b6042SThierry Reding 
6926b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
6936b6b6042SThierry Reding 
6946b6b6042SThierry Reding 	pattern = DP_TRAINING_PATTERN_DISABLE;
6956b6b6042SThierry Reding 
6969542c237SThierry Reding 	err = drm_dp_aux_train(sor->aux, link, pattern);
6976b6b6042SThierry Reding 	if (err < 0)
6986b6b6042SThierry Reding 		return err;
6996b6b6042SThierry Reding 
7006b6b6042SThierry Reding 	return 0;
7016b6b6042SThierry Reding }
7026b6b6042SThierry Reding 
7036b6b6042SThierry Reding static void tegra_sor_super_update(struct tegra_sor *sor)
7046b6b6042SThierry Reding {
705a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
706a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 1, SOR_SUPER_STATE0);
707a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
7086b6b6042SThierry Reding }
7096b6b6042SThierry Reding 
7106b6b6042SThierry Reding static void tegra_sor_update(struct tegra_sor *sor)
7116b6b6042SThierry Reding {
712a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE0);
713a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 1, SOR_STATE0);
714a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE0);
7156b6b6042SThierry Reding }
7166b6b6042SThierry Reding 
7176b6b6042SThierry Reding static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
7186b6b6042SThierry Reding {
71928fe2076SThierry Reding 	u32 value;
7206b6b6042SThierry Reding 
7216b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWM_DIV);
7226b6b6042SThierry Reding 	value &= ~SOR_PWM_DIV_MASK;
7236b6b6042SThierry Reding 	value |= 0x400; /* period */
7246b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWM_DIV);
7256b6b6042SThierry Reding 
7266b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWM_CTL);
7276b6b6042SThierry Reding 	value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
7286b6b6042SThierry Reding 	value |= 0x400; /* duty cycle */
7296b6b6042SThierry Reding 	value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
7306b6b6042SThierry Reding 	value |= SOR_PWM_CTL_TRIGGER;
7316b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWM_CTL);
7326b6b6042SThierry Reding 
7336b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
7346b6b6042SThierry Reding 
7356b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
7366b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWM_CTL);
7376b6b6042SThierry Reding 		if ((value & SOR_PWM_CTL_TRIGGER) == 0)
7386b6b6042SThierry Reding 			return 0;
7396b6b6042SThierry Reding 
7406b6b6042SThierry Reding 		usleep_range(25, 100);
7416b6b6042SThierry Reding 	}
7426b6b6042SThierry Reding 
7436b6b6042SThierry Reding 	return -ETIMEDOUT;
7446b6b6042SThierry Reding }
7456b6b6042SThierry Reding 
7466b6b6042SThierry Reding static int tegra_sor_attach(struct tegra_sor *sor)
7476b6b6042SThierry Reding {
7486b6b6042SThierry Reding 	unsigned long value, timeout;
7496b6b6042SThierry Reding 
7506b6b6042SThierry Reding 	/* wake up in normal mode */
751a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
7526b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
7536b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_MODE_NORMAL;
754a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
7556b6b6042SThierry Reding 	tegra_sor_super_update(sor);
7566b6b6042SThierry Reding 
7576b6b6042SThierry Reding 	/* attach */
758a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
7596b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_ATTACHED;
760a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
7616b6b6042SThierry Reding 	tegra_sor_super_update(sor);
7626b6b6042SThierry Reding 
7636b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
7646b6b6042SThierry Reding 
7656b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
7666b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
7676b6b6042SThierry Reding 		if ((value & SOR_TEST_ATTACHED) != 0)
7686b6b6042SThierry Reding 			return 0;
7696b6b6042SThierry Reding 
7706b6b6042SThierry Reding 		usleep_range(25, 100);
7716b6b6042SThierry Reding 	}
7726b6b6042SThierry Reding 
7736b6b6042SThierry Reding 	return -ETIMEDOUT;
7746b6b6042SThierry Reding }
7756b6b6042SThierry Reding 
7766b6b6042SThierry Reding static int tegra_sor_wakeup(struct tegra_sor *sor)
7776b6b6042SThierry Reding {
7786b6b6042SThierry Reding 	unsigned long value, timeout;
7796b6b6042SThierry Reding 
7806b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
7816b6b6042SThierry Reding 
7826b6b6042SThierry Reding 	/* wait for head to wake up */
7836b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
7846b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
7856b6b6042SThierry Reding 		value &= SOR_TEST_HEAD_MODE_MASK;
7866b6b6042SThierry Reding 
7876b6b6042SThierry Reding 		if (value == SOR_TEST_HEAD_MODE_AWAKE)
7886b6b6042SThierry Reding 			return 0;
7896b6b6042SThierry Reding 
7906b6b6042SThierry Reding 		usleep_range(25, 100);
7916b6b6042SThierry Reding 	}
7926b6b6042SThierry Reding 
7936b6b6042SThierry Reding 	return -ETIMEDOUT;
7946b6b6042SThierry Reding }
7956b6b6042SThierry Reding 
7966b6b6042SThierry Reding static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
7976b6b6042SThierry Reding {
79828fe2076SThierry Reding 	u32 value;
7996b6b6042SThierry Reding 
8006b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWR);
8016b6b6042SThierry Reding 	value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
8026b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWR);
8036b6b6042SThierry Reding 
8046b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
8056b6b6042SThierry Reding 
8066b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
8076b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
8086b6b6042SThierry Reding 		if ((value & SOR_PWR_TRIGGER) == 0)
8096b6b6042SThierry Reding 			return 0;
8106b6b6042SThierry Reding 
8116b6b6042SThierry Reding 		usleep_range(25, 100);
8126b6b6042SThierry Reding 	}
8136b6b6042SThierry Reding 
8146b6b6042SThierry Reding 	return -ETIMEDOUT;
8156b6b6042SThierry Reding }
8166b6b6042SThierry Reding 
81734fa183bSThierry Reding struct tegra_sor_params {
81834fa183bSThierry Reding 	/* number of link clocks per line */
81934fa183bSThierry Reding 	unsigned int num_clocks;
82034fa183bSThierry Reding 	/* ratio between input and output */
82134fa183bSThierry Reding 	u64 ratio;
82234fa183bSThierry Reding 	/* precision factor */
82334fa183bSThierry Reding 	u64 precision;
82434fa183bSThierry Reding 
82534fa183bSThierry Reding 	unsigned int active_polarity;
82634fa183bSThierry Reding 	unsigned int active_count;
82734fa183bSThierry Reding 	unsigned int active_frac;
82834fa183bSThierry Reding 	unsigned int tu_size;
82934fa183bSThierry Reding 	unsigned int error;
83034fa183bSThierry Reding };
83134fa183bSThierry Reding 
83234fa183bSThierry Reding static int tegra_sor_compute_params(struct tegra_sor *sor,
83334fa183bSThierry Reding 				    struct tegra_sor_params *params,
83434fa183bSThierry Reding 				    unsigned int tu_size)
83534fa183bSThierry Reding {
83634fa183bSThierry Reding 	u64 active_sym, active_count, frac, approx;
83734fa183bSThierry Reding 	u32 active_polarity, active_frac = 0;
83834fa183bSThierry Reding 	const u64 f = params->precision;
83934fa183bSThierry Reding 	s64 error;
84034fa183bSThierry Reding 
84134fa183bSThierry Reding 	active_sym = params->ratio * tu_size;
84234fa183bSThierry Reding 	active_count = div_u64(active_sym, f) * f;
84334fa183bSThierry Reding 	frac = active_sym - active_count;
84434fa183bSThierry Reding 
84534fa183bSThierry Reding 	/* fraction < 0.5 */
84634fa183bSThierry Reding 	if (frac >= (f / 2)) {
84734fa183bSThierry Reding 		active_polarity = 1;
84834fa183bSThierry Reding 		frac = f - frac;
84934fa183bSThierry Reding 	} else {
85034fa183bSThierry Reding 		active_polarity = 0;
85134fa183bSThierry Reding 	}
85234fa183bSThierry Reding 
85334fa183bSThierry Reding 	if (frac != 0) {
85434fa183bSThierry Reding 		frac = div_u64(f * f,  frac); /* 1/fraction */
85534fa183bSThierry Reding 		if (frac <= (15 * f)) {
85634fa183bSThierry Reding 			active_frac = div_u64(frac, f);
85734fa183bSThierry Reding 
85834fa183bSThierry Reding 			/* round up */
85934fa183bSThierry Reding 			if (active_polarity)
86034fa183bSThierry Reding 				active_frac++;
86134fa183bSThierry Reding 		} else {
86234fa183bSThierry Reding 			active_frac = active_polarity ? 1 : 15;
86334fa183bSThierry Reding 		}
86434fa183bSThierry Reding 	}
86534fa183bSThierry Reding 
86634fa183bSThierry Reding 	if (active_frac == 1)
86734fa183bSThierry Reding 		active_polarity = 0;
86834fa183bSThierry Reding 
86934fa183bSThierry Reding 	if (active_polarity == 1) {
87034fa183bSThierry Reding 		if (active_frac) {
87134fa183bSThierry Reding 			approx = active_count + (active_frac * (f - 1)) * f;
87234fa183bSThierry Reding 			approx = div_u64(approx, active_frac * f);
87334fa183bSThierry Reding 		} else {
87434fa183bSThierry Reding 			approx = active_count + f;
87534fa183bSThierry Reding 		}
87634fa183bSThierry Reding 	} else {
87734fa183bSThierry Reding 		if (active_frac)
87834fa183bSThierry Reding 			approx = active_count + div_u64(f, active_frac);
87934fa183bSThierry Reding 		else
88034fa183bSThierry Reding 			approx = active_count;
88134fa183bSThierry Reding 	}
88234fa183bSThierry Reding 
88334fa183bSThierry Reding 	error = div_s64(active_sym - approx, tu_size);
88434fa183bSThierry Reding 	error *= params->num_clocks;
88534fa183bSThierry Reding 
88679211c8eSAndrew Morton 	if (error <= 0 && abs(error) < params->error) {
88734fa183bSThierry Reding 		params->active_count = div_u64(active_count, f);
88834fa183bSThierry Reding 		params->active_polarity = active_polarity;
88934fa183bSThierry Reding 		params->active_frac = active_frac;
89079211c8eSAndrew Morton 		params->error = abs(error);
89134fa183bSThierry Reding 		params->tu_size = tu_size;
89234fa183bSThierry Reding 
89334fa183bSThierry Reding 		if (error == 0)
89434fa183bSThierry Reding 			return true;
89534fa183bSThierry Reding 	}
89634fa183bSThierry Reding 
89734fa183bSThierry Reding 	return false;
89834fa183bSThierry Reding }
89934fa183bSThierry Reding 
900a198359eSThierry Reding static int tegra_sor_compute_config(struct tegra_sor *sor,
90180444495SThierry Reding 				    const struct drm_display_mode *mode,
90234fa183bSThierry Reding 				    struct tegra_sor_config *config,
90334fa183bSThierry Reding 				    struct drm_dp_link *link)
90434fa183bSThierry Reding {
90534fa183bSThierry Reding 	const u64 f = 100000, link_rate = link->rate * 1000;
90634fa183bSThierry Reding 	const u64 pclk = mode->clock * 1000;
9077890b576SThierry Reding 	u64 input, output, watermark, num;
90834fa183bSThierry Reding 	struct tegra_sor_params params;
90934fa183bSThierry Reding 	u32 num_syms_per_line;
91034fa183bSThierry Reding 	unsigned int i;
91134fa183bSThierry Reding 
91234fa183bSThierry Reding 	if (!link_rate || !link->num_lanes || !pclk || !config->bits_per_pixel)
91334fa183bSThierry Reding 		return -EINVAL;
91434fa183bSThierry Reding 
91534fa183bSThierry Reding 	output = link_rate * 8 * link->num_lanes;
91634fa183bSThierry Reding 	input = pclk * config->bits_per_pixel;
91734fa183bSThierry Reding 
91834fa183bSThierry Reding 	if (input >= output)
91934fa183bSThierry Reding 		return -ERANGE;
92034fa183bSThierry Reding 
92134fa183bSThierry Reding 	memset(&params, 0, sizeof(params));
92234fa183bSThierry Reding 	params.ratio = div64_u64(input * f, output);
92334fa183bSThierry Reding 	params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
92434fa183bSThierry Reding 	params.precision = f;
92534fa183bSThierry Reding 	params.error = 64 * f;
92634fa183bSThierry Reding 	params.tu_size = 64;
92734fa183bSThierry Reding 
92834fa183bSThierry Reding 	for (i = params.tu_size; i >= 32; i--)
92934fa183bSThierry Reding 		if (tegra_sor_compute_params(sor, &params, i))
93034fa183bSThierry Reding 			break;
93134fa183bSThierry Reding 
93234fa183bSThierry Reding 	if (params.active_frac == 0) {
93334fa183bSThierry Reding 		config->active_polarity = 0;
93434fa183bSThierry Reding 		config->active_count = params.active_count;
93534fa183bSThierry Reding 
93634fa183bSThierry Reding 		if (!params.active_polarity)
93734fa183bSThierry Reding 			config->active_count--;
93834fa183bSThierry Reding 
93934fa183bSThierry Reding 		config->tu_size = params.tu_size;
94034fa183bSThierry Reding 		config->active_frac = 1;
94134fa183bSThierry Reding 	} else {
94234fa183bSThierry Reding 		config->active_polarity = params.active_polarity;
94334fa183bSThierry Reding 		config->active_count = params.active_count;
94434fa183bSThierry Reding 		config->active_frac = params.active_frac;
94534fa183bSThierry Reding 		config->tu_size = params.tu_size;
94634fa183bSThierry Reding 	}
94734fa183bSThierry Reding 
94834fa183bSThierry Reding 	dev_dbg(sor->dev,
94934fa183bSThierry Reding 		"polarity: %d active count: %d tu size: %d active frac: %d\n",
95034fa183bSThierry Reding 		config->active_polarity, config->active_count,
95134fa183bSThierry Reding 		config->tu_size, config->active_frac);
95234fa183bSThierry Reding 
95334fa183bSThierry Reding 	watermark = params.ratio * config->tu_size * (f - params.ratio);
95434fa183bSThierry Reding 	watermark = div_u64(watermark, f);
95534fa183bSThierry Reding 
95634fa183bSThierry Reding 	watermark = div_u64(watermark + params.error, f);
95734fa183bSThierry Reding 	config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
95834fa183bSThierry Reding 	num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
95934fa183bSThierry Reding 			    (link->num_lanes * 8);
96034fa183bSThierry Reding 
96134fa183bSThierry Reding 	if (config->watermark > 30) {
96234fa183bSThierry Reding 		config->watermark = 30;
96334fa183bSThierry Reding 		dev_err(sor->dev,
96434fa183bSThierry Reding 			"unable to compute TU size, forcing watermark to %u\n",
96534fa183bSThierry Reding 			config->watermark);
96634fa183bSThierry Reding 	} else if (config->watermark > num_syms_per_line) {
96734fa183bSThierry Reding 		config->watermark = num_syms_per_line;
96834fa183bSThierry Reding 		dev_err(sor->dev, "watermark too high, forcing to %u\n",
96934fa183bSThierry Reding 			config->watermark);
97034fa183bSThierry Reding 	}
97134fa183bSThierry Reding 
9727890b576SThierry Reding 	/* compute the number of symbols per horizontal blanking interval */
9737890b576SThierry Reding 	num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
9747890b576SThierry Reding 	config->hblank_symbols = div_u64(num, pclk);
9757890b576SThierry Reding 
9767890b576SThierry Reding 	if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
9777890b576SThierry Reding 		config->hblank_symbols -= 3;
9787890b576SThierry Reding 
9797890b576SThierry Reding 	config->hblank_symbols -= 12 / link->num_lanes;
9807890b576SThierry Reding 
9817890b576SThierry Reding 	/* compute the number of symbols per vertical blanking interval */
9827890b576SThierry Reding 	num = (mode->hdisplay - 25) * link_rate;
9837890b576SThierry Reding 	config->vblank_symbols = div_u64(num, pclk);
9847890b576SThierry Reding 	config->vblank_symbols -= 36 / link->num_lanes + 4;
9857890b576SThierry Reding 
9867890b576SThierry Reding 	dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
9877890b576SThierry Reding 		config->vblank_symbols);
9887890b576SThierry Reding 
98934fa183bSThierry Reding 	return 0;
99034fa183bSThierry Reding }
99134fa183bSThierry Reding 
992402f6bcdSThierry Reding static void tegra_sor_apply_config(struct tegra_sor *sor,
993402f6bcdSThierry Reding 				   const struct tegra_sor_config *config)
994402f6bcdSThierry Reding {
995402f6bcdSThierry Reding 	u32 value;
996402f6bcdSThierry Reding 
997402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
998402f6bcdSThierry Reding 	value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
999402f6bcdSThierry Reding 	value |= SOR_DP_LINKCTL_TU_SIZE(config->tu_size);
1000402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
1001402f6bcdSThierry Reding 
1002402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_CONFIG0);
1003402f6bcdSThierry Reding 	value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
1004402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_WATERMARK(config->watermark);
1005402f6bcdSThierry Reding 
1006402f6bcdSThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
1007402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config->active_count);
1008402f6bcdSThierry Reding 
1009402f6bcdSThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
1010402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config->active_frac);
1011402f6bcdSThierry Reding 
1012402f6bcdSThierry Reding 	if (config->active_polarity)
1013402f6bcdSThierry Reding 		value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
1014402f6bcdSThierry Reding 	else
1015402f6bcdSThierry Reding 		value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
1016402f6bcdSThierry Reding 
1017402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
1018402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
1019402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_CONFIG0);
1020402f6bcdSThierry Reding 
1021402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
1022402f6bcdSThierry Reding 	value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
1023402f6bcdSThierry Reding 	value |= config->hblank_symbols & 0xffff;
1024402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
1025402f6bcdSThierry Reding 
1026402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
1027402f6bcdSThierry Reding 	value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
1028402f6bcdSThierry Reding 	value |= config->vblank_symbols & 0xffff;
1029402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
1030402f6bcdSThierry Reding }
1031402f6bcdSThierry Reding 
10322bd1dd39SThierry Reding static void tegra_sor_mode_set(struct tegra_sor *sor,
10332bd1dd39SThierry Reding 			       const struct drm_display_mode *mode,
1034c31efa7aSThierry Reding 			       struct tegra_sor_state *state)
10352bd1dd39SThierry Reding {
10362bd1dd39SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(sor->output.encoder.crtc);
10372bd1dd39SThierry Reding 	unsigned int vbe, vse, hbe, hse, vbs, hbs;
10382bd1dd39SThierry Reding 	u32 value;
10392bd1dd39SThierry Reding 
10402bd1dd39SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
10412bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_PIXELDEPTH_MASK;
10422bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
10432bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_OWNER_MASK;
10442bd1dd39SThierry Reding 
10452bd1dd39SThierry Reding 	value |= SOR_STATE_ASY_CRC_MODE_COMPLETE |
10462bd1dd39SThierry Reding 		 SOR_STATE_ASY_OWNER(dc->pipe + 1);
10472bd1dd39SThierry Reding 
10482bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PHSYNC)
10492bd1dd39SThierry Reding 		value &= ~SOR_STATE_ASY_HSYNCPOL;
10502bd1dd39SThierry Reding 
10512bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
10522bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_HSYNCPOL;
10532bd1dd39SThierry Reding 
10542bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PVSYNC)
10552bd1dd39SThierry Reding 		value &= ~SOR_STATE_ASY_VSYNCPOL;
10562bd1dd39SThierry Reding 
10572bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
10582bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_VSYNCPOL;
10592bd1dd39SThierry Reding 
1060c31efa7aSThierry Reding 	switch (state->bpc) {
1061c31efa7aSThierry Reding 	case 16:
1062c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_48_444;
1063c31efa7aSThierry Reding 		break;
1064c31efa7aSThierry Reding 
1065c31efa7aSThierry Reding 	case 12:
1066c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_36_444;
1067c31efa7aSThierry Reding 		break;
1068c31efa7aSThierry Reding 
1069c31efa7aSThierry Reding 	case 10:
1070c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_30_444;
1071c31efa7aSThierry Reding 		break;
1072c31efa7aSThierry Reding 
10732bd1dd39SThierry Reding 	case 8:
10742bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
10752bd1dd39SThierry Reding 		break;
10762bd1dd39SThierry Reding 
10772bd1dd39SThierry Reding 	case 6:
10782bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
10792bd1dd39SThierry Reding 		break;
10802bd1dd39SThierry Reding 
10812bd1dd39SThierry Reding 	default:
1082c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
10832bd1dd39SThierry Reding 		break;
10842bd1dd39SThierry Reding 	}
10852bd1dd39SThierry Reding 
10862bd1dd39SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
10872bd1dd39SThierry Reding 
10882bd1dd39SThierry Reding 	/*
10892bd1dd39SThierry Reding 	 * TODO: The video timing programming below doesn't seem to match the
10902bd1dd39SThierry Reding 	 * register definitions.
10912bd1dd39SThierry Reding 	 */
10922bd1dd39SThierry Reding 
10932bd1dd39SThierry Reding 	value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
1094880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state1 + dc->pipe);
10952bd1dd39SThierry Reding 
10962bd1dd39SThierry Reding 	/* sync end = sync width - 1 */
10972bd1dd39SThierry Reding 	vse = mode->vsync_end - mode->vsync_start - 1;
10982bd1dd39SThierry Reding 	hse = mode->hsync_end - mode->hsync_start - 1;
10992bd1dd39SThierry Reding 
11002bd1dd39SThierry Reding 	value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
1101880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state2 + dc->pipe);
11022bd1dd39SThierry Reding 
11032bd1dd39SThierry Reding 	/* blank end = sync end + back porch */
11042bd1dd39SThierry Reding 	vbe = vse + (mode->vtotal - mode->vsync_end);
11052bd1dd39SThierry Reding 	hbe = hse + (mode->htotal - mode->hsync_end);
11062bd1dd39SThierry Reding 
11072bd1dd39SThierry Reding 	value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
1108880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state3 + dc->pipe);
11092bd1dd39SThierry Reding 
11102bd1dd39SThierry Reding 	/* blank start = blank end + active */
11112bd1dd39SThierry Reding 	vbs = vbe + mode->vdisplay;
11122bd1dd39SThierry Reding 	hbs = hbe + mode->hdisplay;
11132bd1dd39SThierry Reding 
11142bd1dd39SThierry Reding 	value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
1115880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state4 + dc->pipe);
11162bd1dd39SThierry Reding 
11172bd1dd39SThierry Reding 	/* XXX interlacing support */
1118880cee0bSThierry Reding 	tegra_sor_writel(sor, 0x001, sor->soc->regs->head_state5 + dc->pipe);
11192bd1dd39SThierry Reding }
11202bd1dd39SThierry Reding 
11216fad8f66SThierry Reding static int tegra_sor_detach(struct tegra_sor *sor)
11226b6b6042SThierry Reding {
11236fad8f66SThierry Reding 	unsigned long value, timeout;
11246fad8f66SThierry Reding 
11256fad8f66SThierry Reding 	/* switch to safe mode */
1126a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
11276fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_MODE_NORMAL;
1128a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
11296fad8f66SThierry Reding 	tegra_sor_super_update(sor);
11306fad8f66SThierry Reding 
11316fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
11326fad8f66SThierry Reding 
11336fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
11346fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
11356fad8f66SThierry Reding 		if (value & SOR_PWR_MODE_SAFE)
11366fad8f66SThierry Reding 			break;
11376fad8f66SThierry Reding 	}
11386fad8f66SThierry Reding 
11396fad8f66SThierry Reding 	if ((value & SOR_PWR_MODE_SAFE) == 0)
11406fad8f66SThierry Reding 		return -ETIMEDOUT;
11416fad8f66SThierry Reding 
11426fad8f66SThierry Reding 	/* go to sleep */
1143a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
11446fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
1145a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
11466fad8f66SThierry Reding 	tegra_sor_super_update(sor);
11476fad8f66SThierry Reding 
11486fad8f66SThierry Reding 	/* detach */
1149a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
11506fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_ATTACHED;
1151a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
11526fad8f66SThierry Reding 	tegra_sor_super_update(sor);
11536fad8f66SThierry Reding 
11546fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
11556fad8f66SThierry Reding 
11566fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
11576fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
11586fad8f66SThierry Reding 		if ((value & SOR_TEST_ATTACHED) == 0)
11596fad8f66SThierry Reding 			break;
11606fad8f66SThierry Reding 
11616fad8f66SThierry Reding 		usleep_range(25, 100);
11626fad8f66SThierry Reding 	}
11636fad8f66SThierry Reding 
11646fad8f66SThierry Reding 	if ((value & SOR_TEST_ATTACHED) != 0)
11656fad8f66SThierry Reding 		return -ETIMEDOUT;
11666fad8f66SThierry Reding 
11676fad8f66SThierry Reding 	return 0;
11686fad8f66SThierry Reding }
11696fad8f66SThierry Reding 
11706fad8f66SThierry Reding static int tegra_sor_power_down(struct tegra_sor *sor)
11716fad8f66SThierry Reding {
11726fad8f66SThierry Reding 	unsigned long value, timeout;
11736fad8f66SThierry Reding 	int err;
11746fad8f66SThierry Reding 
11756fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWR);
11766fad8f66SThierry Reding 	value &= ~SOR_PWR_NORMAL_STATE_PU;
11776fad8f66SThierry Reding 	value |= SOR_PWR_TRIGGER;
11786fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWR);
11796fad8f66SThierry Reding 
11806fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
11816fad8f66SThierry Reding 
11826fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
11836fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
11846fad8f66SThierry Reding 		if ((value & SOR_PWR_TRIGGER) == 0)
11856fad8f66SThierry Reding 			return 0;
11866fad8f66SThierry Reding 
11876fad8f66SThierry Reding 		usleep_range(25, 100);
11886fad8f66SThierry Reding 	}
11896fad8f66SThierry Reding 
11906fad8f66SThierry Reding 	if ((value & SOR_PWR_TRIGGER) != 0)
11916fad8f66SThierry Reding 		return -ETIMEDOUT;
11926fad8f66SThierry Reding 
119325bb2cecSThierry Reding 	/* switch to safe parent clock */
119425bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
1195e1335e2fSThierry Reding 	if (err < 0) {
11966fad8f66SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
1197e1335e2fSThierry Reding 		return err;
1198e1335e2fSThierry Reding 	}
11996fad8f66SThierry Reding 
1200880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
12016fad8f66SThierry Reding 	value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
12026fad8f66SThierry Reding 		   SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
1203880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
12046fad8f66SThierry Reding 
12056fad8f66SThierry Reding 	/* stop lane sequencer */
12066fad8f66SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
12076fad8f66SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
12086fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
12096fad8f66SThierry Reding 
12106fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
12116fad8f66SThierry Reding 
12126fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
12136fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
12146fad8f66SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
12156fad8f66SThierry Reding 			break;
12166fad8f66SThierry Reding 
12176fad8f66SThierry Reding 		usleep_range(25, 100);
12186fad8f66SThierry Reding 	}
12196fad8f66SThierry Reding 
12206fad8f66SThierry Reding 	if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
12216fad8f66SThierry Reding 		return -ETIMEDOUT;
12226fad8f66SThierry Reding 
1223880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1224a9a9e4fdSThierry Reding 	value |= SOR_PLL2_PORT_POWERDOWN;
1225880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
12266fad8f66SThierry Reding 
12276fad8f66SThierry Reding 	usleep_range(20, 100);
12286fad8f66SThierry Reding 
1229880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
1230a9a9e4fdSThierry Reding 	value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
1231880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
12326fad8f66SThierry Reding 
1233880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1234a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD;
1235a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1236880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
12376fad8f66SThierry Reding 
12386fad8f66SThierry Reding 	usleep_range(20, 100);
12396fad8f66SThierry Reding 
12406fad8f66SThierry Reding 	return 0;
12416fad8f66SThierry Reding }
12426fad8f66SThierry Reding 
12436fad8f66SThierry Reding static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
12446fad8f66SThierry Reding {
12456fad8f66SThierry Reding 	u32 value;
12466fad8f66SThierry Reding 
12476fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
12486fad8f66SThierry Reding 
12496fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
1250a9a9e4fdSThierry Reding 		value = tegra_sor_readl(sor, SOR_CRCA);
1251a9a9e4fdSThierry Reding 		if (value & SOR_CRCA_VALID)
12526fad8f66SThierry Reding 			return 0;
12536fad8f66SThierry Reding 
12546fad8f66SThierry Reding 		usleep_range(100, 200);
12556fad8f66SThierry Reding 	}
12566fad8f66SThierry Reding 
12576fad8f66SThierry Reding 	return -ETIMEDOUT;
12586fad8f66SThierry Reding }
12596fad8f66SThierry Reding 
1260530239a8SThierry Reding static int tegra_sor_show_crc(struct seq_file *s, void *data)
12616fad8f66SThierry Reding {
1262530239a8SThierry Reding 	struct drm_info_node *node = s->private;
1263530239a8SThierry Reding 	struct tegra_sor *sor = node->info_ent->data;
1264850bab44SThierry Reding 	struct drm_crtc *crtc = sor->output.encoder.crtc;
1265850bab44SThierry Reding 	struct drm_device *drm = node->minor->dev;
1266530239a8SThierry Reding 	int err = 0;
12676fad8f66SThierry Reding 	u32 value;
12686fad8f66SThierry Reding 
1269850bab44SThierry Reding 	drm_modeset_lock_all(drm);
12706fad8f66SThierry Reding 
1271850bab44SThierry Reding 	if (!crtc || !crtc->state->active) {
1272850bab44SThierry Reding 		err = -EBUSY;
12736fad8f66SThierry Reding 		goto unlock;
12746fad8f66SThierry Reding 	}
12756fad8f66SThierry Reding 
1276a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
12776fad8f66SThierry Reding 	value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
1278a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
12796fad8f66SThierry Reding 
12806fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
12816fad8f66SThierry Reding 	value |= SOR_CRC_CNTRL_ENABLE;
12826fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
12836fad8f66SThierry Reding 
12846fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_TEST);
12856fad8f66SThierry Reding 	value &= ~SOR_TEST_CRC_POST_SERIALIZE;
12866fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_TEST);
12876fad8f66SThierry Reding 
12886fad8f66SThierry Reding 	err = tegra_sor_crc_wait(sor, 100);
12896fad8f66SThierry Reding 	if (err < 0)
12906fad8f66SThierry Reding 		goto unlock;
12916fad8f66SThierry Reding 
1292a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, SOR_CRCA_RESET, SOR_CRCA);
1293a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_CRCB);
12946fad8f66SThierry Reding 
1295530239a8SThierry Reding 	seq_printf(s, "%08x\n", value);
12966fad8f66SThierry Reding 
12976fad8f66SThierry Reding unlock:
1298850bab44SThierry Reding 	drm_modeset_unlock_all(drm);
12996fad8f66SThierry Reding 	return err;
13006fad8f66SThierry Reding }
13016fad8f66SThierry Reding 
1302062f5b2cSThierry Reding #define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
1303062f5b2cSThierry Reding 
1304062f5b2cSThierry Reding static const struct debugfs_reg32 tegra_sor_regs[] = {
1305062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CTXSW),
1306062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SUPER_STATE0),
1307062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SUPER_STATE1),
1308062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_STATE0),
1309062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_STATE1),
1310062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE0(0)),
1311062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE0(1)),
1312062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE1(0)),
1313062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE1(1)),
1314062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE2(0)),
1315062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE2(1)),
1316062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE3(0)),
1317062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE3(1)),
1318062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE4(0)),
1319062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE4(1)),
1320062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE5(0)),
1321062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE5(1)),
1322062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CRC_CNTRL),
1323062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_DEBUG_MVID),
1324062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CLK_CNTRL),
1325062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CAP),
1326062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PWR),
1327062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_TEST),
1328062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL0),
1329062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL1),
1330062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL2),
1331062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL3),
1332062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CSTM),
1333062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LVDS),
1334062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CRCA),
1335062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CRCB),
1336062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_BLANK),
1337062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_CTL),
1338062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_SEQ_CTL),
1339062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(0)),
1340062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(1)),
1341062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(2)),
1342062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(3)),
1343062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(4)),
1344062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(5)),
1345062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(6)),
1346062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(7)),
1347062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(8)),
1348062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(9)),
1349062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(10)),
1350062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(11)),
1351062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(12)),
1352062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(13)),
1353062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(14)),
1354062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(15)),
1355062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PWM_DIV),
1356062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PWM_CTL),
1357062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_A0),
1358062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_A1),
1359062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_B0),
1360062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_B1),
1361062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_A0),
1362062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_A1),
1363062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_B0),
1364062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_B1),
1365062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_A0),
1366062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_A1),
1367062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_B0),
1368062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_B1),
1369062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_A0),
1370062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_A1),
1371062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_B0),
1372062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_B1),
1373062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_A0),
1374062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_A1),
1375062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_B0),
1376062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_B1),
1377062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_TRIG),
1378062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_MSCHECK),
1379062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_XBAR_CTRL),
1380062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_XBAR_POL),
1381062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LINKCTL0),
1382062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LINKCTL1),
1383062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT0),
1384062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT1),
1385062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT0),
1386062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT1),
1387062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_PREEMPHASIS0),
1388062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_PREEMPHASIS1),
1389062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS0),
1390062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS1),
1391062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_POSTCURSOR0),
1392062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_POSTCURSOR1),
1393062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_CONFIG0),
1394062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_CONFIG1),
1395062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_MN0),
1396062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_MN1),
1397062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_PADCTL0),
1398062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_PADCTL1),
1399c57997bcSThierry Reding 	DEBUGFS_REG32(SOR_DP_PADCTL2),
1400062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_DEBUG0),
1401062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_DEBUG1),
1402062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_SPARE0),
1403062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_SPARE1),
1404062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_AUDIO_CTRL),
1405062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_AUDIO_HBLANK_SYMBOLS),
1406062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_AUDIO_VBLANK_SYMBOLS),
1407062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_HEADER),
1408062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK0),
1409062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK1),
1410062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK2),
1411062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK3),
1412062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK4),
1413062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK5),
1414062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK6),
1415062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_TPG),
1416062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_TPG_CONFIG),
1417062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LQ_CSTM0),
1418062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LQ_CSTM1),
1419062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LQ_CSTM2),
1420062f5b2cSThierry Reding };
1421062f5b2cSThierry Reding 
1422dab16336SThierry Reding static int tegra_sor_show_regs(struct seq_file *s, void *data)
1423dab16336SThierry Reding {
1424dab16336SThierry Reding 	struct drm_info_node *node = s->private;
1425dab16336SThierry Reding 	struct tegra_sor *sor = node->info_ent->data;
1426850bab44SThierry Reding 	struct drm_crtc *crtc = sor->output.encoder.crtc;
1427850bab44SThierry Reding 	struct drm_device *drm = node->minor->dev;
1428062f5b2cSThierry Reding 	unsigned int i;
1429850bab44SThierry Reding 	int err = 0;
1430850bab44SThierry Reding 
1431850bab44SThierry Reding 	drm_modeset_lock_all(drm);
1432850bab44SThierry Reding 
1433850bab44SThierry Reding 	if (!crtc || !crtc->state->active) {
1434850bab44SThierry Reding 		err = -EBUSY;
1435850bab44SThierry Reding 		goto unlock;
1436850bab44SThierry Reding 	}
1437dab16336SThierry Reding 
1438062f5b2cSThierry Reding 	for (i = 0; i < ARRAY_SIZE(tegra_sor_regs); i++) {
1439062f5b2cSThierry Reding 		unsigned int offset = tegra_sor_regs[i].offset;
1440dab16336SThierry Reding 
1441062f5b2cSThierry Reding 		seq_printf(s, "%-38s %#05x %08x\n", tegra_sor_regs[i].name,
1442062f5b2cSThierry Reding 			   offset, tegra_sor_readl(sor, offset));
1443062f5b2cSThierry Reding 	}
1444dab16336SThierry Reding 
1445850bab44SThierry Reding unlock:
1446850bab44SThierry Reding 	drm_modeset_unlock_all(drm);
1447850bab44SThierry Reding 	return err;
1448dab16336SThierry Reding }
1449dab16336SThierry Reding 
1450dab16336SThierry Reding static const struct drm_info_list debugfs_files[] = {
1451530239a8SThierry Reding 	{ "crc", tegra_sor_show_crc, 0, NULL },
1452dab16336SThierry Reding 	{ "regs", tegra_sor_show_regs, 0, NULL },
1453dab16336SThierry Reding };
1454dab16336SThierry Reding 
14555b8e043bSThierry Reding static int tegra_sor_late_register(struct drm_connector *connector)
14566fad8f66SThierry Reding {
14575b8e043bSThierry Reding 	struct tegra_output *output = connector_to_output(connector);
14585b8e043bSThierry Reding 	unsigned int i, count = ARRAY_SIZE(debugfs_files);
14595b8e043bSThierry Reding 	struct drm_minor *minor = connector->dev->primary;
14605b8e043bSThierry Reding 	struct dentry *root = connector->debugfs_entry;
14615b8e043bSThierry Reding 	struct tegra_sor *sor = to_sor(output);
1462530239a8SThierry Reding 	int err;
14636fad8f66SThierry Reding 
1464dab16336SThierry Reding 	sor->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1465dab16336SThierry Reding 				     GFP_KERNEL);
14665b8e043bSThierry Reding 	if (!sor->debugfs_files)
14675b8e043bSThierry Reding 		return -ENOMEM;
14686fad8f66SThierry Reding 
14695b8e043bSThierry Reding 	for (i = 0; i < count; i++)
1470dab16336SThierry Reding 		sor->debugfs_files[i].data = sor;
1471dab16336SThierry Reding 
14725b8e043bSThierry Reding 	err = drm_debugfs_create_files(sor->debugfs_files, count, root, minor);
1473dab16336SThierry Reding 	if (err < 0)
1474dab16336SThierry Reding 		goto free;
1475dab16336SThierry Reding 
1476530239a8SThierry Reding 	return 0;
14776fad8f66SThierry Reding 
1478dab16336SThierry Reding free:
1479dab16336SThierry Reding 	kfree(sor->debugfs_files);
1480dab16336SThierry Reding 	sor->debugfs_files = NULL;
14815b8e043bSThierry Reding 
14826fad8f66SThierry Reding 	return err;
14836fad8f66SThierry Reding }
14846fad8f66SThierry Reding 
14855b8e043bSThierry Reding static void tegra_sor_early_unregister(struct drm_connector *connector)
14866fad8f66SThierry Reding {
14875b8e043bSThierry Reding 	struct tegra_output *output = connector_to_output(connector);
14885b8e043bSThierry Reding 	unsigned int count = ARRAY_SIZE(debugfs_files);
14895b8e043bSThierry Reding 	struct tegra_sor *sor = to_sor(output);
1490d92e6009SThierry Reding 
14915b8e043bSThierry Reding 	drm_debugfs_remove_files(sor->debugfs_files, count,
14925b8e043bSThierry Reding 				 connector->dev->primary);
1493dab16336SThierry Reding 	kfree(sor->debugfs_files);
1494066d30f8SThierry Reding 	sor->debugfs_files = NULL;
14956fad8f66SThierry Reding }
14966fad8f66SThierry Reding 
1497c31efa7aSThierry Reding static void tegra_sor_connector_reset(struct drm_connector *connector)
1498c31efa7aSThierry Reding {
1499c31efa7aSThierry Reding 	struct tegra_sor_state *state;
1500c31efa7aSThierry Reding 
1501c31efa7aSThierry Reding 	state = kzalloc(sizeof(*state), GFP_KERNEL);
1502c31efa7aSThierry Reding 	if (!state)
1503c31efa7aSThierry Reding 		return;
1504c31efa7aSThierry Reding 
1505c31efa7aSThierry Reding 	if (connector->state) {
1506c31efa7aSThierry Reding 		__drm_atomic_helper_connector_destroy_state(connector->state);
1507c31efa7aSThierry Reding 		kfree(connector->state);
1508c31efa7aSThierry Reding 	}
1509c31efa7aSThierry Reding 
1510c31efa7aSThierry Reding 	__drm_atomic_helper_connector_reset(connector, &state->base);
1511c31efa7aSThierry Reding }
1512c31efa7aSThierry Reding 
15136fad8f66SThierry Reding static enum drm_connector_status
15146fad8f66SThierry Reding tegra_sor_connector_detect(struct drm_connector *connector, bool force)
15156fad8f66SThierry Reding {
15166fad8f66SThierry Reding 	struct tegra_output *output = connector_to_output(connector);
15176fad8f66SThierry Reding 	struct tegra_sor *sor = to_sor(output);
15186fad8f66SThierry Reding 
15199542c237SThierry Reding 	if (sor->aux)
15209542c237SThierry Reding 		return drm_dp_aux_detect(sor->aux);
15216fad8f66SThierry Reding 
1522459cc2c6SThierry Reding 	return tegra_output_connector_detect(connector, force);
15236fad8f66SThierry Reding }
15246fad8f66SThierry Reding 
1525c31efa7aSThierry Reding static struct drm_connector_state *
1526c31efa7aSThierry Reding tegra_sor_connector_duplicate_state(struct drm_connector *connector)
1527c31efa7aSThierry Reding {
1528c31efa7aSThierry Reding 	struct tegra_sor_state *state = to_sor_state(connector->state);
1529c31efa7aSThierry Reding 	struct tegra_sor_state *copy;
1530c31efa7aSThierry Reding 
1531c31efa7aSThierry Reding 	copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
1532c31efa7aSThierry Reding 	if (!copy)
1533c31efa7aSThierry Reding 		return NULL;
1534c31efa7aSThierry Reding 
1535c31efa7aSThierry Reding 	__drm_atomic_helper_connector_duplicate_state(connector, &copy->base);
1536c31efa7aSThierry Reding 
1537c31efa7aSThierry Reding 	return &copy->base;
1538c31efa7aSThierry Reding }
1539c31efa7aSThierry Reding 
15406fad8f66SThierry Reding static const struct drm_connector_funcs tegra_sor_connector_funcs = {
1541c31efa7aSThierry Reding 	.reset = tegra_sor_connector_reset,
15426fad8f66SThierry Reding 	.detect = tegra_sor_connector_detect,
15436fad8f66SThierry Reding 	.fill_modes = drm_helper_probe_single_connector_modes,
15446fad8f66SThierry Reding 	.destroy = tegra_output_connector_destroy,
1545c31efa7aSThierry Reding 	.atomic_duplicate_state = tegra_sor_connector_duplicate_state,
15464aa3df71SThierry Reding 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
15475b8e043bSThierry Reding 	.late_register = tegra_sor_late_register,
15485b8e043bSThierry Reding 	.early_unregister = tegra_sor_early_unregister,
15496fad8f66SThierry Reding };
15506fad8f66SThierry Reding 
15516fad8f66SThierry Reding static int tegra_sor_connector_get_modes(struct drm_connector *connector)
15526fad8f66SThierry Reding {
15536fad8f66SThierry Reding 	struct tegra_output *output = connector_to_output(connector);
15546fad8f66SThierry Reding 	struct tegra_sor *sor = to_sor(output);
15556fad8f66SThierry Reding 	int err;
15566fad8f66SThierry Reding 
15579542c237SThierry Reding 	if (sor->aux)
15589542c237SThierry Reding 		drm_dp_aux_enable(sor->aux);
15596fad8f66SThierry Reding 
15606fad8f66SThierry Reding 	err = tegra_output_connector_get_modes(connector);
15616fad8f66SThierry Reding 
15629542c237SThierry Reding 	if (sor->aux)
15639542c237SThierry Reding 		drm_dp_aux_disable(sor->aux);
15646fad8f66SThierry Reding 
15656fad8f66SThierry Reding 	return err;
15666fad8f66SThierry Reding }
15676fad8f66SThierry Reding 
15686fad8f66SThierry Reding static enum drm_mode_status
15696fad8f66SThierry Reding tegra_sor_connector_mode_valid(struct drm_connector *connector,
15706fad8f66SThierry Reding 			       struct drm_display_mode *mode)
15716fad8f66SThierry Reding {
15726fad8f66SThierry Reding 	return MODE_OK;
15736fad8f66SThierry Reding }
15746fad8f66SThierry Reding 
15756fad8f66SThierry Reding static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
15766fad8f66SThierry Reding 	.get_modes = tegra_sor_connector_get_modes,
15776fad8f66SThierry Reding 	.mode_valid = tegra_sor_connector_mode_valid,
15786fad8f66SThierry Reding };
15796fad8f66SThierry Reding 
15806fad8f66SThierry Reding static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
15816fad8f66SThierry Reding 	.destroy = tegra_output_encoder_destroy,
15826fad8f66SThierry Reding };
15836fad8f66SThierry Reding 
1584850bab44SThierry Reding static void tegra_sor_edp_disable(struct drm_encoder *encoder)
15856fad8f66SThierry Reding {
1586850bab44SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
1587850bab44SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1588850bab44SThierry Reding 	struct tegra_sor *sor = to_sor(output);
1589850bab44SThierry Reding 	u32 value;
1590850bab44SThierry Reding 	int err;
1591850bab44SThierry Reding 
1592850bab44SThierry Reding 	if (output->panel)
1593850bab44SThierry Reding 		drm_panel_disable(output->panel);
1594850bab44SThierry Reding 
1595850bab44SThierry Reding 	err = tegra_sor_detach(sor);
1596850bab44SThierry Reding 	if (err < 0)
1597850bab44SThierry Reding 		dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1598850bab44SThierry Reding 
1599850bab44SThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE1);
1600850bab44SThierry Reding 	tegra_sor_update(sor);
1601850bab44SThierry Reding 
1602850bab44SThierry Reding 	/*
1603850bab44SThierry Reding 	 * The following accesses registers of the display controller, so make
1604850bab44SThierry Reding 	 * sure it's only executed when the output is attached to one.
1605850bab44SThierry Reding 	 */
1606850bab44SThierry Reding 	if (dc) {
1607850bab44SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1608c57997bcSThierry Reding 		value &= ~SOR_ENABLE(0);
1609850bab44SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1610850bab44SThierry Reding 
1611850bab44SThierry Reding 		tegra_dc_commit(dc);
16126fad8f66SThierry Reding 	}
16136fad8f66SThierry Reding 
1614850bab44SThierry Reding 	err = tegra_sor_power_down(sor);
1615850bab44SThierry Reding 	if (err < 0)
1616850bab44SThierry Reding 		dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1617850bab44SThierry Reding 
16189542c237SThierry Reding 	if (sor->aux) {
16199542c237SThierry Reding 		err = drm_dp_aux_disable(sor->aux);
1620850bab44SThierry Reding 		if (err < 0)
1621850bab44SThierry Reding 			dev_err(sor->dev, "failed to disable DP: %d\n", err);
16226fad8f66SThierry Reding 	}
16236fad8f66SThierry Reding 
1624c57997bcSThierry Reding 	err = tegra_io_pad_power_disable(sor->pad);
1625850bab44SThierry Reding 	if (err < 0)
1626c57997bcSThierry Reding 		dev_err(sor->dev, "failed to power off I/O pad: %d\n", err);
1627850bab44SThierry Reding 
1628850bab44SThierry Reding 	if (output->panel)
1629850bab44SThierry Reding 		drm_panel_unprepare(output->panel);
1630850bab44SThierry Reding 
1631aaff8bd2SThierry Reding 	pm_runtime_put(sor->dev);
16326fad8f66SThierry Reding }
16336fad8f66SThierry Reding 
1634459cc2c6SThierry Reding #if 0
1635459cc2c6SThierry Reding static int calc_h_ref_to_sync(const struct drm_display_mode *mode,
1636459cc2c6SThierry Reding 			      unsigned int *value)
1637459cc2c6SThierry Reding {
1638459cc2c6SThierry Reding 	unsigned int hfp, hsw, hbp, a = 0, b;
1639459cc2c6SThierry Reding 
1640459cc2c6SThierry Reding 	hfp = mode->hsync_start - mode->hdisplay;
1641459cc2c6SThierry Reding 	hsw = mode->hsync_end - mode->hsync_start;
1642459cc2c6SThierry Reding 	hbp = mode->htotal - mode->hsync_end;
1643459cc2c6SThierry Reding 
1644459cc2c6SThierry Reding 	pr_info("hfp: %u, hsw: %u, hbp: %u\n", hfp, hsw, hbp);
1645459cc2c6SThierry Reding 
1646459cc2c6SThierry Reding 	b = hfp - 1;
1647459cc2c6SThierry Reding 
1648459cc2c6SThierry Reding 	pr_info("a: %u, b: %u\n", a, b);
1649459cc2c6SThierry Reding 	pr_info("a + hsw + hbp = %u\n", a + hsw + hbp);
1650459cc2c6SThierry Reding 
1651459cc2c6SThierry Reding 	if (a + hsw + hbp <= 11) {
1652459cc2c6SThierry Reding 		a = 1 + 11 - hsw - hbp;
1653459cc2c6SThierry Reding 		pr_info("a: %u\n", a);
1654459cc2c6SThierry Reding 	}
1655459cc2c6SThierry Reding 
1656459cc2c6SThierry Reding 	if (a > b)
1657459cc2c6SThierry Reding 		return -EINVAL;
1658459cc2c6SThierry Reding 
1659459cc2c6SThierry Reding 	if (hsw < 1)
1660459cc2c6SThierry Reding 		return -EINVAL;
1661459cc2c6SThierry Reding 
1662459cc2c6SThierry Reding 	if (mode->hdisplay < 16)
1663459cc2c6SThierry Reding 		return -EINVAL;
1664459cc2c6SThierry Reding 
1665459cc2c6SThierry Reding 	if (value) {
1666459cc2c6SThierry Reding 		if (b > a && a % 2)
1667459cc2c6SThierry Reding 			*value = a + 1;
1668459cc2c6SThierry Reding 		else
1669459cc2c6SThierry Reding 			*value = a;
1670459cc2c6SThierry Reding 	}
1671459cc2c6SThierry Reding 
1672459cc2c6SThierry Reding 	return 0;
1673459cc2c6SThierry Reding }
1674459cc2c6SThierry Reding #endif
1675459cc2c6SThierry Reding 
1676850bab44SThierry Reding static void tegra_sor_edp_enable(struct drm_encoder *encoder)
16776fad8f66SThierry Reding {
1678850bab44SThierry Reding 	struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
16796fad8f66SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
16806fad8f66SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
16816b6b6042SThierry Reding 	struct tegra_sor *sor = to_sor(output);
168234fa183bSThierry Reding 	struct tegra_sor_config config;
1683c31efa7aSThierry Reding 	struct tegra_sor_state *state;
168434fa183bSThierry Reding 	struct drm_dp_link link;
168501b9bea0SThierry Reding 	u8 rate, lanes;
16862bd1dd39SThierry Reding 	unsigned int i;
168786f5c52dSThierry Reding 	int err = 0;
168828fe2076SThierry Reding 	u32 value;
168986f5c52dSThierry Reding 
1690c31efa7aSThierry Reding 	state = to_sor_state(output->connector.state);
16916b6b6042SThierry Reding 
1692aaff8bd2SThierry Reding 	pm_runtime_get_sync(sor->dev);
16936b6b6042SThierry Reding 
16946fad8f66SThierry Reding 	if (output->panel)
16956fad8f66SThierry Reding 		drm_panel_prepare(output->panel);
16966fad8f66SThierry Reding 
16979542c237SThierry Reding 	err = drm_dp_aux_enable(sor->aux);
16986b6b6042SThierry Reding 	if (err < 0)
16996b6b6042SThierry Reding 		dev_err(sor->dev, "failed to enable DP: %d\n", err);
170034fa183bSThierry Reding 
17019542c237SThierry Reding 	err = drm_dp_link_probe(sor->aux, &link);
170234fa183bSThierry Reding 	if (err < 0) {
170301b9bea0SThierry Reding 		dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
1704850bab44SThierry Reding 		return;
170534fa183bSThierry Reding 	}
17066b6b6042SThierry Reding 
170725bb2cecSThierry Reding 	/* switch to safe parent clock */
170825bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
17096b6b6042SThierry Reding 	if (err < 0)
17106b6b6042SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
17116b6b6042SThierry Reding 
171234fa183bSThierry Reding 	memset(&config, 0, sizeof(config));
1713c31efa7aSThierry Reding 	config.bits_per_pixel = state->bpc * 3;
171434fa183bSThierry Reding 
1715a198359eSThierry Reding 	err = tegra_sor_compute_config(sor, mode, &config, &link);
171634fa183bSThierry Reding 	if (err < 0)
1717a198359eSThierry Reding 		dev_err(sor->dev, "failed to compute configuration: %d\n", err);
171834fa183bSThierry Reding 
17196b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
17206b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
17216b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
17226b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
17236b6b6042SThierry Reding 
1724880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1725a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1726880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
17276b6b6042SThierry Reding 	usleep_range(20, 100);
17286b6b6042SThierry Reding 
1729880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll3);
1730a9a9e4fdSThierry Reding 	value |= SOR_PLL3_PLL_VDD_MODE_3V3;
1731880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll3);
17326b6b6042SThierry Reding 
1733a9a9e4fdSThierry Reding 	value = SOR_PLL0_ICHPMP(0xf) | SOR_PLL0_VCOCAP_RST |
1734a9a9e4fdSThierry Reding 		SOR_PLL0_PLLREG_LEVEL_V45 | SOR_PLL0_RESISTOR_EXT;
1735880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
17366b6b6042SThierry Reding 
1737880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1738a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD;
1739a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1740a9a9e4fdSThierry Reding 	value |= SOR_PLL2_LVDS_ENABLE;
1741880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
17426b6b6042SThierry Reding 
1743a9a9e4fdSThierry Reding 	value = SOR_PLL1_TERM_COMPOUT | SOR_PLL1_TMDS_TERM;
1744880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll1);
17456b6b6042SThierry Reding 
17466b6b6042SThierry Reding 	while (true) {
1747880cee0bSThierry Reding 		value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1748a9a9e4fdSThierry Reding 		if ((value & SOR_PLL2_SEQ_PLLCAPPD_ENFORCE) == 0)
17496b6b6042SThierry Reding 			break;
17506b6b6042SThierry Reding 
17516b6b6042SThierry Reding 		usleep_range(250, 1000);
17526b6b6042SThierry Reding 	}
17536b6b6042SThierry Reding 
1754880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1755a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
1756a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
1757880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
17586b6b6042SThierry Reding 
17596b6b6042SThierry Reding 	/*
17606b6b6042SThierry Reding 	 * power up
17616b6b6042SThierry Reding 	 */
17626b6b6042SThierry Reding 
17636b6b6042SThierry Reding 	/* set safe link bandwidth (1.62 Gbps) */
17646b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
17656b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
17666b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
17676b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
17686b6b6042SThierry Reding 
17696b6b6042SThierry Reding 	/* step 1 */
1770880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1771a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL2_PORT_POWERDOWN |
1772a9a9e4fdSThierry Reding 		 SOR_PLL2_BANDGAP_POWERDOWN;
1773880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
17746b6b6042SThierry Reding 
1775880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
1776a9a9e4fdSThierry Reding 	value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
1777880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
17786b6b6042SThierry Reding 
1779880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
17806b6b6042SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
1781880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
17826b6b6042SThierry Reding 
17836b6b6042SThierry Reding 	/* step 2 */
1784c57997bcSThierry Reding 	err = tegra_io_pad_power_enable(sor->pad);
1785850bab44SThierry Reding 	if (err < 0)
1786c57997bcSThierry Reding 		dev_err(sor->dev, "failed to power on I/O pad: %d\n", err);
17876b6b6042SThierry Reding 
17886b6b6042SThierry Reding 	usleep_range(5, 100);
17896b6b6042SThierry Reding 
17906b6b6042SThierry Reding 	/* step 3 */
1791880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1792a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1793880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
17946b6b6042SThierry Reding 
17956b6b6042SThierry Reding 	usleep_range(20, 100);
17966b6b6042SThierry Reding 
17976b6b6042SThierry Reding 	/* step 4 */
1798880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
1799a9a9e4fdSThierry Reding 	value &= ~SOR_PLL0_VCOPD;
1800a9a9e4fdSThierry Reding 	value &= ~SOR_PLL0_PWR;
1801880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
18026b6b6042SThierry Reding 
1803880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1804a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1805880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
18066b6b6042SThierry Reding 
18076b6b6042SThierry Reding 	usleep_range(200, 1000);
18086b6b6042SThierry Reding 
18096b6b6042SThierry Reding 	/* step 5 */
1810880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1811a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
1812880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
18136b6b6042SThierry Reding 
181430b49435SThierry Reding 	/* XXX not in TRM */
181530b49435SThierry Reding 	for (value = 0, i = 0; i < 5; i++)
18166d6c815dSThierry Reding 		value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->xbar_cfg[i]) |
181730b49435SThierry Reding 			 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
181830b49435SThierry Reding 
181930b49435SThierry Reding 	tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
182030b49435SThierry Reding 	tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
182130b49435SThierry Reding 
182225bb2cecSThierry Reding 	/* switch to DP parent clock */
182325bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_dp);
18246b6b6042SThierry Reding 	if (err < 0)
182525bb2cecSThierry Reding 		dev_err(sor->dev, "failed to set parent clock: %d\n", err);
18266b6b6042SThierry Reding 
1827899451b7SThierry Reding 	/* power DP lanes */
1828880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
1829899451b7SThierry Reding 
1830899451b7SThierry Reding 	if (link.num_lanes <= 2)
1831899451b7SThierry Reding 		value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2);
1832899451b7SThierry Reding 	else
1833899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2;
1834899451b7SThierry Reding 
1835899451b7SThierry Reding 	if (link.num_lanes <= 1)
1836899451b7SThierry Reding 		value &= ~SOR_DP_PADCTL_PD_TXD_1;
1837899451b7SThierry Reding 	else
1838899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_1;
1839899451b7SThierry Reding 
1840899451b7SThierry Reding 	if (link.num_lanes == 0)
1841899451b7SThierry Reding 		value &= ~SOR_DP_PADCTL_PD_TXD_0;
1842899451b7SThierry Reding 	else
1843899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_0;
1844899451b7SThierry Reding 
1845880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
18466b6b6042SThierry Reding 
1847a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
18486b6b6042SThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
18490c90a184SThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(link.num_lanes);
1850a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
18516b6b6042SThierry Reding 
18526b6b6042SThierry Reding 	/* start lane sequencer */
18536b6b6042SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
18546b6b6042SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_UP;
18556b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
18566b6b6042SThierry Reding 
18576b6b6042SThierry Reding 	while (true) {
18586b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
18596b6b6042SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
18606b6b6042SThierry Reding 			break;
18616b6b6042SThierry Reding 
18626b6b6042SThierry Reding 		usleep_range(250, 1000);
18636b6b6042SThierry Reding 	}
18646b6b6042SThierry Reding 
1865a4263fedSThierry Reding 	/* set link bandwidth */
18666b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
18676b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1868a4263fedSThierry Reding 	value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
18696b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
18706b6b6042SThierry Reding 
1871402f6bcdSThierry Reding 	tegra_sor_apply_config(sor, &config);
1872402f6bcdSThierry Reding 
1873402f6bcdSThierry Reding 	/* enable link */
1874a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
18756b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_ENABLE;
18766b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
1877a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
18786b6b6042SThierry Reding 
18796b6b6042SThierry Reding 	for (i = 0, value = 0; i < 4; i++) {
18806b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
18816b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
18826b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
18836b6b6042SThierry Reding 		value = (value << 8) | lane;
18846b6b6042SThierry Reding 	}
18856b6b6042SThierry Reding 
18866b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
18876b6b6042SThierry Reding 
18886b6b6042SThierry Reding 	/* enable pad calibration logic */
1889880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
18906b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
1891880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
18926b6b6042SThierry Reding 
18939542c237SThierry Reding 	err = drm_dp_link_probe(sor->aux, &link);
1894850bab44SThierry Reding 	if (err < 0)
189501b9bea0SThierry Reding 		dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
18966b6b6042SThierry Reding 
18979542c237SThierry Reding 	err = drm_dp_link_power_up(sor->aux, &link);
1898850bab44SThierry Reding 	if (err < 0)
189901b9bea0SThierry Reding 		dev_err(sor->dev, "failed to power up eDP link: %d\n", err);
19006b6b6042SThierry Reding 
19019542c237SThierry Reding 	err = drm_dp_link_configure(sor->aux, &link);
1902850bab44SThierry Reding 	if (err < 0)
190301b9bea0SThierry Reding 		dev_err(sor->dev, "failed to configure eDP link: %d\n", err);
19046b6b6042SThierry Reding 
19056b6b6042SThierry Reding 	rate = drm_dp_link_rate_to_bw_code(link.rate);
19066b6b6042SThierry Reding 	lanes = link.num_lanes;
19076b6b6042SThierry Reding 
19086b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
19096b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
19106b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
19116b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
19126b6b6042SThierry Reding 
1913a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
19146b6b6042SThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
19156b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
19166b6b6042SThierry Reding 
19176b6b6042SThierry Reding 	if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
19186b6b6042SThierry Reding 		value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
19196b6b6042SThierry Reding 
1920a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
19216b6b6042SThierry Reding 
19226b6b6042SThierry Reding 	/* disable training pattern generator */
19236b6b6042SThierry Reding 
19246b6b6042SThierry Reding 	for (i = 0; i < link.num_lanes; i++) {
19256b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
19266b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
19276b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
19286b6b6042SThierry Reding 		value = (value << 8) | lane;
19296b6b6042SThierry Reding 	}
19306b6b6042SThierry Reding 
19316b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
19326b6b6042SThierry Reding 
19336b6b6042SThierry Reding 	err = tegra_sor_dp_train_fast(sor, &link);
193401b9bea0SThierry Reding 	if (err < 0)
193501b9bea0SThierry Reding 		dev_err(sor->dev, "DP fast link training failed: %d\n", err);
19366b6b6042SThierry Reding 
19376b6b6042SThierry Reding 	dev_dbg(sor->dev, "fast link training succeeded\n");
19386b6b6042SThierry Reding 
19396b6b6042SThierry Reding 	err = tegra_sor_power_up(sor, 250);
1940850bab44SThierry Reding 	if (err < 0)
19416b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
19426b6b6042SThierry Reding 
19436b6b6042SThierry Reding 	/* CSTM (LVDS, link A/B, upper) */
1944143b1df2SStéphane Marchesin 	value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
19456b6b6042SThierry Reding 		SOR_CSTM_UPPER;
19466b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CSTM);
19476b6b6042SThierry Reding 
19482bd1dd39SThierry Reding 	/* use DP-A protocol */
19492bd1dd39SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
19502bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
19512bd1dd39SThierry Reding 	value |= SOR_STATE_ASY_PROTOCOL_DP_A;
19522bd1dd39SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
19532bd1dd39SThierry Reding 
1954c31efa7aSThierry Reding 	tegra_sor_mode_set(sor, mode, state);
19552bd1dd39SThierry Reding 
19566b6b6042SThierry Reding 	/* PWM setup */
19576b6b6042SThierry Reding 	err = tegra_sor_setup_pwm(sor, 250);
1958850bab44SThierry Reding 	if (err < 0)
19596b6b6042SThierry Reding 		dev_err(sor->dev, "failed to setup PWM: %d\n", err);
19606b6b6042SThierry Reding 
1961666cb873SThierry Reding 	tegra_sor_update(sor);
1962666cb873SThierry Reding 
19636b6b6042SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1964c57997bcSThierry Reding 	value |= SOR_ENABLE(0);
19656b6b6042SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
19666b6b6042SThierry Reding 
1967666cb873SThierry Reding 	tegra_dc_commit(dc);
19686b6b6042SThierry Reding 
19696b6b6042SThierry Reding 	err = tegra_sor_attach(sor);
1970850bab44SThierry Reding 	if (err < 0)
19716b6b6042SThierry Reding 		dev_err(sor->dev, "failed to attach SOR: %d\n", err);
19726b6b6042SThierry Reding 
19736b6b6042SThierry Reding 	err = tegra_sor_wakeup(sor);
1974850bab44SThierry Reding 	if (err < 0)
19756b6b6042SThierry Reding 		dev_err(sor->dev, "failed to enable DC: %d\n", err);
19766b6b6042SThierry Reding 
19776fad8f66SThierry Reding 	if (output->panel)
19786fad8f66SThierry Reding 		drm_panel_enable(output->panel);
19796b6b6042SThierry Reding }
19806b6b6042SThierry Reding 
198182f1511cSThierry Reding static int
198282f1511cSThierry Reding tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
198382f1511cSThierry Reding 			       struct drm_crtc_state *crtc_state,
198482f1511cSThierry Reding 			       struct drm_connector_state *conn_state)
198582f1511cSThierry Reding {
198682f1511cSThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
1987c31efa7aSThierry Reding 	struct tegra_sor_state *state = to_sor_state(conn_state);
198882f1511cSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
198982f1511cSThierry Reding 	unsigned long pclk = crtc_state->mode.clock * 1000;
199082f1511cSThierry Reding 	struct tegra_sor *sor = to_sor(output);
1991c31efa7aSThierry Reding 	struct drm_display_info *info;
199282f1511cSThierry Reding 	int err;
199382f1511cSThierry Reding 
1994c31efa7aSThierry Reding 	info = &output->connector.display_info;
1995c31efa7aSThierry Reding 
199636e90221SThierry Reding 	/*
199736e90221SThierry Reding 	 * For HBR2 modes, the SOR brick needs to use the x20 multiplier, so
199836e90221SThierry Reding 	 * the pixel clock must be corrected accordingly.
199936e90221SThierry Reding 	 */
200036e90221SThierry Reding 	if (pclk >= 340000000) {
200136e90221SThierry Reding 		state->link_speed = 20;
200236e90221SThierry Reding 		state->pclk = pclk / 2;
200336e90221SThierry Reding 	} else {
200436e90221SThierry Reding 		state->link_speed = 10;
200536e90221SThierry Reding 		state->pclk = pclk;
200636e90221SThierry Reding 	}
200736e90221SThierry Reding 
200882f1511cSThierry Reding 	err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
200982f1511cSThierry Reding 					 pclk, 0);
201082f1511cSThierry Reding 	if (err < 0) {
201182f1511cSThierry Reding 		dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
201282f1511cSThierry Reding 		return err;
201382f1511cSThierry Reding 	}
201482f1511cSThierry Reding 
2015c31efa7aSThierry Reding 	switch (info->bpc) {
2016c31efa7aSThierry Reding 	case 8:
2017c31efa7aSThierry Reding 	case 6:
2018c31efa7aSThierry Reding 		state->bpc = info->bpc;
2019c31efa7aSThierry Reding 		break;
2020c31efa7aSThierry Reding 
2021c31efa7aSThierry Reding 	default:
2022c31efa7aSThierry Reding 		DRM_DEBUG_KMS("%u bits-per-color not supported\n", info->bpc);
2023c31efa7aSThierry Reding 		state->bpc = 8;
2024c31efa7aSThierry Reding 		break;
2025c31efa7aSThierry Reding 	}
2026c31efa7aSThierry Reding 
202782f1511cSThierry Reding 	return 0;
202882f1511cSThierry Reding }
202982f1511cSThierry Reding 
2030459cc2c6SThierry Reding static const struct drm_encoder_helper_funcs tegra_sor_edp_helpers = {
2031850bab44SThierry Reding 	.disable = tegra_sor_edp_disable,
2032850bab44SThierry Reding 	.enable = tegra_sor_edp_enable,
203382f1511cSThierry Reding 	.atomic_check = tegra_sor_encoder_atomic_check,
20346b6b6042SThierry Reding };
20356b6b6042SThierry Reding 
2036459cc2c6SThierry Reding static inline u32 tegra_sor_hdmi_subpack(const u8 *ptr, size_t size)
2037459cc2c6SThierry Reding {
2038459cc2c6SThierry Reding 	u32 value = 0;
2039459cc2c6SThierry Reding 	size_t i;
2040459cc2c6SThierry Reding 
2041459cc2c6SThierry Reding 	for (i = size; i > 0; i--)
2042459cc2c6SThierry Reding 		value = (value << 8) | ptr[i - 1];
2043459cc2c6SThierry Reding 
2044459cc2c6SThierry Reding 	return value;
2045459cc2c6SThierry Reding }
2046459cc2c6SThierry Reding 
2047459cc2c6SThierry Reding static void tegra_sor_hdmi_write_infopack(struct tegra_sor *sor,
2048459cc2c6SThierry Reding 					  const void *data, size_t size)
2049459cc2c6SThierry Reding {
2050459cc2c6SThierry Reding 	const u8 *ptr = data;
2051459cc2c6SThierry Reding 	unsigned long offset;
2052459cc2c6SThierry Reding 	size_t i, j;
2053459cc2c6SThierry Reding 	u32 value;
2054459cc2c6SThierry Reding 
2055459cc2c6SThierry Reding 	switch (ptr[0]) {
2056459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_AVI:
2057459cc2c6SThierry Reding 		offset = SOR_HDMI_AVI_INFOFRAME_HEADER;
2058459cc2c6SThierry Reding 		break;
2059459cc2c6SThierry Reding 
2060459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_AUDIO:
2061459cc2c6SThierry Reding 		offset = SOR_HDMI_AUDIO_INFOFRAME_HEADER;
2062459cc2c6SThierry Reding 		break;
2063459cc2c6SThierry Reding 
2064459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_VENDOR:
2065459cc2c6SThierry Reding 		offset = SOR_HDMI_VSI_INFOFRAME_HEADER;
2066459cc2c6SThierry Reding 		break;
2067459cc2c6SThierry Reding 
2068459cc2c6SThierry Reding 	default:
2069459cc2c6SThierry Reding 		dev_err(sor->dev, "unsupported infoframe type: %02x\n",
2070459cc2c6SThierry Reding 			ptr[0]);
2071459cc2c6SThierry Reding 		return;
2072459cc2c6SThierry Reding 	}
2073459cc2c6SThierry Reding 
2074459cc2c6SThierry Reding 	value = INFOFRAME_HEADER_TYPE(ptr[0]) |
2075459cc2c6SThierry Reding 		INFOFRAME_HEADER_VERSION(ptr[1]) |
2076459cc2c6SThierry Reding 		INFOFRAME_HEADER_LEN(ptr[2]);
2077459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, offset);
2078459cc2c6SThierry Reding 	offset++;
2079459cc2c6SThierry Reding 
2080459cc2c6SThierry Reding 	/*
2081459cc2c6SThierry Reding 	 * Each subpack contains 7 bytes, divided into:
2082459cc2c6SThierry Reding 	 * - subpack_low: bytes 0 - 3
2083459cc2c6SThierry Reding 	 * - subpack_high: bytes 4 - 6 (with byte 7 padded to 0x00)
2084459cc2c6SThierry Reding 	 */
2085459cc2c6SThierry Reding 	for (i = 3, j = 0; i < size; i += 7, j += 8) {
2086459cc2c6SThierry Reding 		size_t rem = size - i, num = min_t(size_t, rem, 4);
2087459cc2c6SThierry Reding 
2088459cc2c6SThierry Reding 		value = tegra_sor_hdmi_subpack(&ptr[i], num);
2089459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, offset++);
2090459cc2c6SThierry Reding 
2091459cc2c6SThierry Reding 		num = min_t(size_t, rem - num, 3);
2092459cc2c6SThierry Reding 
2093459cc2c6SThierry Reding 		value = tegra_sor_hdmi_subpack(&ptr[i + 4], num);
2094459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, offset++);
2095459cc2c6SThierry Reding 	}
2096459cc2c6SThierry Reding }
2097459cc2c6SThierry Reding 
2098459cc2c6SThierry Reding static int
2099459cc2c6SThierry Reding tegra_sor_hdmi_setup_avi_infoframe(struct tegra_sor *sor,
2100459cc2c6SThierry Reding 				   const struct drm_display_mode *mode)
2101459cc2c6SThierry Reding {
2102459cc2c6SThierry Reding 	u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
2103459cc2c6SThierry Reding 	struct hdmi_avi_infoframe frame;
2104459cc2c6SThierry Reding 	u32 value;
2105459cc2c6SThierry Reding 	int err;
2106459cc2c6SThierry Reding 
2107459cc2c6SThierry Reding 	/* disable AVI infoframe */
2108459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
2109459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_SINGLE;
2110459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_OTHER;
2111459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_ENABLE;
2112459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
2113459cc2c6SThierry Reding 
211413d0add3SVille Syrjälä 	err = drm_hdmi_avi_infoframe_from_display_mode(&frame,
211513d0add3SVille Syrjälä 						       &sor->output.connector, mode);
2116459cc2c6SThierry Reding 	if (err < 0) {
2117459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
2118459cc2c6SThierry Reding 		return err;
2119459cc2c6SThierry Reding 	}
2120459cc2c6SThierry Reding 
2121459cc2c6SThierry Reding 	err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
2122459cc2c6SThierry Reding 	if (err < 0) {
2123459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to pack AVI infoframe: %d\n", err);
2124459cc2c6SThierry Reding 		return err;
2125459cc2c6SThierry Reding 	}
2126459cc2c6SThierry Reding 
2127459cc2c6SThierry Reding 	tegra_sor_hdmi_write_infopack(sor, buffer, err);
2128459cc2c6SThierry Reding 
2129459cc2c6SThierry Reding 	/* enable AVI infoframe */
2130459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
2131459cc2c6SThierry Reding 	value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
2132459cc2c6SThierry Reding 	value |= INFOFRAME_CTRL_ENABLE;
2133459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
2134459cc2c6SThierry Reding 
2135459cc2c6SThierry Reding 	return 0;
2136459cc2c6SThierry Reding }
2137459cc2c6SThierry Reding 
21388e2988a7SThierry Reding static void tegra_sor_write_eld(struct tegra_sor *sor)
21398e2988a7SThierry Reding {
21408e2988a7SThierry Reding 	size_t length = drm_eld_size(sor->output.connector.eld), i;
21418e2988a7SThierry Reding 
21428e2988a7SThierry Reding 	for (i = 0; i < length; i++)
21438e2988a7SThierry Reding 		tegra_sor_writel(sor, i << 8 | sor->output.connector.eld[i],
21448e2988a7SThierry Reding 				 SOR_AUDIO_HDA_ELD_BUFWR);
21458e2988a7SThierry Reding 
21468e2988a7SThierry Reding 	/*
21478e2988a7SThierry Reding 	 * The HDA codec will always report an ELD buffer size of 96 bytes and
21488e2988a7SThierry Reding 	 * the HDA codec driver will check that each byte read from the buffer
21498e2988a7SThierry Reding 	 * is valid. Therefore every byte must be written, even if no 96 bytes
21508e2988a7SThierry Reding 	 * were parsed from EDID.
21518e2988a7SThierry Reding 	 */
21528e2988a7SThierry Reding 	for (i = length; i < 96; i++)
21538e2988a7SThierry Reding 		tegra_sor_writel(sor, i << 8 | 0, SOR_AUDIO_HDA_ELD_BUFWR);
21548e2988a7SThierry Reding }
21558e2988a7SThierry Reding 
21568e2988a7SThierry Reding static void tegra_sor_audio_prepare(struct tegra_sor *sor)
21578e2988a7SThierry Reding {
21588e2988a7SThierry Reding 	u32 value;
21598e2988a7SThierry Reding 
21608e2988a7SThierry Reding 	tegra_sor_write_eld(sor);
21618e2988a7SThierry Reding 
21628e2988a7SThierry Reding 	value = SOR_AUDIO_HDA_PRESENSE_ELDV | SOR_AUDIO_HDA_PRESENSE_PD;
21638e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_AUDIO_HDA_PRESENSE);
21648e2988a7SThierry Reding }
21658e2988a7SThierry Reding 
21668e2988a7SThierry Reding static void tegra_sor_audio_unprepare(struct tegra_sor *sor)
21678e2988a7SThierry Reding {
21688e2988a7SThierry Reding 	tegra_sor_writel(sor, 0, SOR_AUDIO_HDA_PRESENSE);
21698e2988a7SThierry Reding }
21708e2988a7SThierry Reding 
21718e2988a7SThierry Reding static int tegra_sor_hdmi_enable_audio_infoframe(struct tegra_sor *sor)
21728e2988a7SThierry Reding {
21738e2988a7SThierry Reding 	u8 buffer[HDMI_INFOFRAME_SIZE(AUDIO)];
21748e2988a7SThierry Reding 	struct hdmi_audio_infoframe frame;
21758e2988a7SThierry Reding 	u32 value;
21768e2988a7SThierry Reding 	int err;
21778e2988a7SThierry Reding 
21788e2988a7SThierry Reding 	err = hdmi_audio_infoframe_init(&frame);
21798e2988a7SThierry Reding 	if (err < 0) {
21808e2988a7SThierry Reding 		dev_err(sor->dev, "failed to setup audio infoframe: %d\n", err);
21818e2988a7SThierry Reding 		return err;
21828e2988a7SThierry Reding 	}
21838e2988a7SThierry Reding 
2184fad7b806SThierry Reding 	frame.channels = sor->format.channels;
21858e2988a7SThierry Reding 
21868e2988a7SThierry Reding 	err = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
21878e2988a7SThierry Reding 	if (err < 0) {
21888e2988a7SThierry Reding 		dev_err(sor->dev, "failed to pack audio infoframe: %d\n", err);
21898e2988a7SThierry Reding 		return err;
21908e2988a7SThierry Reding 	}
21918e2988a7SThierry Reding 
21928e2988a7SThierry Reding 	tegra_sor_hdmi_write_infopack(sor, buffer, err);
21938e2988a7SThierry Reding 
21948e2988a7SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
21958e2988a7SThierry Reding 	value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
21968e2988a7SThierry Reding 	value |= INFOFRAME_CTRL_ENABLE;
21978e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
21988e2988a7SThierry Reding 
21998e2988a7SThierry Reding 	return 0;
22008e2988a7SThierry Reding }
22018e2988a7SThierry Reding 
22028e2988a7SThierry Reding static void tegra_sor_hdmi_audio_enable(struct tegra_sor *sor)
22038e2988a7SThierry Reding {
22048e2988a7SThierry Reding 	u32 value;
22058e2988a7SThierry Reding 
22068e2988a7SThierry Reding 	value = tegra_sor_readl(sor, SOR_AUDIO_CNTRL);
22078e2988a7SThierry Reding 
22088e2988a7SThierry Reding 	/* select HDA audio input */
22098e2988a7SThierry Reding 	value &= ~SOR_AUDIO_CNTRL_SOURCE_SELECT(SOURCE_SELECT_MASK);
22108e2988a7SThierry Reding 	value |= SOR_AUDIO_CNTRL_SOURCE_SELECT(SOURCE_SELECT_HDA);
22118e2988a7SThierry Reding 
22128e2988a7SThierry Reding 	/* inject null samples */
2213fad7b806SThierry Reding 	if (sor->format.channels != 2)
22148e2988a7SThierry Reding 		value &= ~SOR_AUDIO_CNTRL_INJECT_NULLSMPL;
22158e2988a7SThierry Reding 	else
22168e2988a7SThierry Reding 		value |= SOR_AUDIO_CNTRL_INJECT_NULLSMPL;
22178e2988a7SThierry Reding 
22188e2988a7SThierry Reding 	value |= SOR_AUDIO_CNTRL_AFIFO_FLUSH;
22198e2988a7SThierry Reding 
22208e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_AUDIO_CNTRL);
22218e2988a7SThierry Reding 
22228e2988a7SThierry Reding 	/* enable advertising HBR capability */
22238e2988a7SThierry Reding 	tegra_sor_writel(sor, SOR_AUDIO_SPARE_HBR_ENABLE, SOR_AUDIO_SPARE);
22248e2988a7SThierry Reding 
22258e2988a7SThierry Reding 	tegra_sor_writel(sor, 0, SOR_HDMI_ACR_CTRL);
22268e2988a7SThierry Reding 
22278e2988a7SThierry Reding 	value = SOR_HDMI_SPARE_ACR_PRIORITY_HIGH |
22288e2988a7SThierry Reding 		SOR_HDMI_SPARE_CTS_RESET(1) |
22298e2988a7SThierry Reding 		SOR_HDMI_SPARE_HW_CTS_ENABLE;
22308e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_SPARE);
22318e2988a7SThierry Reding 
22328e2988a7SThierry Reding 	/* enable HW CTS */
22338e2988a7SThierry Reding 	value = SOR_HDMI_ACR_SUBPACK_LOW_SB1(0);
22348e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_ACR_0441_SUBPACK_LOW);
22358e2988a7SThierry Reding 
22368e2988a7SThierry Reding 	/* allow packet to be sent */
22378e2988a7SThierry Reding 	value = SOR_HDMI_ACR_SUBPACK_HIGH_ENABLE;
22388e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_ACR_0441_SUBPACK_HIGH);
22398e2988a7SThierry Reding 
22408e2988a7SThierry Reding 	/* reset N counter and enable lookup */
22418e2988a7SThierry Reding 	value = SOR_HDMI_AUDIO_N_RESET | SOR_HDMI_AUDIO_N_LOOKUP;
22428e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_N);
22438e2988a7SThierry Reding 
2244fad7b806SThierry Reding 	value = (24000 * 4096) / (128 * sor->format.sample_rate / 1000);
22458e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_0320);
22468e2988a7SThierry Reding 	tegra_sor_writel(sor, 4096, SOR_AUDIO_NVAL_0320);
22478e2988a7SThierry Reding 
22488e2988a7SThierry Reding 	tegra_sor_writel(sor, 20000, SOR_AUDIO_AVAL_0441);
22498e2988a7SThierry Reding 	tegra_sor_writel(sor, 4704, SOR_AUDIO_NVAL_0441);
22508e2988a7SThierry Reding 
22518e2988a7SThierry Reding 	tegra_sor_writel(sor, 20000, SOR_AUDIO_AVAL_0882);
22528e2988a7SThierry Reding 	tegra_sor_writel(sor, 9408, SOR_AUDIO_NVAL_0882);
22538e2988a7SThierry Reding 
22548e2988a7SThierry Reding 	tegra_sor_writel(sor, 20000, SOR_AUDIO_AVAL_1764);
22558e2988a7SThierry Reding 	tegra_sor_writel(sor, 18816, SOR_AUDIO_NVAL_1764);
22568e2988a7SThierry Reding 
2257fad7b806SThierry Reding 	value = (24000 * 6144) / (128 * sor->format.sample_rate / 1000);
22588e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_0480);
22598e2988a7SThierry Reding 	tegra_sor_writel(sor, 6144, SOR_AUDIO_NVAL_0480);
22608e2988a7SThierry Reding 
2261fad7b806SThierry Reding 	value = (24000 * 12288) / (128 * sor->format.sample_rate / 1000);
22628e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_0960);
22638e2988a7SThierry Reding 	tegra_sor_writel(sor, 12288, SOR_AUDIO_NVAL_0960);
22648e2988a7SThierry Reding 
2265fad7b806SThierry Reding 	value = (24000 * 24576) / (128 * sor->format.sample_rate / 1000);
22668e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_1920);
22678e2988a7SThierry Reding 	tegra_sor_writel(sor, 24576, SOR_AUDIO_NVAL_1920);
22688e2988a7SThierry Reding 
22698e2988a7SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_N);
22708e2988a7SThierry Reding 	value &= ~SOR_HDMI_AUDIO_N_RESET;
22718e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_N);
22728e2988a7SThierry Reding 
22738e2988a7SThierry Reding 	tegra_sor_hdmi_enable_audio_infoframe(sor);
22748e2988a7SThierry Reding }
22758e2988a7SThierry Reding 
2276459cc2c6SThierry Reding static void tegra_sor_hdmi_disable_audio_infoframe(struct tegra_sor *sor)
2277459cc2c6SThierry Reding {
2278459cc2c6SThierry Reding 	u32 value;
2279459cc2c6SThierry Reding 
2280459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2281459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_ENABLE;
2282459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2283459cc2c6SThierry Reding }
2284459cc2c6SThierry Reding 
22858e2988a7SThierry Reding static void tegra_sor_hdmi_audio_disable(struct tegra_sor *sor)
22868e2988a7SThierry Reding {
22878e2988a7SThierry Reding 	tegra_sor_hdmi_disable_audio_infoframe(sor);
22888e2988a7SThierry Reding }
22898e2988a7SThierry Reding 
2290459cc2c6SThierry Reding static struct tegra_sor_hdmi_settings *
2291459cc2c6SThierry Reding tegra_sor_hdmi_find_settings(struct tegra_sor *sor, unsigned long frequency)
2292459cc2c6SThierry Reding {
2293459cc2c6SThierry Reding 	unsigned int i;
2294459cc2c6SThierry Reding 
2295459cc2c6SThierry Reding 	for (i = 0; i < sor->num_settings; i++)
2296459cc2c6SThierry Reding 		if (frequency <= sor->settings[i].frequency)
2297459cc2c6SThierry Reding 			return &sor->settings[i];
2298459cc2c6SThierry Reding 
2299459cc2c6SThierry Reding 	return NULL;
2300459cc2c6SThierry Reding }
2301459cc2c6SThierry Reding 
230236e90221SThierry Reding static void tegra_sor_hdmi_disable_scrambling(struct tegra_sor *sor)
230336e90221SThierry Reding {
230436e90221SThierry Reding 	u32 value;
230536e90221SThierry Reding 
230636e90221SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI2_CTRL);
230736e90221SThierry Reding 	value &= ~SOR_HDMI2_CTRL_CLOCK_MODE_DIV_BY_4;
230836e90221SThierry Reding 	value &= ~SOR_HDMI2_CTRL_SCRAMBLE;
230936e90221SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI2_CTRL);
231036e90221SThierry Reding }
231136e90221SThierry Reding 
231236e90221SThierry Reding static void tegra_sor_hdmi_scdc_disable(struct tegra_sor *sor)
231336e90221SThierry Reding {
231436e90221SThierry Reding 	struct i2c_adapter *ddc = sor->output.ddc;
231536e90221SThierry Reding 
231636e90221SThierry Reding 	drm_scdc_set_high_tmds_clock_ratio(ddc, false);
231736e90221SThierry Reding 	drm_scdc_set_scrambling(ddc, false);
231836e90221SThierry Reding 
231936e90221SThierry Reding 	tegra_sor_hdmi_disable_scrambling(sor);
232036e90221SThierry Reding }
232136e90221SThierry Reding 
232236e90221SThierry Reding static void tegra_sor_hdmi_scdc_stop(struct tegra_sor *sor)
232336e90221SThierry Reding {
232436e90221SThierry Reding 	if (sor->scdc_enabled) {
232536e90221SThierry Reding 		cancel_delayed_work_sync(&sor->scdc);
232636e90221SThierry Reding 		tegra_sor_hdmi_scdc_disable(sor);
232736e90221SThierry Reding 	}
232836e90221SThierry Reding }
232936e90221SThierry Reding 
233036e90221SThierry Reding static void tegra_sor_hdmi_enable_scrambling(struct tegra_sor *sor)
233136e90221SThierry Reding {
233236e90221SThierry Reding 	u32 value;
233336e90221SThierry Reding 
233436e90221SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI2_CTRL);
233536e90221SThierry Reding 	value |= SOR_HDMI2_CTRL_CLOCK_MODE_DIV_BY_4;
233636e90221SThierry Reding 	value |= SOR_HDMI2_CTRL_SCRAMBLE;
233736e90221SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI2_CTRL);
233836e90221SThierry Reding }
233936e90221SThierry Reding 
234036e90221SThierry Reding static void tegra_sor_hdmi_scdc_enable(struct tegra_sor *sor)
234136e90221SThierry Reding {
234236e90221SThierry Reding 	struct i2c_adapter *ddc = sor->output.ddc;
234336e90221SThierry Reding 
234436e90221SThierry Reding 	drm_scdc_set_high_tmds_clock_ratio(ddc, true);
234536e90221SThierry Reding 	drm_scdc_set_scrambling(ddc, true);
234636e90221SThierry Reding 
234736e90221SThierry Reding 	tegra_sor_hdmi_enable_scrambling(sor);
234836e90221SThierry Reding }
234936e90221SThierry Reding 
235036e90221SThierry Reding static void tegra_sor_hdmi_scdc_work(struct work_struct *work)
235136e90221SThierry Reding {
235236e90221SThierry Reding 	struct tegra_sor *sor = container_of(work, struct tegra_sor, scdc.work);
235336e90221SThierry Reding 	struct i2c_adapter *ddc = sor->output.ddc;
235436e90221SThierry Reding 
235536e90221SThierry Reding 	if (!drm_scdc_get_scrambling_status(ddc)) {
235636e90221SThierry Reding 		DRM_DEBUG_KMS("SCDC not scrambled\n");
235736e90221SThierry Reding 		tegra_sor_hdmi_scdc_enable(sor);
235836e90221SThierry Reding 	}
235936e90221SThierry Reding 
236036e90221SThierry Reding 	schedule_delayed_work(&sor->scdc, msecs_to_jiffies(5000));
236136e90221SThierry Reding }
236236e90221SThierry Reding 
236336e90221SThierry Reding static void tegra_sor_hdmi_scdc_start(struct tegra_sor *sor)
236436e90221SThierry Reding {
236536e90221SThierry Reding 	struct drm_scdc *scdc = &sor->output.connector.display_info.hdmi.scdc;
236636e90221SThierry Reding 	struct drm_display_mode *mode;
236736e90221SThierry Reding 
236836e90221SThierry Reding 	mode = &sor->output.encoder.crtc->state->adjusted_mode;
236936e90221SThierry Reding 
237036e90221SThierry Reding 	if (mode->clock >= 340000 && scdc->supported) {
237136e90221SThierry Reding 		schedule_delayed_work(&sor->scdc, msecs_to_jiffies(5000));
237236e90221SThierry Reding 		tegra_sor_hdmi_scdc_enable(sor);
237336e90221SThierry Reding 		sor->scdc_enabled = true;
237436e90221SThierry Reding 	}
237536e90221SThierry Reding }
237636e90221SThierry Reding 
2377459cc2c6SThierry Reding static void tegra_sor_hdmi_disable(struct drm_encoder *encoder)
2378459cc2c6SThierry Reding {
2379459cc2c6SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
2380459cc2c6SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
2381459cc2c6SThierry Reding 	struct tegra_sor *sor = to_sor(output);
2382459cc2c6SThierry Reding 	u32 value;
2383459cc2c6SThierry Reding 	int err;
2384459cc2c6SThierry Reding 
23858e2988a7SThierry Reding 	tegra_sor_audio_unprepare(sor);
238636e90221SThierry Reding 	tegra_sor_hdmi_scdc_stop(sor);
238736e90221SThierry Reding 
2388459cc2c6SThierry Reding 	err = tegra_sor_detach(sor);
2389459cc2c6SThierry Reding 	if (err < 0)
2390459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to detach SOR: %d\n", err);
2391459cc2c6SThierry Reding 
2392459cc2c6SThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE1);
2393459cc2c6SThierry Reding 	tegra_sor_update(sor);
2394459cc2c6SThierry Reding 
2395459cc2c6SThierry Reding 	/* disable display to SOR clock */
2396459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
2397c57997bcSThierry Reding 
2398c57997bcSThierry Reding 	if (!sor->soc->has_nvdisplay)
2399c57997bcSThierry Reding 		value &= ~(SOR1_TIMING_CYA | SOR_ENABLE(1));
2400c57997bcSThierry Reding 	else
2401c57997bcSThierry Reding 		value &= ~SOR_ENABLE(sor->index);
2402c57997bcSThierry Reding 
2403459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2404459cc2c6SThierry Reding 
2405459cc2c6SThierry Reding 	tegra_dc_commit(dc);
2406459cc2c6SThierry Reding 
2407459cc2c6SThierry Reding 	err = tegra_sor_power_down(sor);
2408459cc2c6SThierry Reding 	if (err < 0)
2409459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power down SOR: %d\n", err);
2410459cc2c6SThierry Reding 
2411c57997bcSThierry Reding 	err = tegra_io_pad_power_disable(sor->pad);
2412459cc2c6SThierry Reding 	if (err < 0)
2413c57997bcSThierry Reding 		dev_err(sor->dev, "failed to power off I/O pad: %d\n", err);
2414459cc2c6SThierry Reding 
2415aaff8bd2SThierry Reding 	pm_runtime_put(sor->dev);
2416459cc2c6SThierry Reding }
2417459cc2c6SThierry Reding 
2418459cc2c6SThierry Reding static void tegra_sor_hdmi_enable(struct drm_encoder *encoder)
2419459cc2c6SThierry Reding {
2420459cc2c6SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
2421459cc2c6SThierry Reding 	unsigned int h_ref_to_sync = 1, pulse_start, max_ac;
2422459cc2c6SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
2423459cc2c6SThierry Reding 	struct tegra_sor_hdmi_settings *settings;
2424459cc2c6SThierry Reding 	struct tegra_sor *sor = to_sor(output);
2425c31efa7aSThierry Reding 	struct tegra_sor_state *state;
2426459cc2c6SThierry Reding 	struct drm_display_mode *mode;
242736e90221SThierry Reding 	unsigned long rate, pclk;
242830b49435SThierry Reding 	unsigned int div, i;
2429459cc2c6SThierry Reding 	u32 value;
2430459cc2c6SThierry Reding 	int err;
2431459cc2c6SThierry Reding 
2432c31efa7aSThierry Reding 	state = to_sor_state(output->connector.state);
2433459cc2c6SThierry Reding 	mode = &encoder->crtc->state->adjusted_mode;
243436e90221SThierry Reding 	pclk = mode->clock * 1000;
2435459cc2c6SThierry Reding 
2436aaff8bd2SThierry Reding 	pm_runtime_get_sync(sor->dev);
2437459cc2c6SThierry Reding 
243825bb2cecSThierry Reding 	/* switch to safe parent clock */
243925bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
2440e1335e2fSThierry Reding 	if (err < 0) {
2441459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
2442e1335e2fSThierry Reding 		return;
2443e1335e2fSThierry Reding 	}
2444459cc2c6SThierry Reding 
2445459cc2c6SThierry Reding 	div = clk_get_rate(sor->clk) / 1000000 * 4;
2446459cc2c6SThierry Reding 
2447c57997bcSThierry Reding 	err = tegra_io_pad_power_enable(sor->pad);
2448459cc2c6SThierry Reding 	if (err < 0)
2449c57997bcSThierry Reding 		dev_err(sor->dev, "failed to power on I/O pad: %d\n", err);
2450459cc2c6SThierry Reding 
2451459cc2c6SThierry Reding 	usleep_range(20, 100);
2452459cc2c6SThierry Reding 
2453880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
2454459cc2c6SThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
2455880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
2456459cc2c6SThierry Reding 
2457459cc2c6SThierry Reding 	usleep_range(20, 100);
2458459cc2c6SThierry Reding 
2459880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll3);
2460459cc2c6SThierry Reding 	value &= ~SOR_PLL3_PLL_VDD_MODE_3V3;
2461880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll3);
2462459cc2c6SThierry Reding 
2463880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
2464459cc2c6SThierry Reding 	value &= ~SOR_PLL0_VCOPD;
2465459cc2c6SThierry Reding 	value &= ~SOR_PLL0_PWR;
2466880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
2467459cc2c6SThierry Reding 
2468880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
2469459cc2c6SThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
2470880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
2471459cc2c6SThierry Reding 
2472459cc2c6SThierry Reding 	usleep_range(200, 400);
2473459cc2c6SThierry Reding 
2474880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
2475459cc2c6SThierry Reding 	value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
2476459cc2c6SThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
2477880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
2478459cc2c6SThierry Reding 
2479459cc2c6SThierry Reding 	usleep_range(20, 100);
2480459cc2c6SThierry Reding 
2481880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2482459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
2483459cc2c6SThierry Reding 		 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2;
2484880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2485459cc2c6SThierry Reding 
2486459cc2c6SThierry Reding 	while (true) {
2487459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2488459cc2c6SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_STATE_BUSY) == 0)
2489459cc2c6SThierry Reding 			break;
2490459cc2c6SThierry Reding 
2491459cc2c6SThierry Reding 		usleep_range(250, 1000);
2492459cc2c6SThierry Reding 	}
2493459cc2c6SThierry Reding 
2494459cc2c6SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
2495459cc2c6SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_UP | SOR_LANE_SEQ_CTL_DELAY(5);
2496459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
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_TRIGGER) == 0)
2501459cc2c6SThierry Reding 			break;
2502459cc2c6SThierry Reding 
2503459cc2c6SThierry Reding 		usleep_range(250, 1000);
2504459cc2c6SThierry Reding 	}
2505459cc2c6SThierry Reding 
2506459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
2507459cc2c6SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
2508459cc2c6SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
2509459cc2c6SThierry Reding 
251036e90221SThierry Reding 	if (mode->clock < 340000) {
251136e90221SThierry Reding 		DRM_DEBUG_KMS("setting 2.7 GHz link speed\n");
2512459cc2c6SThierry Reding 		value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G2_70;
251336e90221SThierry Reding 	} else {
251436e90221SThierry Reding 		DRM_DEBUG_KMS("setting 5.4 GHz link speed\n");
2515459cc2c6SThierry Reding 		value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G5_40;
251636e90221SThierry Reding 	}
2517459cc2c6SThierry Reding 
2518459cc2c6SThierry Reding 	value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
2519459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
2520459cc2c6SThierry Reding 
2521c57997bcSThierry Reding 	/* SOR pad PLL stabilization time */
2522c57997bcSThierry Reding 	usleep_range(250, 1000);
2523c57997bcSThierry Reding 
2524c57997bcSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
2525c57997bcSThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
2526c57997bcSThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(4);
2527c57997bcSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
2528c57997bcSThierry Reding 
2529459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
2530c57997bcSThierry Reding 	value &= ~SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
2531459cc2c6SThierry Reding 	value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
2532c57997bcSThierry Reding 	value &= ~SOR_DP_SPARE_SEQ_ENABLE;
2533c57997bcSThierry Reding 	value &= ~SOR_DP_SPARE_MACRO_SOR_CLK;
2534459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
2535459cc2c6SThierry Reding 
2536459cc2c6SThierry Reding 	value = SOR_SEQ_CTL_PU_PC(0) | SOR_SEQ_CTL_PU_PC_ALT(0) |
2537459cc2c6SThierry Reding 		SOR_SEQ_CTL_PD_PC(8) | SOR_SEQ_CTL_PD_PC_ALT(8);
2538459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_CTL);
2539459cc2c6SThierry Reding 
2540459cc2c6SThierry Reding 	value = SOR_SEQ_INST_DRIVE_PWM_OUT_LO | SOR_SEQ_INST_HALT |
2541459cc2c6SThierry Reding 		SOR_SEQ_INST_WAIT_VSYNC | SOR_SEQ_INST_WAIT(1);
2542459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_INST(0));
2543459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_INST(8));
2544459cc2c6SThierry Reding 
2545c57997bcSThierry Reding 	if (!sor->soc->has_nvdisplay) {
2546459cc2c6SThierry Reding 		/* program the reference clock */
2547459cc2c6SThierry Reding 		value = SOR_REFCLK_DIV_INT(div) | SOR_REFCLK_DIV_FRAC(div);
2548459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, SOR_REFCLK);
2549c57997bcSThierry Reding 	}
2550459cc2c6SThierry Reding 
255130b49435SThierry Reding 	/* XXX not in TRM */
255230b49435SThierry Reding 	for (value = 0, i = 0; i < 5; i++)
25536d6c815dSThierry Reding 		value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->xbar_cfg[i]) |
255430b49435SThierry Reding 			 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
2555459cc2c6SThierry Reding 
2556459cc2c6SThierry Reding 	tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
255730b49435SThierry Reding 	tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
2558459cc2c6SThierry Reding 
255925bb2cecSThierry Reding 	/* switch to parent clock */
2560e1335e2fSThierry Reding 	err = clk_set_parent(sor->clk, sor->clk_parent);
2561e1335e2fSThierry Reding 	if (err < 0) {
2562459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to set parent clock: %d\n", err);
2563e1335e2fSThierry Reding 		return;
2564e1335e2fSThierry Reding 	}
2565e1335e2fSThierry Reding 
2566e1335e2fSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_pad);
2567e1335e2fSThierry Reding 	if (err < 0) {
2568e1335e2fSThierry Reding 		dev_err(sor->dev, "failed to set pad clock: %d\n", err);
2569e1335e2fSThierry Reding 		return;
2570e1335e2fSThierry Reding 	}
2571459cc2c6SThierry Reding 
257236e90221SThierry Reding 	/* adjust clock rate for HDMI 2.0 modes */
257336e90221SThierry Reding 	rate = clk_get_rate(sor->clk_parent);
257436e90221SThierry Reding 
257536e90221SThierry Reding 	if (mode->clock >= 340000)
257636e90221SThierry Reding 		rate /= 2;
257736e90221SThierry Reding 
257836e90221SThierry Reding 	DRM_DEBUG_KMS("setting clock to %lu Hz, mode: %lu Hz\n", rate, pclk);
257936e90221SThierry Reding 
258036e90221SThierry Reding 	clk_set_rate(sor->clk, rate);
2581c57997bcSThierry Reding 
2582c57997bcSThierry Reding 	if (!sor->soc->has_nvdisplay) {
2583459cc2c6SThierry Reding 		value = SOR_INPUT_CONTROL_HDMI_SRC_SELECT(dc->pipe);
2584459cc2c6SThierry Reding 
2585459cc2c6SThierry Reding 		/* XXX is this the proper check? */
2586459cc2c6SThierry Reding 		if (mode->clock < 75000)
2587459cc2c6SThierry Reding 			value |= SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_LIMITED;
2588459cc2c6SThierry Reding 
2589459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, SOR_INPUT_CONTROL);
2590c57997bcSThierry Reding 	}
2591459cc2c6SThierry Reding 
2592459cc2c6SThierry Reding 	max_ac = ((mode->htotal - mode->hdisplay) - SOR_REKEY - 18) / 32;
2593459cc2c6SThierry Reding 
2594459cc2c6SThierry Reding 	value = SOR_HDMI_CTRL_ENABLE | SOR_HDMI_CTRL_MAX_AC_PACKET(max_ac) |
2595459cc2c6SThierry Reding 		SOR_HDMI_CTRL_AUDIO_LAYOUT | SOR_HDMI_CTRL_REKEY(SOR_REKEY);
2596459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_CTRL);
2597459cc2c6SThierry Reding 
2598c57997bcSThierry Reding 	if (!dc->soc->has_nvdisplay) {
2599459cc2c6SThierry Reding 		/* H_PULSE2 setup */
2600c57997bcSThierry Reding 		pulse_start = h_ref_to_sync +
2601c57997bcSThierry Reding 			      (mode->hsync_end - mode->hsync_start) +
2602459cc2c6SThierry Reding 			      (mode->htotal - mode->hsync_end) - 10;
2603459cc2c6SThierry Reding 
2604459cc2c6SThierry Reding 		value = PULSE_LAST_END_A | PULSE_QUAL_VACTIVE |
2605459cc2c6SThierry Reding 			PULSE_POLARITY_HIGH | PULSE_MODE_NORMAL;
2606459cc2c6SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_CONTROL);
2607459cc2c6SThierry Reding 
2608459cc2c6SThierry Reding 		value = PULSE_END(pulse_start + 8) | PULSE_START(pulse_start);
2609459cc2c6SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_POSITION_A);
2610459cc2c6SThierry Reding 
2611459cc2c6SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_DISP_SIGNAL_OPTIONS0);
2612459cc2c6SThierry Reding 		value |= H_PULSE2_ENABLE;
2613459cc2c6SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_SIGNAL_OPTIONS0);
2614c57997bcSThierry Reding 	}
2615459cc2c6SThierry Reding 
2616459cc2c6SThierry Reding 	/* infoframe setup */
2617459cc2c6SThierry Reding 	err = tegra_sor_hdmi_setup_avi_infoframe(sor, mode);
2618459cc2c6SThierry Reding 	if (err < 0)
2619459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
2620459cc2c6SThierry Reding 
2621459cc2c6SThierry Reding 	/* XXX HDMI audio support not implemented yet */
2622459cc2c6SThierry Reding 	tegra_sor_hdmi_disable_audio_infoframe(sor);
2623459cc2c6SThierry Reding 
2624459cc2c6SThierry Reding 	/* use single TMDS protocol */
2625459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
2626459cc2c6SThierry Reding 	value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
2627459cc2c6SThierry Reding 	value |= SOR_STATE_ASY_PROTOCOL_SINGLE_TMDS_A;
2628459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
2629459cc2c6SThierry Reding 
2630459cc2c6SThierry Reding 	/* power up pad calibration */
2631880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2632459cc2c6SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
2633880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2634459cc2c6SThierry Reding 
2635459cc2c6SThierry Reding 	/* production settings */
2636459cc2c6SThierry Reding 	settings = tegra_sor_hdmi_find_settings(sor, mode->clock * 1000);
2637db8b42fbSDan Carpenter 	if (!settings) {
2638db8b42fbSDan Carpenter 		dev_err(sor->dev, "no settings for pixel clock %d Hz\n",
2639db8b42fbSDan Carpenter 			mode->clock * 1000);
2640459cc2c6SThierry Reding 		return;
2641459cc2c6SThierry Reding 	}
2642459cc2c6SThierry Reding 
2643880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
2644459cc2c6SThierry Reding 	value &= ~SOR_PLL0_ICHPMP_MASK;
2645c57997bcSThierry Reding 	value &= ~SOR_PLL0_FILTER_MASK;
2646459cc2c6SThierry Reding 	value &= ~SOR_PLL0_VCOCAP_MASK;
2647459cc2c6SThierry Reding 	value |= SOR_PLL0_ICHPMP(settings->ichpmp);
2648c57997bcSThierry Reding 	value |= SOR_PLL0_FILTER(settings->filter);
2649459cc2c6SThierry Reding 	value |= SOR_PLL0_VCOCAP(settings->vcocap);
2650880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
2651459cc2c6SThierry Reding 
2652c57997bcSThierry Reding 	/* XXX not in TRM */
2653880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll1);
2654459cc2c6SThierry Reding 	value &= ~SOR_PLL1_LOADADJ_MASK;
2655c57997bcSThierry Reding 	value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
2656459cc2c6SThierry Reding 	value |= SOR_PLL1_LOADADJ(settings->loadadj);
2657c57997bcSThierry Reding 	value |= SOR_PLL1_TMDS_TERMADJ(settings->tmds_termadj);
2658c57997bcSThierry Reding 	value |= SOR_PLL1_TMDS_TERM;
2659880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll1);
2660459cc2c6SThierry Reding 
2661880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll3);
2662c57997bcSThierry Reding 	value &= ~SOR_PLL3_BG_TEMP_COEF_MASK;
2663459cc2c6SThierry Reding 	value &= ~SOR_PLL3_BG_VREF_LEVEL_MASK;
2664c57997bcSThierry Reding 	value &= ~SOR_PLL3_AVDD10_LEVEL_MASK;
2665c57997bcSThierry Reding 	value &= ~SOR_PLL3_AVDD14_LEVEL_MASK;
2666c57997bcSThierry Reding 	value |= SOR_PLL3_BG_TEMP_COEF(settings->bg_temp_coef);
2667c57997bcSThierry Reding 	value |= SOR_PLL3_BG_VREF_LEVEL(settings->bg_vref_level);
2668c57997bcSThierry Reding 	value |= SOR_PLL3_AVDD10_LEVEL(settings->avdd10_level);
2669c57997bcSThierry Reding 	value |= SOR_PLL3_AVDD14_LEVEL(settings->avdd14_level);
2670880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll3);
2671459cc2c6SThierry Reding 
2672c57997bcSThierry Reding 	value = settings->drive_current[3] << 24 |
2673c57997bcSThierry Reding 		settings->drive_current[2] << 16 |
2674c57997bcSThierry Reding 		settings->drive_current[1] <<  8 |
2675c57997bcSThierry Reding 		settings->drive_current[0] <<  0;
2676459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
2677459cc2c6SThierry Reding 
2678c57997bcSThierry Reding 	value = settings->preemphasis[3] << 24 |
2679c57997bcSThierry Reding 		settings->preemphasis[2] << 16 |
2680c57997bcSThierry Reding 		settings->preemphasis[1] <<  8 |
2681c57997bcSThierry Reding 		settings->preemphasis[0] <<  0;
2682459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
2683459cc2c6SThierry Reding 
2684880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2685459cc2c6SThierry Reding 	value &= ~SOR_DP_PADCTL_TX_PU_MASK;
2686459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU_ENABLE;
2687c57997bcSThierry Reding 	value |= SOR_DP_PADCTL_TX_PU(settings->tx_pu_value);
2688880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2689459cc2c6SThierry Reding 
2690c57997bcSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl2);
2691c57997bcSThierry Reding 	value &= ~SOR_DP_PADCTL_SPAREPLL_MASK;
2692c57997bcSThierry Reding 	value |= SOR_DP_PADCTL_SPAREPLL(settings->sparepll);
2693c57997bcSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl2);
2694c57997bcSThierry Reding 
2695459cc2c6SThierry Reding 	/* power down pad calibration */
2696880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2697459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
2698880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2699459cc2c6SThierry Reding 
2700c57997bcSThierry Reding 	if (!dc->soc->has_nvdisplay) {
2701459cc2c6SThierry Reding 		/* miscellaneous display controller settings */
2702459cc2c6SThierry Reding 		value = VSYNC_H_POSITION(1);
2703459cc2c6SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_TIMING_OPTIONS);
2704c57997bcSThierry Reding 	}
2705459cc2c6SThierry Reding 
2706459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_COLOR_CONTROL);
2707459cc2c6SThierry Reding 	value &= ~DITHER_CONTROL_MASK;
2708459cc2c6SThierry Reding 	value &= ~BASE_COLOR_SIZE_MASK;
2709459cc2c6SThierry Reding 
2710c31efa7aSThierry Reding 	switch (state->bpc) {
2711459cc2c6SThierry Reding 	case 6:
2712459cc2c6SThierry Reding 		value |= BASE_COLOR_SIZE_666;
2713459cc2c6SThierry Reding 		break;
2714459cc2c6SThierry Reding 
2715459cc2c6SThierry Reding 	case 8:
2716459cc2c6SThierry Reding 		value |= BASE_COLOR_SIZE_888;
2717459cc2c6SThierry Reding 		break;
2718459cc2c6SThierry Reding 
2719c57997bcSThierry Reding 	case 10:
2720c57997bcSThierry Reding 		value |= BASE_COLOR_SIZE_101010;
2721c57997bcSThierry Reding 		break;
2722c57997bcSThierry Reding 
2723c57997bcSThierry Reding 	case 12:
2724c57997bcSThierry Reding 		value |= BASE_COLOR_SIZE_121212;
2725c57997bcSThierry Reding 		break;
2726c57997bcSThierry Reding 
2727459cc2c6SThierry Reding 	default:
2728c31efa7aSThierry Reding 		WARN(1, "%u bits-per-color not supported\n", state->bpc);
2729c31efa7aSThierry Reding 		value |= BASE_COLOR_SIZE_888;
2730459cc2c6SThierry Reding 		break;
2731459cc2c6SThierry Reding 	}
2732459cc2c6SThierry Reding 
2733459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_COLOR_CONTROL);
2734459cc2c6SThierry Reding 
2735c57997bcSThierry Reding 	/* XXX set display head owner */
2736c57997bcSThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
2737c57997bcSThierry Reding 	value &= ~SOR_STATE_ASY_OWNER_MASK;
2738c57997bcSThierry Reding 	value |= SOR_STATE_ASY_OWNER(1 + dc->pipe);
2739c57997bcSThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
2740c57997bcSThierry Reding 
2741459cc2c6SThierry Reding 	err = tegra_sor_power_up(sor, 250);
2742459cc2c6SThierry Reding 	if (err < 0)
2743459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
2744459cc2c6SThierry Reding 
27452bd1dd39SThierry Reding 	/* configure dynamic range of output */
2746880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
2747459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_RANGECOMPRESS_MASK;
2748459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_DYNRANGE_MASK;
2749880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
2750459cc2c6SThierry Reding 
27512bd1dd39SThierry Reding 	/* configure colorspace */
2752880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
2753459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_COLORSPACE_MASK;
2754459cc2c6SThierry Reding 	value |= SOR_HEAD_STATE_COLORSPACE_RGB;
2755880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
2756459cc2c6SThierry Reding 
2757c31efa7aSThierry Reding 	tegra_sor_mode_set(sor, mode, state);
2758459cc2c6SThierry Reding 
2759459cc2c6SThierry Reding 	tegra_sor_update(sor);
2760459cc2c6SThierry Reding 
2761c57997bcSThierry Reding 	/* program preamble timing in SOR (XXX) */
2762c57997bcSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
2763c57997bcSThierry Reding 	value &= ~SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
2764c57997bcSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
2765c57997bcSThierry Reding 
2766459cc2c6SThierry Reding 	err = tegra_sor_attach(sor);
2767459cc2c6SThierry Reding 	if (err < 0)
2768459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to attach SOR: %d\n", err);
2769459cc2c6SThierry Reding 
2770459cc2c6SThierry Reding 	/* enable display to SOR clock and generate HDMI preamble */
2771459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
2772c57997bcSThierry Reding 
2773c57997bcSThierry Reding 	if (!sor->soc->has_nvdisplay)
2774c57997bcSThierry Reding 		value |= SOR_ENABLE(1) | SOR1_TIMING_CYA;
2775c57997bcSThierry Reding 	else
2776c57997bcSThierry Reding 		value |= SOR_ENABLE(sor->index);
2777c57997bcSThierry Reding 
2778459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2779459cc2c6SThierry Reding 
2780c57997bcSThierry Reding 	if (dc->soc->has_nvdisplay) {
2781c57997bcSThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_CORE_SOR_SET_CONTROL(sor->index));
2782c57997bcSThierry Reding 		value &= ~PROTOCOL_MASK;
2783c57997bcSThierry Reding 		value |= PROTOCOL_SINGLE_TMDS_A;
2784c57997bcSThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_CORE_SOR_SET_CONTROL(sor->index));
2785c57997bcSThierry Reding 	}
2786c57997bcSThierry Reding 
2787459cc2c6SThierry Reding 	tegra_dc_commit(dc);
2788459cc2c6SThierry Reding 
2789459cc2c6SThierry Reding 	err = tegra_sor_wakeup(sor);
2790459cc2c6SThierry Reding 	if (err < 0)
2791459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
279236e90221SThierry Reding 
279336e90221SThierry Reding 	tegra_sor_hdmi_scdc_start(sor);
27948e2988a7SThierry Reding 	tegra_sor_audio_prepare(sor);
2795459cc2c6SThierry Reding }
2796459cc2c6SThierry Reding 
2797459cc2c6SThierry Reding static const struct drm_encoder_helper_funcs tegra_sor_hdmi_helpers = {
2798459cc2c6SThierry Reding 	.disable = tegra_sor_hdmi_disable,
2799459cc2c6SThierry Reding 	.enable = tegra_sor_hdmi_enable,
2800459cc2c6SThierry Reding 	.atomic_check = tegra_sor_encoder_atomic_check,
2801459cc2c6SThierry Reding };
2802459cc2c6SThierry Reding 
28036b6b6042SThierry Reding static int tegra_sor_init(struct host1x_client *client)
28046b6b6042SThierry Reding {
28059910f5c4SThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->parent);
2806459cc2c6SThierry Reding 	const struct drm_encoder_helper_funcs *helpers = NULL;
28076b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
2808459cc2c6SThierry Reding 	int connector = DRM_MODE_CONNECTOR_Unknown;
2809459cc2c6SThierry Reding 	int encoder = DRM_MODE_ENCODER_NONE;
28108e2988a7SThierry Reding 	u32 value;
28116b6b6042SThierry Reding 	int err;
28126b6b6042SThierry Reding 
28139542c237SThierry Reding 	if (!sor->aux) {
2814459cc2c6SThierry Reding 		if (sor->soc->supports_hdmi) {
2815459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_HDMIA;
2816459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
2817459cc2c6SThierry Reding 			helpers = &tegra_sor_hdmi_helpers;
2818459cc2c6SThierry Reding 		} else if (sor->soc->supports_lvds) {
2819459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_LVDS;
2820459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_LVDS;
2821459cc2c6SThierry Reding 		}
2822459cc2c6SThierry Reding 	} else {
2823459cc2c6SThierry Reding 		if (sor->soc->supports_edp) {
2824459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_eDP;
2825459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
2826459cc2c6SThierry Reding 			helpers = &tegra_sor_edp_helpers;
2827459cc2c6SThierry Reding 		} else if (sor->soc->supports_dp) {
2828459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_DisplayPort;
2829459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
2830459cc2c6SThierry Reding 		}
2831459cc2c6SThierry Reding 	}
28326b6b6042SThierry Reding 
28336b6b6042SThierry Reding 	sor->output.dev = sor->dev;
28346b6b6042SThierry Reding 
28356fad8f66SThierry Reding 	drm_connector_init(drm, &sor->output.connector,
28366fad8f66SThierry Reding 			   &tegra_sor_connector_funcs,
2837459cc2c6SThierry Reding 			   connector);
28386fad8f66SThierry Reding 	drm_connector_helper_add(&sor->output.connector,
28396fad8f66SThierry Reding 				 &tegra_sor_connector_helper_funcs);
28406fad8f66SThierry Reding 	sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
28416fad8f66SThierry Reding 
28426fad8f66SThierry Reding 	drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
284313a3d91fSVille Syrjälä 			 encoder, NULL);
2844459cc2c6SThierry Reding 	drm_encoder_helper_add(&sor->output.encoder, helpers);
28456fad8f66SThierry Reding 
2846cde4c44dSDaniel Vetter 	drm_connector_attach_encoder(&sor->output.connector,
28476fad8f66SThierry Reding 					  &sor->output.encoder);
28486fad8f66SThierry Reding 	drm_connector_register(&sor->output.connector);
28496fad8f66SThierry Reding 
2850ea130b24SThierry Reding 	err = tegra_output_init(drm, &sor->output);
2851ea130b24SThierry Reding 	if (err < 0) {
2852ea130b24SThierry Reding 		dev_err(client->dev, "failed to initialize output: %d\n", err);
2853ea130b24SThierry Reding 		return err;
2854ea130b24SThierry Reding 	}
28556fad8f66SThierry Reding 
2856c57997bcSThierry Reding 	tegra_output_find_possible_crtcs(&sor->output, drm);
28576b6b6042SThierry Reding 
28589542c237SThierry Reding 	if (sor->aux) {
28599542c237SThierry Reding 		err = drm_dp_aux_attach(sor->aux, &sor->output);
28606b6b6042SThierry Reding 		if (err < 0) {
28616b6b6042SThierry Reding 			dev_err(sor->dev, "failed to attach DP: %d\n", err);
28626b6b6042SThierry Reding 			return err;
28636b6b6042SThierry Reding 		}
28646b6b6042SThierry Reding 	}
28656b6b6042SThierry Reding 
2866535a65dbSTomeu Vizoso 	/*
2867535a65dbSTomeu Vizoso 	 * XXX: Remove this reset once proper hand-over from firmware to
2868535a65dbSTomeu Vizoso 	 * kernel is possible.
2869535a65dbSTomeu Vizoso 	 */
2870f8c79120SJon Hunter 	if (sor->rst) {
287111c632e1SThierry Reding 		err = reset_control_acquire(sor->rst);
287211c632e1SThierry Reding 		if (err < 0) {
287311c632e1SThierry Reding 			dev_err(sor->dev, "failed to acquire SOR reset: %d\n",
287411c632e1SThierry Reding 				err);
287511c632e1SThierry Reding 			return err;
287611c632e1SThierry Reding 		}
287711c632e1SThierry Reding 
2878535a65dbSTomeu Vizoso 		err = reset_control_assert(sor->rst);
2879535a65dbSTomeu Vizoso 		if (err < 0) {
2880f8c79120SJon Hunter 			dev_err(sor->dev, "failed to assert SOR reset: %d\n",
2881f8c79120SJon Hunter 				err);
2882535a65dbSTomeu Vizoso 			return err;
2883535a65dbSTomeu Vizoso 		}
2884f8c79120SJon Hunter 	}
2885535a65dbSTomeu Vizoso 
28866fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk);
28876fad8f66SThierry Reding 	if (err < 0) {
28886fad8f66SThierry Reding 		dev_err(sor->dev, "failed to enable clock: %d\n", err);
28896fad8f66SThierry Reding 		return err;
28906fad8f66SThierry Reding 	}
28916fad8f66SThierry Reding 
2892535a65dbSTomeu Vizoso 	usleep_range(1000, 3000);
2893535a65dbSTomeu Vizoso 
2894f8c79120SJon Hunter 	if (sor->rst) {
2895535a65dbSTomeu Vizoso 		err = reset_control_deassert(sor->rst);
2896535a65dbSTomeu Vizoso 		if (err < 0) {
2897f8c79120SJon Hunter 			dev_err(sor->dev, "failed to deassert SOR reset: %d\n",
2898f8c79120SJon Hunter 				err);
2899535a65dbSTomeu Vizoso 			return err;
2900535a65dbSTomeu Vizoso 		}
290111c632e1SThierry Reding 
290211c632e1SThierry Reding 		reset_control_release(sor->rst);
2903f8c79120SJon Hunter 	}
2904535a65dbSTomeu Vizoso 
29056fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk_safe);
29066fad8f66SThierry Reding 	if (err < 0)
29076fad8f66SThierry Reding 		return err;
29086fad8f66SThierry Reding 
29096fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk_dp);
29106fad8f66SThierry Reding 	if (err < 0)
29116fad8f66SThierry Reding 		return err;
29126fad8f66SThierry Reding 
29138e2988a7SThierry Reding 	/*
29148e2988a7SThierry Reding 	 * Enable and unmask the HDA codec SCRATCH0 register interrupt. This
29158e2988a7SThierry Reding 	 * is used for interoperability between the HDA codec driver and the
29168e2988a7SThierry Reding 	 * HDMI/DP driver.
29178e2988a7SThierry Reding 	 */
29188e2988a7SThierry Reding 	value = SOR_INT_CODEC_SCRATCH1 | SOR_INT_CODEC_SCRATCH0;
29198e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_INT_ENABLE);
29208e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_INT_MASK);
29218e2988a7SThierry Reding 
29226b6b6042SThierry Reding 	return 0;
29236b6b6042SThierry Reding }
29246b6b6042SThierry Reding 
29256b6b6042SThierry Reding static int tegra_sor_exit(struct host1x_client *client)
29266b6b6042SThierry Reding {
29276b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
29286b6b6042SThierry Reding 	int err;
29296b6b6042SThierry Reding 
29308e2988a7SThierry Reding 	tegra_sor_writel(sor, 0, SOR_INT_MASK);
29318e2988a7SThierry Reding 	tegra_sor_writel(sor, 0, SOR_INT_ENABLE);
29328e2988a7SThierry Reding 
2933328ec69eSThierry Reding 	tegra_output_exit(&sor->output);
2934328ec69eSThierry Reding 
29359542c237SThierry Reding 	if (sor->aux) {
29369542c237SThierry Reding 		err = drm_dp_aux_detach(sor->aux);
29376b6b6042SThierry Reding 		if (err < 0) {
29386b6b6042SThierry Reding 			dev_err(sor->dev, "failed to detach DP: %d\n", err);
29396b6b6042SThierry Reding 			return err;
29406b6b6042SThierry Reding 		}
29416b6b6042SThierry Reding 	}
29426b6b6042SThierry Reding 
29436fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk_safe);
29446fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk_dp);
29456fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk);
29466fad8f66SThierry Reding 
29476b6b6042SThierry Reding 	return 0;
29486b6b6042SThierry Reding }
29496b6b6042SThierry Reding 
29506b6b6042SThierry Reding static const struct host1x_client_ops sor_client_ops = {
29516b6b6042SThierry Reding 	.init = tegra_sor_init,
29526b6b6042SThierry Reding 	.exit = tegra_sor_exit,
29536b6b6042SThierry Reding };
29546b6b6042SThierry Reding 
2955459cc2c6SThierry Reding static const struct tegra_sor_ops tegra_sor_edp_ops = {
2956459cc2c6SThierry Reding 	.name = "eDP",
2957459cc2c6SThierry Reding };
2958459cc2c6SThierry Reding 
2959459cc2c6SThierry Reding static int tegra_sor_hdmi_probe(struct tegra_sor *sor)
2960459cc2c6SThierry Reding {
2961459cc2c6SThierry Reding 	int err;
2962459cc2c6SThierry Reding 
2963459cc2c6SThierry Reding 	sor->avdd_io_supply = devm_regulator_get(sor->dev, "avdd-io");
2964459cc2c6SThierry Reding 	if (IS_ERR(sor->avdd_io_supply)) {
2965459cc2c6SThierry Reding 		dev_err(sor->dev, "cannot get AVDD I/O supply: %ld\n",
2966459cc2c6SThierry Reding 			PTR_ERR(sor->avdd_io_supply));
2967459cc2c6SThierry Reding 		return PTR_ERR(sor->avdd_io_supply);
2968459cc2c6SThierry Reding 	}
2969459cc2c6SThierry Reding 
2970459cc2c6SThierry Reding 	err = regulator_enable(sor->avdd_io_supply);
2971459cc2c6SThierry Reding 	if (err < 0) {
2972459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable AVDD I/O supply: %d\n",
2973459cc2c6SThierry Reding 			err);
2974459cc2c6SThierry Reding 		return err;
2975459cc2c6SThierry Reding 	}
2976459cc2c6SThierry Reding 
2977459cc2c6SThierry Reding 	sor->vdd_pll_supply = devm_regulator_get(sor->dev, "vdd-pll");
2978459cc2c6SThierry Reding 	if (IS_ERR(sor->vdd_pll_supply)) {
2979459cc2c6SThierry Reding 		dev_err(sor->dev, "cannot get VDD PLL supply: %ld\n",
2980459cc2c6SThierry Reding 			PTR_ERR(sor->vdd_pll_supply));
2981459cc2c6SThierry Reding 		return PTR_ERR(sor->vdd_pll_supply);
2982459cc2c6SThierry Reding 	}
2983459cc2c6SThierry Reding 
2984459cc2c6SThierry Reding 	err = regulator_enable(sor->vdd_pll_supply);
2985459cc2c6SThierry Reding 	if (err < 0) {
2986459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable VDD PLL supply: %d\n",
2987459cc2c6SThierry Reding 			err);
2988459cc2c6SThierry Reding 		return err;
2989459cc2c6SThierry Reding 	}
2990459cc2c6SThierry Reding 
2991459cc2c6SThierry Reding 	sor->hdmi_supply = devm_regulator_get(sor->dev, "hdmi");
2992459cc2c6SThierry Reding 	if (IS_ERR(sor->hdmi_supply)) {
2993459cc2c6SThierry Reding 		dev_err(sor->dev, "cannot get HDMI supply: %ld\n",
2994459cc2c6SThierry Reding 			PTR_ERR(sor->hdmi_supply));
2995459cc2c6SThierry Reding 		return PTR_ERR(sor->hdmi_supply);
2996459cc2c6SThierry Reding 	}
2997459cc2c6SThierry Reding 
2998459cc2c6SThierry Reding 	err = regulator_enable(sor->hdmi_supply);
2999459cc2c6SThierry Reding 	if (err < 0) {
3000459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable HDMI supply: %d\n", err);
3001459cc2c6SThierry Reding 		return err;
3002459cc2c6SThierry Reding 	}
3003459cc2c6SThierry Reding 
300436e90221SThierry Reding 	INIT_DELAYED_WORK(&sor->scdc, tegra_sor_hdmi_scdc_work);
300536e90221SThierry Reding 
3006459cc2c6SThierry Reding 	return 0;
3007459cc2c6SThierry Reding }
3008459cc2c6SThierry Reding 
3009459cc2c6SThierry Reding static int tegra_sor_hdmi_remove(struct tegra_sor *sor)
3010459cc2c6SThierry Reding {
3011459cc2c6SThierry Reding 	regulator_disable(sor->hdmi_supply);
3012459cc2c6SThierry Reding 	regulator_disable(sor->vdd_pll_supply);
3013459cc2c6SThierry Reding 	regulator_disable(sor->avdd_io_supply);
3014459cc2c6SThierry Reding 
3015459cc2c6SThierry Reding 	return 0;
3016459cc2c6SThierry Reding }
3017459cc2c6SThierry Reding 
3018459cc2c6SThierry Reding static const struct tegra_sor_ops tegra_sor_hdmi_ops = {
3019459cc2c6SThierry Reding 	.name = "HDMI",
3020459cc2c6SThierry Reding 	.probe = tegra_sor_hdmi_probe,
3021459cc2c6SThierry Reding 	.remove = tegra_sor_hdmi_remove,
3022459cc2c6SThierry Reding };
3023459cc2c6SThierry Reding 
302430b49435SThierry Reding static const u8 tegra124_sor_xbar_cfg[5] = {
302530b49435SThierry Reding 	0, 1, 2, 3, 4
302630b49435SThierry Reding };
302730b49435SThierry Reding 
3028880cee0bSThierry Reding static const struct tegra_sor_regs tegra124_sor_regs = {
3029880cee0bSThierry Reding 	.head_state0 = 0x05,
3030880cee0bSThierry Reding 	.head_state1 = 0x07,
3031880cee0bSThierry Reding 	.head_state2 = 0x09,
3032880cee0bSThierry Reding 	.head_state3 = 0x0b,
3033880cee0bSThierry Reding 	.head_state4 = 0x0d,
3034880cee0bSThierry Reding 	.head_state5 = 0x0f,
3035880cee0bSThierry Reding 	.pll0 = 0x17,
3036880cee0bSThierry Reding 	.pll1 = 0x18,
3037880cee0bSThierry Reding 	.pll2 = 0x19,
3038880cee0bSThierry Reding 	.pll3 = 0x1a,
3039880cee0bSThierry Reding 	.dp_padctl0 = 0x5c,
3040880cee0bSThierry Reding 	.dp_padctl2 = 0x73,
3041880cee0bSThierry Reding };
3042880cee0bSThierry Reding 
3043459cc2c6SThierry Reding static const struct tegra_sor_soc tegra124_sor = {
3044459cc2c6SThierry Reding 	.supports_edp = true,
3045459cc2c6SThierry Reding 	.supports_lvds = true,
3046459cc2c6SThierry Reding 	.supports_hdmi = false,
3047459cc2c6SThierry Reding 	.supports_dp = false,
3048880cee0bSThierry Reding 	.regs = &tegra124_sor_regs,
3049c57997bcSThierry Reding 	.has_nvdisplay = false,
305030b49435SThierry Reding 	.xbar_cfg = tegra124_sor_xbar_cfg,
3051459cc2c6SThierry Reding };
3052459cc2c6SThierry Reding 
3053880cee0bSThierry Reding static const struct tegra_sor_regs tegra210_sor_regs = {
3054880cee0bSThierry Reding 	.head_state0 = 0x05,
3055880cee0bSThierry Reding 	.head_state1 = 0x07,
3056880cee0bSThierry Reding 	.head_state2 = 0x09,
3057880cee0bSThierry Reding 	.head_state3 = 0x0b,
3058880cee0bSThierry Reding 	.head_state4 = 0x0d,
3059880cee0bSThierry Reding 	.head_state5 = 0x0f,
3060880cee0bSThierry Reding 	.pll0 = 0x17,
3061880cee0bSThierry Reding 	.pll1 = 0x18,
3062880cee0bSThierry Reding 	.pll2 = 0x19,
3063880cee0bSThierry Reding 	.pll3 = 0x1a,
3064880cee0bSThierry Reding 	.dp_padctl0 = 0x5c,
3065880cee0bSThierry Reding 	.dp_padctl2 = 0x73,
3066880cee0bSThierry Reding };
3067880cee0bSThierry Reding 
3068459cc2c6SThierry Reding static const struct tegra_sor_soc tegra210_sor = {
3069459cc2c6SThierry Reding 	.supports_edp = true,
3070459cc2c6SThierry Reding 	.supports_lvds = false,
3071459cc2c6SThierry Reding 	.supports_hdmi = false,
3072459cc2c6SThierry Reding 	.supports_dp = false,
3073880cee0bSThierry Reding 	.regs = &tegra210_sor_regs,
3074c57997bcSThierry Reding 	.has_nvdisplay = false,
307530b49435SThierry Reding 	.xbar_cfg = tegra124_sor_xbar_cfg,
307630b49435SThierry Reding };
307730b49435SThierry Reding 
307830b49435SThierry Reding static const u8 tegra210_sor_xbar_cfg[5] = {
307930b49435SThierry Reding 	2, 1, 0, 3, 4
3080459cc2c6SThierry Reding };
3081459cc2c6SThierry Reding 
3082459cc2c6SThierry Reding static const struct tegra_sor_soc tegra210_sor1 = {
3083459cc2c6SThierry Reding 	.supports_edp = false,
3084459cc2c6SThierry Reding 	.supports_lvds = false,
3085459cc2c6SThierry Reding 	.supports_hdmi = true,
3086459cc2c6SThierry Reding 	.supports_dp = true,
3087459cc2c6SThierry Reding 
3088880cee0bSThierry Reding 	.regs = &tegra210_sor_regs,
3089c57997bcSThierry Reding 	.has_nvdisplay = false,
3090880cee0bSThierry Reding 
3091459cc2c6SThierry Reding 	.num_settings = ARRAY_SIZE(tegra210_sor_hdmi_defaults),
3092459cc2c6SThierry Reding 	.settings = tegra210_sor_hdmi_defaults,
309330b49435SThierry Reding 
309430b49435SThierry Reding 	.xbar_cfg = tegra210_sor_xbar_cfg,
3095459cc2c6SThierry Reding };
3096459cc2c6SThierry Reding 
3097c57997bcSThierry Reding static const struct tegra_sor_regs tegra186_sor_regs = {
3098c57997bcSThierry Reding 	.head_state0 = 0x151,
3099c57997bcSThierry Reding 	.head_state1 = 0x154,
3100c57997bcSThierry Reding 	.head_state2 = 0x157,
3101c57997bcSThierry Reding 	.head_state3 = 0x15a,
3102c57997bcSThierry Reding 	.head_state4 = 0x15d,
3103c57997bcSThierry Reding 	.head_state5 = 0x160,
3104c57997bcSThierry Reding 	.pll0 = 0x163,
3105c57997bcSThierry Reding 	.pll1 = 0x164,
3106c57997bcSThierry Reding 	.pll2 = 0x165,
3107c57997bcSThierry Reding 	.pll3 = 0x166,
3108c57997bcSThierry Reding 	.dp_padctl0 = 0x168,
3109c57997bcSThierry Reding 	.dp_padctl2 = 0x16a,
3110c57997bcSThierry Reding };
3111c57997bcSThierry Reding 
3112c57997bcSThierry Reding static const struct tegra_sor_soc tegra186_sor = {
3113c57997bcSThierry Reding 	.supports_edp = false,
3114c57997bcSThierry Reding 	.supports_lvds = false,
3115c57997bcSThierry Reding 	.supports_hdmi = false,
3116c57997bcSThierry Reding 	.supports_dp = true,
3117c57997bcSThierry Reding 
3118c57997bcSThierry Reding 	.regs = &tegra186_sor_regs,
3119c57997bcSThierry Reding 	.has_nvdisplay = true,
3120c57997bcSThierry Reding 
3121c57997bcSThierry Reding 	.xbar_cfg = tegra124_sor_xbar_cfg,
3122c57997bcSThierry Reding };
3123c57997bcSThierry Reding 
3124c57997bcSThierry Reding static const struct tegra_sor_soc tegra186_sor1 = {
3125c57997bcSThierry Reding 	.supports_edp = false,
3126c57997bcSThierry Reding 	.supports_lvds = false,
3127c57997bcSThierry Reding 	.supports_hdmi = true,
3128c57997bcSThierry Reding 	.supports_dp = true,
3129c57997bcSThierry Reding 
3130c57997bcSThierry Reding 	.regs = &tegra186_sor_regs,
3131c57997bcSThierry Reding 	.has_nvdisplay = true,
3132c57997bcSThierry Reding 
3133c57997bcSThierry Reding 	.num_settings = ARRAY_SIZE(tegra186_sor_hdmi_defaults),
3134c57997bcSThierry Reding 	.settings = tegra186_sor_hdmi_defaults,
3135c57997bcSThierry Reding 
3136c57997bcSThierry Reding 	.xbar_cfg = tegra124_sor_xbar_cfg,
3137c57997bcSThierry Reding };
3138c57997bcSThierry Reding 
31399b6c14b8SThierry Reding static const struct tegra_sor_regs tegra194_sor_regs = {
31409b6c14b8SThierry Reding 	.head_state0 = 0x151,
31419b6c14b8SThierry Reding 	.head_state1 = 0x155,
31429b6c14b8SThierry Reding 	.head_state2 = 0x159,
31439b6c14b8SThierry Reding 	.head_state3 = 0x15d,
31449b6c14b8SThierry Reding 	.head_state4 = 0x161,
31459b6c14b8SThierry Reding 	.head_state5 = 0x165,
31469b6c14b8SThierry Reding 	.pll0 = 0x169,
31479b6c14b8SThierry Reding 	.pll1 = 0x16a,
31489b6c14b8SThierry Reding 	.pll2 = 0x16b,
31499b6c14b8SThierry Reding 	.pll3 = 0x16c,
31509b6c14b8SThierry Reding 	.dp_padctl0 = 0x16e,
31519b6c14b8SThierry Reding 	.dp_padctl2 = 0x16f,
31529b6c14b8SThierry Reding };
31539b6c14b8SThierry Reding 
31549b6c14b8SThierry Reding static const struct tegra_sor_soc tegra194_sor = {
31559b6c14b8SThierry Reding 	.supports_edp = true,
31569b6c14b8SThierry Reding 	.supports_lvds = false,
31579b6c14b8SThierry Reding 	.supports_hdmi = true,
31589b6c14b8SThierry Reding 	.supports_dp = true,
31599b6c14b8SThierry Reding 
31609b6c14b8SThierry Reding 	.regs = &tegra194_sor_regs,
31619b6c14b8SThierry Reding 	.has_nvdisplay = true,
31629b6c14b8SThierry Reding 
31639b6c14b8SThierry Reding 	.num_settings = ARRAY_SIZE(tegra194_sor_hdmi_defaults),
31649b6c14b8SThierry Reding 	.settings = tegra194_sor_hdmi_defaults,
31659b6c14b8SThierry Reding 
31669b6c14b8SThierry Reding 	.xbar_cfg = tegra210_sor_xbar_cfg,
31679b6c14b8SThierry Reding };
31689b6c14b8SThierry Reding 
3169459cc2c6SThierry Reding static const struct of_device_id tegra_sor_of_match[] = {
31709b6c14b8SThierry Reding 	{ .compatible = "nvidia,tegra194-sor", .data = &tegra194_sor },
3171c57997bcSThierry Reding 	{ .compatible = "nvidia,tegra186-sor1", .data = &tegra186_sor1 },
3172c57997bcSThierry Reding 	{ .compatible = "nvidia,tegra186-sor", .data = &tegra186_sor },
3173459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra210-sor1", .data = &tegra210_sor1 },
3174459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra210-sor", .data = &tegra210_sor },
3175459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra124-sor", .data = &tegra124_sor },
3176459cc2c6SThierry Reding 	{ },
3177459cc2c6SThierry Reding };
3178459cc2c6SThierry Reding MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
3179459cc2c6SThierry Reding 
3180c57997bcSThierry Reding static int tegra_sor_parse_dt(struct tegra_sor *sor)
3181c57997bcSThierry Reding {
3182c57997bcSThierry Reding 	struct device_node *np = sor->dev->of_node;
31836d6c815dSThierry Reding 	u32 xbar_cfg[5];
31846d6c815dSThierry Reding 	unsigned int i;
3185c57997bcSThierry Reding 	u32 value;
3186c57997bcSThierry Reding 	int err;
3187c57997bcSThierry Reding 
3188c57997bcSThierry Reding 	if (sor->soc->has_nvdisplay) {
3189c57997bcSThierry Reding 		err = of_property_read_u32(np, "nvidia,interface", &value);
3190c57997bcSThierry Reding 		if (err < 0)
3191c57997bcSThierry Reding 			return err;
3192c57997bcSThierry Reding 
3193c57997bcSThierry Reding 		sor->index = value;
3194c57997bcSThierry Reding 
3195c57997bcSThierry Reding 		/*
3196c57997bcSThierry Reding 		 * override the default that we already set for Tegra210 and
3197c57997bcSThierry Reding 		 * earlier
3198c57997bcSThierry Reding 		 */
3199c57997bcSThierry Reding 		sor->pad = TEGRA_IO_PAD_HDMI_DP0 + sor->index;
3200c57997bcSThierry Reding 	}
3201c57997bcSThierry Reding 
32026d6c815dSThierry Reding 	err = of_property_read_u32_array(np, "nvidia,xbar-cfg", xbar_cfg, 5);
32036d6c815dSThierry Reding 	if (err < 0) {
32046d6c815dSThierry Reding 		/* fall back to default per-SoC XBAR configuration */
32056d6c815dSThierry Reding 		for (i = 0; i < 5; i++)
32066d6c815dSThierry Reding 			sor->xbar_cfg[i] = sor->soc->xbar_cfg[i];
32076d6c815dSThierry Reding 	} else {
32086d6c815dSThierry Reding 		/* copy cells to SOR XBAR configuration */
32096d6c815dSThierry Reding 		for (i = 0; i < 5; i++)
32106d6c815dSThierry Reding 			sor->xbar_cfg[i] = xbar_cfg[i];
3211c57997bcSThierry Reding 	}
3212c57997bcSThierry Reding 
32136b6b6042SThierry Reding 	return 0;
32148e2988a7SThierry Reding }
32158e2988a7SThierry Reding 
32168e2988a7SThierry Reding static irqreturn_t tegra_sor_irq(int irq, void *data)
32178e2988a7SThierry Reding {
32188e2988a7SThierry Reding 	struct tegra_sor *sor = data;
32198e2988a7SThierry Reding 	u32 value;
32208e2988a7SThierry Reding 
32218e2988a7SThierry Reding 	value = tegra_sor_readl(sor, SOR_INT_STATUS);
32228e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_INT_STATUS);
32238e2988a7SThierry Reding 
32248e2988a7SThierry Reding 	if (value & SOR_INT_CODEC_SCRATCH0) {
32258e2988a7SThierry Reding 		value = tegra_sor_readl(sor, SOR_AUDIO_HDA_CODEC_SCRATCH0);
32268e2988a7SThierry Reding 
32278e2988a7SThierry Reding 		if (value & SOR_AUDIO_HDA_CODEC_SCRATCH0_VALID) {
3228cd54fb96SThierry Reding 			unsigned int format;
32298e2988a7SThierry Reding 
32308e2988a7SThierry Reding 			format = value & SOR_AUDIO_HDA_CODEC_SCRATCH0_FMT_MASK;
32318e2988a7SThierry Reding 
3232fad7b806SThierry Reding 			tegra_hda_parse_format(format, &sor->format);
32338e2988a7SThierry Reding 
32348e2988a7SThierry Reding 			tegra_sor_hdmi_audio_enable(sor);
32358e2988a7SThierry Reding 		} else {
32368e2988a7SThierry Reding 			tegra_sor_hdmi_audio_disable(sor);
32378e2988a7SThierry Reding 		}
32388e2988a7SThierry Reding 	}
32398e2988a7SThierry Reding 
32408e2988a7SThierry Reding 	return IRQ_HANDLED;
32418e2988a7SThierry Reding }
32428e2988a7SThierry Reding 
32436b6b6042SThierry Reding static int tegra_sor_probe(struct platform_device *pdev)
32446b6b6042SThierry Reding {
32456b6b6042SThierry Reding 	struct device_node *np;
32466b6b6042SThierry Reding 	struct tegra_sor *sor;
32476b6b6042SThierry Reding 	struct resource *regs;
32486b6b6042SThierry Reding 	int err;
32496b6b6042SThierry Reding 
32506b6b6042SThierry Reding 	sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
32516b6b6042SThierry Reding 	if (!sor)
32526b6b6042SThierry Reding 		return -ENOMEM;
32536b6b6042SThierry Reding 
32545faea3d0SThierry Reding 	sor->soc = of_device_get_match_data(&pdev->dev);
32556b6b6042SThierry Reding 	sor->output.dev = sor->dev = &pdev->dev;
3256459cc2c6SThierry Reding 
3257459cc2c6SThierry Reding 	sor->settings = devm_kmemdup(&pdev->dev, sor->soc->settings,
3258459cc2c6SThierry Reding 				     sor->soc->num_settings *
3259459cc2c6SThierry Reding 					sizeof(*sor->settings),
3260459cc2c6SThierry Reding 				     GFP_KERNEL);
3261459cc2c6SThierry Reding 	if (!sor->settings)
3262459cc2c6SThierry Reding 		return -ENOMEM;
3263459cc2c6SThierry Reding 
3264459cc2c6SThierry Reding 	sor->num_settings = sor->soc->num_settings;
32656b6b6042SThierry Reding 
32666b6b6042SThierry Reding 	np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
32676b6b6042SThierry Reding 	if (np) {
32689542c237SThierry Reding 		sor->aux = drm_dp_aux_find_by_of_node(np);
32696b6b6042SThierry Reding 		of_node_put(np);
32706b6b6042SThierry Reding 
32719542c237SThierry Reding 		if (!sor->aux)
32726b6b6042SThierry Reding 			return -EPROBE_DEFER;
32736b6b6042SThierry Reding 	}
32746b6b6042SThierry Reding 
32759542c237SThierry Reding 	if (!sor->aux) {
3276459cc2c6SThierry Reding 		if (sor->soc->supports_hdmi) {
3277459cc2c6SThierry Reding 			sor->ops = &tegra_sor_hdmi_ops;
3278c57997bcSThierry Reding 			sor->pad = TEGRA_IO_PAD_HDMI;
3279459cc2c6SThierry Reding 		} else if (sor->soc->supports_lvds) {
3280459cc2c6SThierry Reding 			dev_err(&pdev->dev, "LVDS not supported yet\n");
3281459cc2c6SThierry Reding 			return -ENODEV;
3282459cc2c6SThierry Reding 		} else {
3283459cc2c6SThierry Reding 			dev_err(&pdev->dev, "unknown (non-DP) support\n");
3284459cc2c6SThierry Reding 			return -ENODEV;
3285459cc2c6SThierry Reding 		}
3286459cc2c6SThierry Reding 	} else {
3287459cc2c6SThierry Reding 		if (sor->soc->supports_edp) {
3288459cc2c6SThierry Reding 			sor->ops = &tegra_sor_edp_ops;
3289c57997bcSThierry Reding 			sor->pad = TEGRA_IO_PAD_LVDS;
3290459cc2c6SThierry Reding 		} else if (sor->soc->supports_dp) {
3291459cc2c6SThierry Reding 			dev_err(&pdev->dev, "DisplayPort not supported yet\n");
3292459cc2c6SThierry Reding 			return -ENODEV;
3293459cc2c6SThierry Reding 		} else {
3294459cc2c6SThierry Reding 			dev_err(&pdev->dev, "unknown (DP) support\n");
3295459cc2c6SThierry Reding 			return -ENODEV;
3296459cc2c6SThierry Reding 		}
3297459cc2c6SThierry Reding 	}
3298459cc2c6SThierry Reding 
3299c57997bcSThierry Reding 	err = tegra_sor_parse_dt(sor);
3300c57997bcSThierry Reding 	if (err < 0)
3301c57997bcSThierry Reding 		return err;
3302c57997bcSThierry Reding 
33036b6b6042SThierry Reding 	err = tegra_output_probe(&sor->output);
33044dbdc740SThierry Reding 	if (err < 0) {
33054dbdc740SThierry Reding 		dev_err(&pdev->dev, "failed to probe output: %d\n", err);
33066b6b6042SThierry Reding 		return err;
33074dbdc740SThierry Reding 	}
33086b6b6042SThierry Reding 
3309459cc2c6SThierry Reding 	if (sor->ops && sor->ops->probe) {
3310459cc2c6SThierry Reding 		err = sor->ops->probe(sor);
3311459cc2c6SThierry Reding 		if (err < 0) {
3312459cc2c6SThierry Reding 			dev_err(&pdev->dev, "failed to probe %s: %d\n",
3313459cc2c6SThierry Reding 				sor->ops->name, err);
3314459cc2c6SThierry Reding 			goto output;
3315459cc2c6SThierry Reding 		}
3316459cc2c6SThierry Reding 	}
3317459cc2c6SThierry Reding 
33186b6b6042SThierry Reding 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
33196b6b6042SThierry Reding 	sor->regs = devm_ioremap_resource(&pdev->dev, regs);
3320459cc2c6SThierry Reding 	if (IS_ERR(sor->regs)) {
3321459cc2c6SThierry Reding 		err = PTR_ERR(sor->regs);
3322459cc2c6SThierry Reding 		goto remove;
3323459cc2c6SThierry Reding 	}
33246b6b6042SThierry Reding 
33258e2988a7SThierry Reding 	err = platform_get_irq(pdev, 0);
33268e2988a7SThierry Reding 	if (err < 0) {
33278e2988a7SThierry Reding 		dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
33288e2988a7SThierry Reding 		goto remove;
33298e2988a7SThierry Reding 	}
33308e2988a7SThierry Reding 
33318e2988a7SThierry Reding 	sor->irq = err;
33328e2988a7SThierry Reding 
33338e2988a7SThierry Reding 	err = devm_request_irq(sor->dev, sor->irq, tegra_sor_irq, 0,
33348e2988a7SThierry Reding 			       dev_name(sor->dev), sor);
33358e2988a7SThierry Reding 	if (err < 0) {
33368e2988a7SThierry Reding 		dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
33378e2988a7SThierry Reding 		goto remove;
33388e2988a7SThierry Reding 	}
33398e2988a7SThierry Reding 
334011c632e1SThierry Reding 	sor->rst = devm_reset_control_get_exclusive_released(&pdev->dev, "sor");
33414dbdc740SThierry Reding 	if (IS_ERR(sor->rst)) {
3342459cc2c6SThierry Reding 		err = PTR_ERR(sor->rst);
3343180b46ecSThierry Reding 
3344180b46ecSThierry Reding 		if (err != -EBUSY || WARN_ON(!pdev->dev.pm_domain)) {
3345f8c79120SJon Hunter 			dev_err(&pdev->dev, "failed to get reset control: %d\n",
3346f8c79120SJon Hunter 				err);
3347459cc2c6SThierry Reding 			goto remove;
33484dbdc740SThierry Reding 		}
3349180b46ecSThierry Reding 
3350180b46ecSThierry Reding 		/*
3351180b46ecSThierry Reding 		 * At this point, the reset control is most likely being used
3352180b46ecSThierry Reding 		 * by the generic power domain implementation. With any luck
3353180b46ecSThierry Reding 		 * the power domain will have taken care of resetting the SOR
3354180b46ecSThierry Reding 		 * and we don't have to do anything.
3355180b46ecSThierry Reding 		 */
3356180b46ecSThierry Reding 		sor->rst = NULL;
3357f8c79120SJon Hunter 	}
33586b6b6042SThierry Reding 
33596b6b6042SThierry Reding 	sor->clk = devm_clk_get(&pdev->dev, NULL);
33604dbdc740SThierry Reding 	if (IS_ERR(sor->clk)) {
3361459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk);
3362459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get module clock: %d\n", err);
3363459cc2c6SThierry Reding 		goto remove;
33644dbdc740SThierry Reding 	}
33656b6b6042SThierry Reding 
3366618dee39SThierry Reding 	if (sor->soc->supports_hdmi || sor->soc->supports_dp) {
3367e1335e2fSThierry Reding 		struct device_node *np = pdev->dev.of_node;
3368e1335e2fSThierry Reding 		const char *name;
3369e1335e2fSThierry Reding 
3370e1335e2fSThierry Reding 		/*
3371e1335e2fSThierry Reding 		 * For backwards compatibility with Tegra210 device trees,
3372e1335e2fSThierry Reding 		 * fall back to the old clock name "source" if the new "out"
3373e1335e2fSThierry Reding 		 * clock is not available.
3374e1335e2fSThierry Reding 		 */
3375e1335e2fSThierry Reding 		if (of_property_match_string(np, "clock-names", "out") < 0)
3376e1335e2fSThierry Reding 			name = "source";
3377e1335e2fSThierry Reding 		else
3378e1335e2fSThierry Reding 			name = "out";
3379e1335e2fSThierry Reding 
3380e1335e2fSThierry Reding 		sor->clk_out = devm_clk_get(&pdev->dev, name);
3381e1335e2fSThierry Reding 		if (IS_ERR(sor->clk_out)) {
3382e1335e2fSThierry Reding 			err = PTR_ERR(sor->clk_out);
3383e1335e2fSThierry Reding 			dev_err(sor->dev, "failed to get %s clock: %d\n",
3384e1335e2fSThierry Reding 				name, err);
3385618dee39SThierry Reding 			goto remove;
3386618dee39SThierry Reding 		}
33871087fac1SThierry Reding 	} else {
3388d780537fSThierry Reding 		/* fall back to the module clock on SOR0 (eDP/LVDS only) */
33891087fac1SThierry Reding 		sor->clk_out = sor->clk;
3390618dee39SThierry Reding 	}
3391618dee39SThierry Reding 
33926b6b6042SThierry Reding 	sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
33934dbdc740SThierry Reding 	if (IS_ERR(sor->clk_parent)) {
3394459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_parent);
3395459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get parent clock: %d\n", err);
3396459cc2c6SThierry Reding 		goto remove;
33974dbdc740SThierry Reding 	}
33986b6b6042SThierry Reding 
33996b6b6042SThierry Reding 	sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
34004dbdc740SThierry Reding 	if (IS_ERR(sor->clk_safe)) {
3401459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_safe);
3402459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get safe clock: %d\n", err);
3403459cc2c6SThierry Reding 		goto remove;
34044dbdc740SThierry Reding 	}
34056b6b6042SThierry Reding 
34066b6b6042SThierry Reding 	sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
34074dbdc740SThierry Reding 	if (IS_ERR(sor->clk_dp)) {
3408459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_dp);
3409459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get DP clock: %d\n", err);
3410459cc2c6SThierry Reding 		goto remove;
34114dbdc740SThierry Reding 	}
34126b6b6042SThierry Reding 
3413e1335e2fSThierry Reding 	/*
3414e1335e2fSThierry Reding 	 * Starting with Tegra186, the BPMP provides an implementation for
3415e1335e2fSThierry Reding 	 * the pad output clock, so we have to look it up from device tree.
3416e1335e2fSThierry Reding 	 */
3417e1335e2fSThierry Reding 	sor->clk_pad = devm_clk_get(&pdev->dev, "pad");
3418e1335e2fSThierry Reding 	if (IS_ERR(sor->clk_pad)) {
3419e1335e2fSThierry Reding 		if (sor->clk_pad != ERR_PTR(-ENOENT)) {
3420e1335e2fSThierry Reding 			err = PTR_ERR(sor->clk_pad);
3421e1335e2fSThierry Reding 			goto remove;
3422e1335e2fSThierry Reding 		}
3423e1335e2fSThierry Reding 
3424e1335e2fSThierry Reding 		/*
3425e1335e2fSThierry Reding 		 * If the pad output clock is not available, then we assume
3426e1335e2fSThierry Reding 		 * we're on Tegra210 or earlier and have to provide our own
3427e1335e2fSThierry Reding 		 * implementation.
3428e1335e2fSThierry Reding 		 */
3429e1335e2fSThierry Reding 		sor->clk_pad = NULL;
3430e1335e2fSThierry Reding 	}
3431e1335e2fSThierry Reding 
3432e1335e2fSThierry Reding 	/*
3433e1335e2fSThierry Reding 	 * The bootloader may have set up the SOR such that it's module clock
3434e1335e2fSThierry Reding 	 * is sourced by one of the display PLLs. However, that doesn't work
3435e1335e2fSThierry Reding 	 * without properly having set up other bits of the SOR.
3436e1335e2fSThierry Reding 	 */
3437e1335e2fSThierry Reding 	err = clk_set_parent(sor->clk_out, sor->clk_safe);
3438e1335e2fSThierry Reding 	if (err < 0) {
3439e1335e2fSThierry Reding 		dev_err(&pdev->dev, "failed to use safe clock: %d\n", err);
3440e1335e2fSThierry Reding 		goto remove;
3441e1335e2fSThierry Reding 	}
3442e1335e2fSThierry Reding 
3443aaff8bd2SThierry Reding 	platform_set_drvdata(pdev, sor);
3444aaff8bd2SThierry Reding 	pm_runtime_enable(&pdev->dev);
3445aaff8bd2SThierry Reding 
3446e1335e2fSThierry Reding 	/*
3447e1335e2fSThierry Reding 	 * On Tegra210 and earlier, provide our own implementation for the
3448e1335e2fSThierry Reding 	 * pad output clock.
3449e1335e2fSThierry Reding 	 */
3450e1335e2fSThierry Reding 	if (!sor->clk_pad) {
3451e1335e2fSThierry Reding 		err = pm_runtime_get_sync(&pdev->dev);
3452e1335e2fSThierry Reding 		if (err < 0) {
3453e1335e2fSThierry Reding 			dev_err(&pdev->dev, "failed to get runtime PM: %d\n",
3454e1335e2fSThierry Reding 				err);
3455e1335e2fSThierry Reding 			goto remove;
3456e1335e2fSThierry Reding 		}
3457b299221cSThierry Reding 
3458e1335e2fSThierry Reding 		sor->clk_pad = tegra_clk_sor_pad_register(sor,
3459e1335e2fSThierry Reding 							  "sor1_pad_clkout");
3460e1335e2fSThierry Reding 		pm_runtime_put(&pdev->dev);
3461e1335e2fSThierry Reding 	}
3462e1335e2fSThierry Reding 
3463e1335e2fSThierry Reding 	if (IS_ERR(sor->clk_pad)) {
3464e1335e2fSThierry Reding 		err = PTR_ERR(sor->clk_pad);
3465e1335e2fSThierry Reding 		dev_err(&pdev->dev, "failed to register SOR pad clock: %d\n",
3466e1335e2fSThierry Reding 			err);
3467b299221cSThierry Reding 		goto remove;
3468b299221cSThierry Reding 	}
3469b299221cSThierry Reding 
34706b6b6042SThierry Reding 	INIT_LIST_HEAD(&sor->client.list);
34716b6b6042SThierry Reding 	sor->client.ops = &sor_client_ops;
34726b6b6042SThierry Reding 	sor->client.dev = &pdev->dev;
34736b6b6042SThierry Reding 
34746b6b6042SThierry Reding 	err = host1x_client_register(&sor->client);
34756b6b6042SThierry Reding 	if (err < 0) {
34766b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
34776b6b6042SThierry Reding 			err);
3478459cc2c6SThierry Reding 		goto remove;
34796b6b6042SThierry Reding 	}
34806b6b6042SThierry Reding 
34816b6b6042SThierry Reding 	return 0;
3482459cc2c6SThierry Reding 
3483459cc2c6SThierry Reding remove:
3484459cc2c6SThierry Reding 	if (sor->ops && sor->ops->remove)
3485459cc2c6SThierry Reding 		sor->ops->remove(sor);
3486459cc2c6SThierry Reding output:
3487459cc2c6SThierry Reding 	tegra_output_remove(&sor->output);
3488459cc2c6SThierry Reding 	return err;
34896b6b6042SThierry Reding }
34906b6b6042SThierry Reding 
34916b6b6042SThierry Reding static int tegra_sor_remove(struct platform_device *pdev)
34926b6b6042SThierry Reding {
34936b6b6042SThierry Reding 	struct tegra_sor *sor = platform_get_drvdata(pdev);
34946b6b6042SThierry Reding 	int err;
34956b6b6042SThierry Reding 
3496aaff8bd2SThierry Reding 	pm_runtime_disable(&pdev->dev);
3497aaff8bd2SThierry Reding 
34986b6b6042SThierry Reding 	err = host1x_client_unregister(&sor->client);
34996b6b6042SThierry Reding 	if (err < 0) {
35006b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
35016b6b6042SThierry Reding 			err);
35026b6b6042SThierry Reding 		return err;
35036b6b6042SThierry Reding 	}
35046b6b6042SThierry Reding 
3505459cc2c6SThierry Reding 	if (sor->ops && sor->ops->remove) {
3506459cc2c6SThierry Reding 		err = sor->ops->remove(sor);
3507459cc2c6SThierry Reding 		if (err < 0)
3508459cc2c6SThierry Reding 			dev_err(&pdev->dev, "failed to remove SOR: %d\n", err);
3509459cc2c6SThierry Reding 	}
3510459cc2c6SThierry Reding 
3511328ec69eSThierry Reding 	tegra_output_remove(&sor->output);
35126b6b6042SThierry Reding 
35136b6b6042SThierry Reding 	return 0;
35146b6b6042SThierry Reding }
35156b6b6042SThierry Reding 
3516aaff8bd2SThierry Reding #ifdef CONFIG_PM
3517aaff8bd2SThierry Reding static int tegra_sor_suspend(struct device *dev)
3518aaff8bd2SThierry Reding {
3519aaff8bd2SThierry Reding 	struct tegra_sor *sor = dev_get_drvdata(dev);
3520aaff8bd2SThierry Reding 	int err;
3521aaff8bd2SThierry Reding 
3522f8c79120SJon Hunter 	if (sor->rst) {
3523aaff8bd2SThierry Reding 		err = reset_control_assert(sor->rst);
3524aaff8bd2SThierry Reding 		if (err < 0) {
3525aaff8bd2SThierry Reding 			dev_err(dev, "failed to assert reset: %d\n", err);
3526aaff8bd2SThierry Reding 			return err;
3527aaff8bd2SThierry Reding 		}
352811c632e1SThierry Reding 
352911c632e1SThierry Reding 		reset_control_release(sor->rst);
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) {
355311c632e1SThierry Reding 		err = reset_control_acquire(sor->rst);
355411c632e1SThierry Reding 		if (err < 0) {
355511c632e1SThierry Reding 			dev_err(dev, "failed to acquire reset: %d\n", err);
355611c632e1SThierry Reding 			clk_disable_unprepare(sor->clk);
355711c632e1SThierry Reding 			return err;
355811c632e1SThierry Reding 		}
355911c632e1SThierry Reding 
3560aaff8bd2SThierry Reding 		err = reset_control_deassert(sor->rst);
3561aaff8bd2SThierry Reding 		if (err < 0) {
3562aaff8bd2SThierry Reding 			dev_err(dev, "failed to deassert reset: %d\n", err);
356311c632e1SThierry Reding 			reset_control_release(sor->rst);
3564aaff8bd2SThierry Reding 			clk_disable_unprepare(sor->clk);
3565aaff8bd2SThierry Reding 			return err;
3566aaff8bd2SThierry Reding 		}
3567f8c79120SJon Hunter 	}
3568aaff8bd2SThierry Reding 
3569aaff8bd2SThierry Reding 	return 0;
3570aaff8bd2SThierry Reding }
3571aaff8bd2SThierry Reding #endif
3572aaff8bd2SThierry Reding 
3573aaff8bd2SThierry Reding static const struct dev_pm_ops tegra_sor_pm_ops = {
3574aaff8bd2SThierry Reding 	SET_RUNTIME_PM_OPS(tegra_sor_suspend, tegra_sor_resume, NULL)
3575aaff8bd2SThierry Reding };
3576aaff8bd2SThierry Reding 
35776b6b6042SThierry Reding struct platform_driver tegra_sor_driver = {
35786b6b6042SThierry Reding 	.driver = {
35796b6b6042SThierry Reding 		.name = "tegra-sor",
35806b6b6042SThierry Reding 		.of_match_table = tegra_sor_of_match,
3581aaff8bd2SThierry Reding 		.pm = &tegra_sor_pm_ops,
35826b6b6042SThierry Reding 	},
35836b6b6042SThierry Reding 	.probe = tegra_sor_probe,
35846b6b6042SThierry Reding 	.remove = tegra_sor_remove,
35856b6b6042SThierry Reding };
3586