xref: /openbmc/linux/drivers/gpu/drm/tegra/sor.c (revision 4d0e95e0e5679936b45848826aa080ced1ba40c8)
1d2912cb1SThomas 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>
11eb1df694SSam Ravnborg #include <linux/module.h>
12459cc2c6SThierry Reding #include <linux/of_device.h>
136b6b6042SThierry Reding #include <linux/platform_device.h>
14aaff8bd2SThierry Reding #include <linux/pm_runtime.h>
15459cc2c6SThierry Reding #include <linux/regulator/consumer.h>
166b6b6042SThierry Reding #include <linux/reset.h>
17306a7f91SThierry Reding 
187232398aSThierry Reding #include <soc/tegra/pmc.h>
196b6b6042SThierry Reding 
204aa3df71SThierry Reding #include <drm/drm_atomic_helper.h>
21eb1df694SSam Ravnborg #include <drm/drm_debugfs.h>
226b6b6042SThierry Reding #include <drm/drm_dp_helper.h>
23eb1df694SSam Ravnborg #include <drm/drm_file.h>
246fad8f66SThierry Reding #include <drm/drm_panel.h>
2536e90221SThierry Reding #include <drm/drm_scdc_helper.h>
26*4d0e95e0SThomas Zimmermann #include <drm/drm_simple_kms_helper.h>
276b6b6042SThierry Reding 
286b6b6042SThierry Reding #include "dc.h"
299a42c7c6SThierry Reding #include "dp.h"
306b6b6042SThierry Reding #include "drm.h"
31fad7b806SThierry Reding #include "hda.h"
326b6b6042SThierry Reding #include "sor.h"
33932f6529SThierry Reding #include "trace.h"
346b6b6042SThierry Reding 
35459cc2c6SThierry Reding #define SOR_REKEY 0x38
36459cc2c6SThierry Reding 
37459cc2c6SThierry Reding struct tegra_sor_hdmi_settings {
38459cc2c6SThierry Reding 	unsigned long frequency;
39459cc2c6SThierry Reding 
40459cc2c6SThierry Reding 	u8 vcocap;
41c57997bcSThierry Reding 	u8 filter;
42459cc2c6SThierry Reding 	u8 ichpmp;
43459cc2c6SThierry Reding 	u8 loadadj;
44c57997bcSThierry Reding 	u8 tmds_termadj;
45c57997bcSThierry Reding 	u8 tx_pu_value;
46c57997bcSThierry Reding 	u8 bg_temp_coef;
47c57997bcSThierry Reding 	u8 bg_vref_level;
48c57997bcSThierry Reding 	u8 avdd10_level;
49c57997bcSThierry Reding 	u8 avdd14_level;
50c57997bcSThierry Reding 	u8 sparepll;
51459cc2c6SThierry Reding 
52459cc2c6SThierry Reding 	u8 drive_current[4];
53459cc2c6SThierry Reding 	u8 preemphasis[4];
54459cc2c6SThierry Reding };
55459cc2c6SThierry Reding 
56459cc2c6SThierry Reding #if 1
57459cc2c6SThierry Reding static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
58459cc2c6SThierry Reding 	{
59459cc2c6SThierry Reding 		.frequency = 54000000,
60459cc2c6SThierry Reding 		.vcocap = 0x0,
61c57997bcSThierry Reding 		.filter = 0x0,
62459cc2c6SThierry Reding 		.ichpmp = 0x1,
63459cc2c6SThierry Reding 		.loadadj = 0x3,
64c57997bcSThierry Reding 		.tmds_termadj = 0x9,
65c57997bcSThierry Reding 		.tx_pu_value = 0x10,
66c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
67c57997bcSThierry Reding 		.bg_vref_level = 0x8,
68c57997bcSThierry Reding 		.avdd10_level = 0x4,
69c57997bcSThierry Reding 		.avdd14_level = 0x4,
70c57997bcSThierry Reding 		.sparepll = 0x0,
71459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
72459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
73459cc2c6SThierry Reding 	}, {
74459cc2c6SThierry Reding 		.frequency = 75000000,
75459cc2c6SThierry Reding 		.vcocap = 0x3,
76c57997bcSThierry Reding 		.filter = 0x0,
77459cc2c6SThierry Reding 		.ichpmp = 0x1,
78459cc2c6SThierry Reding 		.loadadj = 0x3,
79c57997bcSThierry Reding 		.tmds_termadj = 0x9,
80c57997bcSThierry Reding 		.tx_pu_value = 0x40,
81c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
82c57997bcSThierry Reding 		.bg_vref_level = 0x8,
83c57997bcSThierry Reding 		.avdd10_level = 0x4,
84c57997bcSThierry Reding 		.avdd14_level = 0x4,
85c57997bcSThierry Reding 		.sparepll = 0x0,
86459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
87459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
88459cc2c6SThierry Reding 	}, {
89459cc2c6SThierry Reding 		.frequency = 150000000,
90459cc2c6SThierry Reding 		.vcocap = 0x3,
91c57997bcSThierry Reding 		.filter = 0x0,
92459cc2c6SThierry Reding 		.ichpmp = 0x1,
93459cc2c6SThierry Reding 		.loadadj = 0x3,
94c57997bcSThierry Reding 		.tmds_termadj = 0x9,
95c57997bcSThierry Reding 		.tx_pu_value = 0x66,
96c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
97c57997bcSThierry Reding 		.bg_vref_level = 0x8,
98c57997bcSThierry Reding 		.avdd10_level = 0x4,
99c57997bcSThierry Reding 		.avdd14_level = 0x4,
100c57997bcSThierry Reding 		.sparepll = 0x0,
101459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
102459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
103459cc2c6SThierry Reding 	}, {
104459cc2c6SThierry Reding 		.frequency = 300000000,
105459cc2c6SThierry Reding 		.vcocap = 0x3,
106c57997bcSThierry Reding 		.filter = 0x0,
107459cc2c6SThierry Reding 		.ichpmp = 0x1,
108459cc2c6SThierry Reding 		.loadadj = 0x3,
109c57997bcSThierry Reding 		.tmds_termadj = 0x9,
110c57997bcSThierry Reding 		.tx_pu_value = 0x66,
111c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
112c57997bcSThierry Reding 		.bg_vref_level = 0xa,
113c57997bcSThierry Reding 		.avdd10_level = 0x4,
114c57997bcSThierry Reding 		.avdd14_level = 0x4,
115c57997bcSThierry Reding 		.sparepll = 0x0,
116459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
117459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x17, 0x17, 0x17 },
118459cc2c6SThierry Reding 	}, {
119459cc2c6SThierry Reding 		.frequency = 600000000,
120459cc2c6SThierry Reding 		.vcocap = 0x3,
121c57997bcSThierry Reding 		.filter = 0x0,
122459cc2c6SThierry Reding 		.ichpmp = 0x1,
123459cc2c6SThierry Reding 		.loadadj = 0x3,
124c57997bcSThierry Reding 		.tmds_termadj = 0x9,
125c57997bcSThierry Reding 		.tx_pu_value = 0x66,
126c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
127c57997bcSThierry Reding 		.bg_vref_level = 0x8,
128c57997bcSThierry Reding 		.avdd10_level = 0x4,
129c57997bcSThierry Reding 		.avdd14_level = 0x4,
130c57997bcSThierry Reding 		.sparepll = 0x0,
131459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
132459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
133459cc2c6SThierry Reding 	},
134459cc2c6SThierry Reding };
135459cc2c6SThierry Reding #else
136459cc2c6SThierry Reding static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
137459cc2c6SThierry Reding 	{
138459cc2c6SThierry Reding 		.frequency = 75000000,
139459cc2c6SThierry Reding 		.vcocap = 0x3,
140c57997bcSThierry Reding 		.filter = 0x0,
141459cc2c6SThierry Reding 		.ichpmp = 0x1,
142459cc2c6SThierry Reding 		.loadadj = 0x3,
143c57997bcSThierry Reding 		.tmds_termadj = 0x9,
144c57997bcSThierry Reding 		.tx_pu_value = 0x40,
145c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
146c57997bcSThierry Reding 		.bg_vref_level = 0x8,
147c57997bcSThierry Reding 		.avdd10_level = 0x4,
148c57997bcSThierry Reding 		.avdd14_level = 0x4,
149c57997bcSThierry Reding 		.sparepll = 0x0,
150459cc2c6SThierry Reding 		.drive_current = { 0x29, 0x29, 0x29, 0x29 },
151459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
152459cc2c6SThierry Reding 	}, {
153459cc2c6SThierry Reding 		.frequency = 150000000,
154459cc2c6SThierry Reding 		.vcocap = 0x3,
155c57997bcSThierry Reding 		.filter = 0x0,
156459cc2c6SThierry Reding 		.ichpmp = 0x1,
157459cc2c6SThierry Reding 		.loadadj = 0x3,
158c57997bcSThierry Reding 		.tmds_termadj = 0x9,
159c57997bcSThierry Reding 		.tx_pu_value = 0x66,
160c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
161c57997bcSThierry Reding 		.bg_vref_level = 0x8,
162c57997bcSThierry Reding 		.avdd10_level = 0x4,
163c57997bcSThierry Reding 		.avdd14_level = 0x4,
164c57997bcSThierry Reding 		.sparepll = 0x0,
165459cc2c6SThierry Reding 		.drive_current = { 0x30, 0x37, 0x37, 0x37 },
166459cc2c6SThierry Reding 		.preemphasis = { 0x01, 0x02, 0x02, 0x02 },
167459cc2c6SThierry Reding 	}, {
168459cc2c6SThierry Reding 		.frequency = 300000000,
169459cc2c6SThierry Reding 		.vcocap = 0x3,
170c57997bcSThierry Reding 		.filter = 0x0,
171459cc2c6SThierry Reding 		.ichpmp = 0x6,
172459cc2c6SThierry Reding 		.loadadj = 0x3,
173c57997bcSThierry Reding 		.tmds_termadj = 0x9,
174c57997bcSThierry Reding 		.tx_pu_value = 0x66,
175c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
176c57997bcSThierry Reding 		.bg_vref_level = 0xf,
177c57997bcSThierry Reding 		.avdd10_level = 0x4,
178c57997bcSThierry Reding 		.avdd14_level = 0x4,
179c57997bcSThierry Reding 		.sparepll = 0x0,
180459cc2c6SThierry Reding 		.drive_current = { 0x30, 0x37, 0x37, 0x37 },
181459cc2c6SThierry Reding 		.preemphasis = { 0x10, 0x3e, 0x3e, 0x3e },
182459cc2c6SThierry Reding 	}, {
183459cc2c6SThierry Reding 		.frequency = 600000000,
184459cc2c6SThierry Reding 		.vcocap = 0x3,
185c57997bcSThierry Reding 		.filter = 0x0,
186459cc2c6SThierry Reding 		.ichpmp = 0xa,
187459cc2c6SThierry Reding 		.loadadj = 0x3,
188c57997bcSThierry Reding 		.tmds_termadj = 0xb,
189c57997bcSThierry Reding 		.tx_pu_value = 0x66,
190c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
191c57997bcSThierry Reding 		.bg_vref_level = 0xe,
192c57997bcSThierry Reding 		.avdd10_level = 0x4,
193c57997bcSThierry Reding 		.avdd14_level = 0x4,
194c57997bcSThierry Reding 		.sparepll = 0x0,
195459cc2c6SThierry Reding 		.drive_current = { 0x35, 0x3e, 0x3e, 0x3e },
196459cc2c6SThierry Reding 		.preemphasis = { 0x02, 0x3f, 0x3f, 0x3f },
197459cc2c6SThierry Reding 	},
198459cc2c6SThierry Reding };
199459cc2c6SThierry Reding #endif
200459cc2c6SThierry Reding 
201c57997bcSThierry Reding static const struct tegra_sor_hdmi_settings tegra186_sor_hdmi_defaults[] = {
202c57997bcSThierry Reding 	{
203c57997bcSThierry Reding 		.frequency = 54000000,
204c57997bcSThierry Reding 		.vcocap = 0,
205c57997bcSThierry Reding 		.filter = 5,
206c57997bcSThierry Reding 		.ichpmp = 5,
207c57997bcSThierry Reding 		.loadadj = 3,
208c57997bcSThierry Reding 		.tmds_termadj = 0xf,
209c57997bcSThierry Reding 		.tx_pu_value = 0,
210c57997bcSThierry Reding 		.bg_temp_coef = 3,
211c57997bcSThierry Reding 		.bg_vref_level = 8,
212c57997bcSThierry Reding 		.avdd10_level = 4,
213c57997bcSThierry Reding 		.avdd14_level = 4,
214c57997bcSThierry Reding 		.sparepll = 0x54,
215c57997bcSThierry Reding 		.drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
216c57997bcSThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
217c57997bcSThierry Reding 	}, {
218c57997bcSThierry Reding 		.frequency = 75000000,
219c57997bcSThierry Reding 		.vcocap = 1,
220c57997bcSThierry Reding 		.filter = 5,
221c57997bcSThierry Reding 		.ichpmp = 5,
222c57997bcSThierry Reding 		.loadadj = 3,
223c57997bcSThierry Reding 		.tmds_termadj = 0xf,
224c57997bcSThierry Reding 		.tx_pu_value = 0,
225c57997bcSThierry Reding 		.bg_temp_coef = 3,
226c57997bcSThierry Reding 		.bg_vref_level = 8,
227c57997bcSThierry Reding 		.avdd10_level = 4,
228c57997bcSThierry Reding 		.avdd14_level = 4,
229c57997bcSThierry Reding 		.sparepll = 0x44,
230c57997bcSThierry Reding 		.drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
231c57997bcSThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
232c57997bcSThierry Reding 	}, {
233c57997bcSThierry Reding 		.frequency = 150000000,
234c57997bcSThierry Reding 		.vcocap = 3,
235c57997bcSThierry Reding 		.filter = 5,
236c57997bcSThierry Reding 		.ichpmp = 5,
237c57997bcSThierry Reding 		.loadadj = 3,
238c57997bcSThierry Reding 		.tmds_termadj = 15,
239c57997bcSThierry Reding 		.tx_pu_value = 0x66 /* 0 */,
240c57997bcSThierry Reding 		.bg_temp_coef = 3,
241c57997bcSThierry Reding 		.bg_vref_level = 8,
242c57997bcSThierry Reding 		.avdd10_level = 4,
243c57997bcSThierry Reding 		.avdd14_level = 4,
244c57997bcSThierry Reding 		.sparepll = 0x00, /* 0x34 */
245c57997bcSThierry Reding 		.drive_current = { 0x3a, 0x3a, 0x3a, 0x37 },
246c57997bcSThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
247c57997bcSThierry Reding 	}, {
248c57997bcSThierry Reding 		.frequency = 300000000,
249c57997bcSThierry Reding 		.vcocap = 3,
250c57997bcSThierry Reding 		.filter = 5,
251c57997bcSThierry Reding 		.ichpmp = 5,
252c57997bcSThierry Reding 		.loadadj = 3,
253c57997bcSThierry Reding 		.tmds_termadj = 15,
254c57997bcSThierry Reding 		.tx_pu_value = 64,
255c57997bcSThierry Reding 		.bg_temp_coef = 3,
256c57997bcSThierry Reding 		.bg_vref_level = 8,
257c57997bcSThierry Reding 		.avdd10_level = 4,
258c57997bcSThierry Reding 		.avdd14_level = 4,
259c57997bcSThierry Reding 		.sparepll = 0x34,
260c57997bcSThierry Reding 		.drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
261c57997bcSThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
262c57997bcSThierry Reding 	}, {
263c57997bcSThierry Reding 		.frequency = 600000000,
264c57997bcSThierry Reding 		.vcocap = 3,
265c57997bcSThierry Reding 		.filter = 5,
266c57997bcSThierry Reding 		.ichpmp = 5,
267c57997bcSThierry Reding 		.loadadj = 3,
268c57997bcSThierry Reding 		.tmds_termadj = 12,
269c57997bcSThierry Reding 		.tx_pu_value = 96,
270c57997bcSThierry Reding 		.bg_temp_coef = 3,
271c57997bcSThierry Reding 		.bg_vref_level = 8,
272c57997bcSThierry Reding 		.avdd10_level = 4,
273c57997bcSThierry Reding 		.avdd14_level = 4,
274c57997bcSThierry Reding 		.sparepll = 0x34,
275c57997bcSThierry Reding 		.drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
276c57997bcSThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
277c57997bcSThierry Reding 	}
278c57997bcSThierry Reding };
279c57997bcSThierry Reding 
2809b6c14b8SThierry Reding static const struct tegra_sor_hdmi_settings tegra194_sor_hdmi_defaults[] = {
2819b6c14b8SThierry Reding 	{
2829b6c14b8SThierry Reding 		.frequency = 54000000,
2839b6c14b8SThierry Reding 		.vcocap = 0,
2849b6c14b8SThierry Reding 		.filter = 5,
2859b6c14b8SThierry Reding 		.ichpmp = 5,
2869b6c14b8SThierry Reding 		.loadadj = 3,
2879b6c14b8SThierry Reding 		.tmds_termadj = 0xf,
2889b6c14b8SThierry Reding 		.tx_pu_value = 0,
2899b6c14b8SThierry Reding 		.bg_temp_coef = 3,
2909b6c14b8SThierry Reding 		.bg_vref_level = 8,
2919b6c14b8SThierry Reding 		.avdd10_level = 4,
2929b6c14b8SThierry Reding 		.avdd14_level = 4,
2939b6c14b8SThierry Reding 		.sparepll = 0x54,
2949b6c14b8SThierry Reding 		.drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
2959b6c14b8SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
2969b6c14b8SThierry Reding 	}, {
2979b6c14b8SThierry Reding 		.frequency = 75000000,
2989b6c14b8SThierry Reding 		.vcocap = 1,
2999b6c14b8SThierry Reding 		.filter = 5,
3009b6c14b8SThierry Reding 		.ichpmp = 5,
3019b6c14b8SThierry Reding 		.loadadj = 3,
3029b6c14b8SThierry Reding 		.tmds_termadj = 0xf,
3039b6c14b8SThierry Reding 		.tx_pu_value = 0,
3049b6c14b8SThierry Reding 		.bg_temp_coef = 3,
3059b6c14b8SThierry Reding 		.bg_vref_level = 8,
3069b6c14b8SThierry Reding 		.avdd10_level = 4,
3079b6c14b8SThierry Reding 		.avdd14_level = 4,
3089b6c14b8SThierry Reding 		.sparepll = 0x44,
3099b6c14b8SThierry Reding 		.drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
3109b6c14b8SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
3119b6c14b8SThierry Reding 	}, {
3129b6c14b8SThierry Reding 		.frequency = 150000000,
3139b6c14b8SThierry Reding 		.vcocap = 3,
3149b6c14b8SThierry Reding 		.filter = 5,
3159b6c14b8SThierry Reding 		.ichpmp = 5,
3169b6c14b8SThierry Reding 		.loadadj = 3,
3179b6c14b8SThierry Reding 		.tmds_termadj = 15,
3189b6c14b8SThierry Reding 		.tx_pu_value = 0x66 /* 0 */,
3199b6c14b8SThierry Reding 		.bg_temp_coef = 3,
3209b6c14b8SThierry Reding 		.bg_vref_level = 8,
3219b6c14b8SThierry Reding 		.avdd10_level = 4,
3229b6c14b8SThierry Reding 		.avdd14_level = 4,
3239b6c14b8SThierry Reding 		.sparepll = 0x00, /* 0x34 */
3249b6c14b8SThierry Reding 		.drive_current = { 0x3a, 0x3a, 0x3a, 0x37 },
3259b6c14b8SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
3269b6c14b8SThierry Reding 	}, {
3279b6c14b8SThierry Reding 		.frequency = 300000000,
3289b6c14b8SThierry Reding 		.vcocap = 3,
3299b6c14b8SThierry Reding 		.filter = 5,
3309b6c14b8SThierry Reding 		.ichpmp = 5,
3319b6c14b8SThierry Reding 		.loadadj = 3,
3329b6c14b8SThierry Reding 		.tmds_termadj = 15,
3339b6c14b8SThierry Reding 		.tx_pu_value = 64,
3349b6c14b8SThierry Reding 		.bg_temp_coef = 3,
3359b6c14b8SThierry Reding 		.bg_vref_level = 8,
3369b6c14b8SThierry Reding 		.avdd10_level = 4,
3379b6c14b8SThierry Reding 		.avdd14_level = 4,
3389b6c14b8SThierry Reding 		.sparepll = 0x34,
3399b6c14b8SThierry Reding 		.drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
3409b6c14b8SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
3419b6c14b8SThierry Reding 	}, {
3429b6c14b8SThierry Reding 		.frequency = 600000000,
3439b6c14b8SThierry Reding 		.vcocap = 3,
3449b6c14b8SThierry Reding 		.filter = 5,
3459b6c14b8SThierry Reding 		.ichpmp = 5,
3469b6c14b8SThierry Reding 		.loadadj = 3,
3479b6c14b8SThierry Reding 		.tmds_termadj = 12,
3489b6c14b8SThierry Reding 		.tx_pu_value = 96,
3499b6c14b8SThierry Reding 		.bg_temp_coef = 3,
3509b6c14b8SThierry Reding 		.bg_vref_level = 8,
3519b6c14b8SThierry Reding 		.avdd10_level = 4,
3529b6c14b8SThierry Reding 		.avdd14_level = 4,
3539b6c14b8SThierry Reding 		.sparepll = 0x34,
3549b6c14b8SThierry Reding 		.drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
3559b6c14b8SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
3569b6c14b8SThierry Reding 	}
3579b6c14b8SThierry Reding };
3589b6c14b8SThierry Reding 
359880cee0bSThierry Reding struct tegra_sor_regs {
360880cee0bSThierry Reding 	unsigned int head_state0;
361880cee0bSThierry Reding 	unsigned int head_state1;
362880cee0bSThierry Reding 	unsigned int head_state2;
363880cee0bSThierry Reding 	unsigned int head_state3;
364880cee0bSThierry Reding 	unsigned int head_state4;
365880cee0bSThierry Reding 	unsigned int head_state5;
366880cee0bSThierry Reding 	unsigned int pll0;
367880cee0bSThierry Reding 	unsigned int pll1;
368880cee0bSThierry Reding 	unsigned int pll2;
369880cee0bSThierry Reding 	unsigned int pll3;
370880cee0bSThierry Reding 	unsigned int dp_padctl0;
371880cee0bSThierry Reding 	unsigned int dp_padctl2;
372880cee0bSThierry Reding };
373880cee0bSThierry Reding 
374459cc2c6SThierry Reding struct tegra_sor_soc {
375459cc2c6SThierry Reding 	bool supports_lvds;
376459cc2c6SThierry Reding 	bool supports_hdmi;
377459cc2c6SThierry Reding 	bool supports_dp;
378d278e4a9SThierry Reding 	bool supports_audio;
379d278e4a9SThierry Reding 	bool supports_hdcp;
380459cc2c6SThierry Reding 
381880cee0bSThierry Reding 	const struct tegra_sor_regs *regs;
382c57997bcSThierry Reding 	bool has_nvdisplay;
383880cee0bSThierry Reding 
384459cc2c6SThierry Reding 	const struct tegra_sor_hdmi_settings *settings;
385459cc2c6SThierry Reding 	unsigned int num_settings;
38630b49435SThierry Reding 
38730b49435SThierry Reding 	const u8 *xbar_cfg;
388c1763937SThierry Reding 	const u8 *lane_map;
389c1763937SThierry Reding 
390c1763937SThierry Reding 	const u8 (*voltage_swing)[4][4];
391c1763937SThierry Reding 	const u8 (*pre_emphasis)[4][4];
392c1763937SThierry Reding 	const u8 (*post_cursor)[4][4];
393c1763937SThierry Reding 	const u8 (*tx_pu)[4][4];
394459cc2c6SThierry Reding };
395459cc2c6SThierry Reding 
396459cc2c6SThierry Reding struct tegra_sor;
397459cc2c6SThierry Reding 
398459cc2c6SThierry Reding struct tegra_sor_ops {
399459cc2c6SThierry Reding 	const char *name;
400459cc2c6SThierry Reding 	int (*probe)(struct tegra_sor *sor);
401459cc2c6SThierry Reding 	int (*remove)(struct tegra_sor *sor);
40285d0c4b5SThierry Reding 	void (*audio_enable)(struct tegra_sor *sor);
40385d0c4b5SThierry Reding 	void (*audio_disable)(struct tegra_sor *sor);
404459cc2c6SThierry Reding };
405459cc2c6SThierry Reding 
4066b6b6042SThierry Reding struct tegra_sor {
4076b6b6042SThierry Reding 	struct host1x_client client;
4086b6b6042SThierry Reding 	struct tegra_output output;
4096b6b6042SThierry Reding 	struct device *dev;
4106b6b6042SThierry Reding 
411459cc2c6SThierry Reding 	const struct tegra_sor_soc *soc;
4126b6b6042SThierry Reding 	void __iomem *regs;
413c57997bcSThierry Reding 	unsigned int index;
4148e2988a7SThierry Reding 	unsigned int irq;
4156b6b6042SThierry Reding 
4166b6b6042SThierry Reding 	struct reset_control *rst;
4176b6b6042SThierry Reding 	struct clk *clk_parent;
4186b6b6042SThierry Reding 	struct clk *clk_safe;
419e1335e2fSThierry Reding 	struct clk *clk_out;
420e1335e2fSThierry Reding 	struct clk *clk_pad;
4216b6b6042SThierry Reding 	struct clk *clk_dp;
4226b6b6042SThierry Reding 	struct clk *clk;
4236b6b6042SThierry Reding 
4246d6c815dSThierry Reding 	u8 xbar_cfg[5];
4256d6c815dSThierry Reding 
426c1763937SThierry Reding 	struct drm_dp_link link;
4279542c237SThierry Reding 	struct drm_dp_aux *aux;
4286b6b6042SThierry Reding 
429dab16336SThierry Reding 	struct drm_info_list *debugfs_files;
430459cc2c6SThierry Reding 
431459cc2c6SThierry Reding 	const struct tegra_sor_ops *ops;
432c57997bcSThierry Reding 	enum tegra_io_pad pad;
433459cc2c6SThierry Reding 
434459cc2c6SThierry Reding 	/* for HDMI 2.0 */
435459cc2c6SThierry Reding 	struct tegra_sor_hdmi_settings *settings;
436459cc2c6SThierry Reding 	unsigned int num_settings;
437459cc2c6SThierry Reding 
438459cc2c6SThierry Reding 	struct regulator *avdd_io_supply;
439459cc2c6SThierry Reding 	struct regulator *vdd_pll_supply;
440459cc2c6SThierry Reding 	struct regulator *hdmi_supply;
44136e90221SThierry Reding 
44236e90221SThierry Reding 	struct delayed_work scdc;
44336e90221SThierry Reding 	bool scdc_enabled;
4448e2988a7SThierry Reding 
445fad7b806SThierry Reding 	struct tegra_hda_format format;
4466b6b6042SThierry Reding };
4476b6b6042SThierry Reding 
448c31efa7aSThierry Reding struct tegra_sor_state {
449c31efa7aSThierry Reding 	struct drm_connector_state base;
450c31efa7aSThierry Reding 
45136e90221SThierry Reding 	unsigned int link_speed;
45236e90221SThierry Reding 	unsigned long pclk;
453c31efa7aSThierry Reding 	unsigned int bpc;
454c31efa7aSThierry Reding };
455c31efa7aSThierry Reding 
456c31efa7aSThierry Reding static inline struct tegra_sor_state *
457c31efa7aSThierry Reding to_sor_state(struct drm_connector_state *state)
458c31efa7aSThierry Reding {
459c31efa7aSThierry Reding 	return container_of(state, struct tegra_sor_state, base);
460c31efa7aSThierry Reding }
461c31efa7aSThierry Reding 
46234fa183bSThierry Reding struct tegra_sor_config {
46334fa183bSThierry Reding 	u32 bits_per_pixel;
46434fa183bSThierry Reding 
46534fa183bSThierry Reding 	u32 active_polarity;
46634fa183bSThierry Reding 	u32 active_count;
46734fa183bSThierry Reding 	u32 tu_size;
46834fa183bSThierry Reding 	u32 active_frac;
46934fa183bSThierry Reding 	u32 watermark;
4707890b576SThierry Reding 
4717890b576SThierry Reding 	u32 hblank_symbols;
4727890b576SThierry Reding 	u32 vblank_symbols;
47334fa183bSThierry Reding };
47434fa183bSThierry Reding 
4756b6b6042SThierry Reding static inline struct tegra_sor *
4766b6b6042SThierry Reding host1x_client_to_sor(struct host1x_client *client)
4776b6b6042SThierry Reding {
4786b6b6042SThierry Reding 	return container_of(client, struct tegra_sor, client);
4796b6b6042SThierry Reding }
4806b6b6042SThierry Reding 
4816b6b6042SThierry Reding static inline struct tegra_sor *to_sor(struct tegra_output *output)
4826b6b6042SThierry Reding {
4836b6b6042SThierry Reding 	return container_of(output, struct tegra_sor, output);
4846b6b6042SThierry Reding }
4856b6b6042SThierry Reding 
4865c5f1301SThierry Reding static inline u32 tegra_sor_readl(struct tegra_sor *sor, unsigned int offset)
4876b6b6042SThierry Reding {
488932f6529SThierry Reding 	u32 value = readl(sor->regs + (offset << 2));
489932f6529SThierry Reding 
490932f6529SThierry Reding 	trace_sor_readl(sor->dev, offset, value);
491932f6529SThierry Reding 
492932f6529SThierry Reding 	return value;
4936b6b6042SThierry Reding }
4946b6b6042SThierry Reding 
49528fe2076SThierry Reding static inline void tegra_sor_writel(struct tegra_sor *sor, u32 value,
4965c5f1301SThierry Reding 				    unsigned int offset)
4976b6b6042SThierry Reding {
498932f6529SThierry Reding 	trace_sor_writel(sor->dev, offset, value);
4996b6b6042SThierry Reding 	writel(value, sor->regs + (offset << 2));
5006b6b6042SThierry Reding }
5016b6b6042SThierry Reding 
50225bb2cecSThierry Reding static int tegra_sor_set_parent_clock(struct tegra_sor *sor, struct clk *parent)
50325bb2cecSThierry Reding {
50425bb2cecSThierry Reding 	int err;
50525bb2cecSThierry Reding 
50625bb2cecSThierry Reding 	clk_disable_unprepare(sor->clk);
50725bb2cecSThierry Reding 
508e1335e2fSThierry Reding 	err = clk_set_parent(sor->clk_out, parent);
50925bb2cecSThierry Reding 	if (err < 0)
51025bb2cecSThierry Reding 		return err;
51125bb2cecSThierry Reding 
51225bb2cecSThierry Reding 	err = clk_prepare_enable(sor->clk);
51325bb2cecSThierry Reding 	if (err < 0)
51425bb2cecSThierry Reding 		return err;
51525bb2cecSThierry Reding 
51625bb2cecSThierry Reding 	return 0;
51725bb2cecSThierry Reding }
51825bb2cecSThierry Reding 
519e1335e2fSThierry Reding struct tegra_clk_sor_pad {
520b299221cSThierry Reding 	struct clk_hw hw;
521b299221cSThierry Reding 	struct tegra_sor *sor;
522b299221cSThierry Reding };
523b299221cSThierry Reding 
524e1335e2fSThierry Reding static inline struct tegra_clk_sor_pad *to_pad(struct clk_hw *hw)
525b299221cSThierry Reding {
526e1335e2fSThierry Reding 	return container_of(hw, struct tegra_clk_sor_pad, hw);
527b299221cSThierry Reding }
528b299221cSThierry Reding 
5294bdf4710SThierry Reding static const char * const tegra_clk_sor_pad_parents[2][2] = {
5304bdf4710SThierry Reding 	{ "pll_d_out0", "pll_dp" },
5314bdf4710SThierry Reding 	{ "pll_d2_out0", "pll_dp" },
532b299221cSThierry Reding };
533b299221cSThierry Reding 
53461417aaaSThierry Reding /*
53561417aaaSThierry Reding  * Implementing ->set_parent() here isn't really required because the parent
53661417aaaSThierry Reding  * will be explicitly selected in the driver code via the DP_CLK_SEL mux in
53761417aaaSThierry Reding  * the SOR_CLK_CNTRL register. This is primarily for compatibility with the
53861417aaaSThierry Reding  * Tegra186 and later SoC generations where the BPMP implements this clock
53961417aaaSThierry Reding  * and doesn't expose the mux via the common clock framework.
54061417aaaSThierry Reding  */
54161417aaaSThierry Reding 
542e1335e2fSThierry Reding static int tegra_clk_sor_pad_set_parent(struct clk_hw *hw, u8 index)
543b299221cSThierry Reding {
544e1335e2fSThierry Reding 	struct tegra_clk_sor_pad *pad = to_pad(hw);
545e1335e2fSThierry Reding 	struct tegra_sor *sor = pad->sor;
546b299221cSThierry Reding 	u32 value;
547b299221cSThierry Reding 
548b299221cSThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
549b299221cSThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
550b299221cSThierry Reding 
551b299221cSThierry Reding 	switch (index) {
552b299221cSThierry Reding 	case 0:
553b299221cSThierry Reding 		value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
554b299221cSThierry Reding 		break;
555b299221cSThierry Reding 
556b299221cSThierry Reding 	case 1:
557b299221cSThierry Reding 		value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
558b299221cSThierry Reding 		break;
559b299221cSThierry Reding 	}
560b299221cSThierry Reding 
561b299221cSThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
562b299221cSThierry Reding 
563b299221cSThierry Reding 	return 0;
564b299221cSThierry Reding }
565b299221cSThierry Reding 
566e1335e2fSThierry Reding static u8 tegra_clk_sor_pad_get_parent(struct clk_hw *hw)
567b299221cSThierry Reding {
568e1335e2fSThierry Reding 	struct tegra_clk_sor_pad *pad = to_pad(hw);
569e1335e2fSThierry Reding 	struct tegra_sor *sor = pad->sor;
570b299221cSThierry Reding 	u8 parent = U8_MAX;
571b299221cSThierry Reding 	u32 value;
572b299221cSThierry Reding 
573b299221cSThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
574b299221cSThierry Reding 
575b299221cSThierry Reding 	switch (value & SOR_CLK_CNTRL_DP_CLK_SEL_MASK) {
576b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK:
577b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_PCLK:
578b299221cSThierry Reding 		parent = 0;
579b299221cSThierry Reding 		break;
580b299221cSThierry Reding 
581b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK:
582b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_DPCLK:
583b299221cSThierry Reding 		parent = 1;
584b299221cSThierry Reding 		break;
585b299221cSThierry Reding 	}
586b299221cSThierry Reding 
587b299221cSThierry Reding 	return parent;
588b299221cSThierry Reding }
589b299221cSThierry Reding 
590e1335e2fSThierry Reding static const struct clk_ops tegra_clk_sor_pad_ops = {
591e1335e2fSThierry Reding 	.set_parent = tegra_clk_sor_pad_set_parent,
592e1335e2fSThierry Reding 	.get_parent = tegra_clk_sor_pad_get_parent,
593b299221cSThierry Reding };
594b299221cSThierry Reding 
595e1335e2fSThierry Reding static struct clk *tegra_clk_sor_pad_register(struct tegra_sor *sor,
596b299221cSThierry Reding 					      const char *name)
597b299221cSThierry Reding {
598e1335e2fSThierry Reding 	struct tegra_clk_sor_pad *pad;
599b299221cSThierry Reding 	struct clk_init_data init;
600b299221cSThierry Reding 	struct clk *clk;
601b299221cSThierry Reding 
602e1335e2fSThierry Reding 	pad = devm_kzalloc(sor->dev, sizeof(*pad), GFP_KERNEL);
603e1335e2fSThierry Reding 	if (!pad)
604b299221cSThierry Reding 		return ERR_PTR(-ENOMEM);
605b299221cSThierry Reding 
606e1335e2fSThierry Reding 	pad->sor = sor;
607b299221cSThierry Reding 
608b299221cSThierry Reding 	init.name = name;
609b299221cSThierry Reding 	init.flags = 0;
6104bdf4710SThierry Reding 	init.parent_names = tegra_clk_sor_pad_parents[sor->index];
6114bdf4710SThierry Reding 	init.num_parents = ARRAY_SIZE(tegra_clk_sor_pad_parents[sor->index]);
612e1335e2fSThierry Reding 	init.ops = &tegra_clk_sor_pad_ops;
613b299221cSThierry Reding 
614e1335e2fSThierry Reding 	pad->hw.init = &init;
615b299221cSThierry Reding 
616e1335e2fSThierry Reding 	clk = devm_clk_register(sor->dev, &pad->hw);
617b299221cSThierry Reding 
618b299221cSThierry Reding 	return clk;
619b299221cSThierry Reding }
620b299221cSThierry Reding 
621c9533131SThierry Reding static void tegra_sor_filter_rates(struct tegra_sor *sor)
622c9533131SThierry Reding {
623c9533131SThierry Reding 	struct drm_dp_link *link = &sor->link;
624c9533131SThierry Reding 	unsigned int i;
625c9533131SThierry Reding 
626c9533131SThierry Reding 	/* Tegra only supports RBR, HBR and HBR2 */
627c9533131SThierry Reding 	for (i = 0; i < link->num_rates; i++) {
628c9533131SThierry Reding 		switch (link->rates[i]) {
629c9533131SThierry Reding 		case 1620000:
630c9533131SThierry Reding 		case 2700000:
631c9533131SThierry Reding 		case 5400000:
632c9533131SThierry Reding 			break;
633c9533131SThierry Reding 
634c9533131SThierry Reding 		default:
635c9533131SThierry Reding 			DRM_DEBUG_KMS("link rate %lu kHz not supported\n",
636c9533131SThierry Reding 				      link->rates[i]);
637c9533131SThierry Reding 			link->rates[i] = 0;
638c9533131SThierry Reding 			break;
639c9533131SThierry Reding 		}
640c9533131SThierry Reding 	}
641c9533131SThierry Reding 
642c9533131SThierry Reding 	drm_dp_link_update_rates(link);
643c9533131SThierry Reding }
644c9533131SThierry Reding 
645c1763937SThierry Reding static int tegra_sor_power_up_lanes(struct tegra_sor *sor, unsigned int lanes)
6466b6b6042SThierry Reding {
647c1763937SThierry Reding 	unsigned long timeout;
64828fe2076SThierry Reding 	u32 value;
6496b6b6042SThierry Reding 
650c1763937SThierry Reding 	/*
651c1763937SThierry Reding 	 * Clear or set the PD_TXD bit corresponding to each lane, depending
652c1763937SThierry Reding 	 * on whether it is used or not.
653c1763937SThierry Reding 	 */
654880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
655c1763937SThierry Reding 
656c1763937SThierry Reding 	if (lanes <= 2)
657c1763937SThierry Reding 		value &= ~(SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[3]) |
658c1763937SThierry Reding 			   SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[2]));
659c1763937SThierry Reding 	else
660c1763937SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[3]) |
661c1763937SThierry Reding 			 SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[2]);
662c1763937SThierry Reding 
663c1763937SThierry Reding 	if (lanes <= 1)
664c1763937SThierry Reding 		value &= ~SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[1]);
665c1763937SThierry Reding 	else
666c1763937SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[1]);
667c1763937SThierry Reding 
668c1763937SThierry Reding 	if (lanes == 0)
669c1763937SThierry Reding 		value &= ~SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[0]);
670c1763937SThierry Reding 	else
671c1763937SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[0]);
672c1763937SThierry Reding 
673880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
6746b6b6042SThierry Reding 
675c1763937SThierry Reding 	/* start lane sequencer */
676c1763937SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
677c1763937SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_UP;
678c1763937SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
679c1763937SThierry Reding 
680c1763937SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
681c1763937SThierry Reding 
682c1763937SThierry Reding 	while (time_before(jiffies, timeout)) {
683c1763937SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
684c1763937SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
685c1763937SThierry Reding 			break;
686c1763937SThierry Reding 
687c1763937SThierry Reding 		usleep_range(250, 1000);
688c1763937SThierry Reding 	}
689c1763937SThierry Reding 
690c1763937SThierry Reding 	if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
691c1763937SThierry Reding 		return -ETIMEDOUT;
692c1763937SThierry Reding 
693c1763937SThierry Reding 	return 0;
694c1763937SThierry Reding }
695c1763937SThierry Reding 
696c1763937SThierry Reding static int tegra_sor_power_down_lanes(struct tegra_sor *sor)
697c1763937SThierry Reding {
698c1763937SThierry Reding 	unsigned long timeout;
699c1763937SThierry Reding 	u32 value;
700c1763937SThierry Reding 
701c1763937SThierry Reding 	/* power down all lanes */
702880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
703c1763937SThierry Reding 	value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
704c1763937SThierry Reding 		   SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
705880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
7066b6b6042SThierry Reding 
707c1763937SThierry Reding 	/* start lane sequencer */
708c1763937SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
709c1763937SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
710c1763937SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
711c1763937SThierry Reding 
712c1763937SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
713c1763937SThierry Reding 
714c1763937SThierry Reding 	while (time_before(jiffies, timeout)) {
715c1763937SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
716c1763937SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
717c1763937SThierry Reding 			break;
718c1763937SThierry Reding 
719c1763937SThierry Reding 		usleep_range(25, 100);
720c1763937SThierry Reding 	}
721c1763937SThierry Reding 
722c1763937SThierry Reding 	if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
723c1763937SThierry Reding 		return -ETIMEDOUT;
724c1763937SThierry Reding 
725c1763937SThierry Reding 	return 0;
726c1763937SThierry Reding }
727c1763937SThierry Reding 
728c1763937SThierry Reding static void tegra_sor_dp_precharge(struct tegra_sor *sor, unsigned int lanes)
729c1763937SThierry Reding {
730c1763937SThierry Reding 	u32 value;
731c1763937SThierry Reding 
732c1763937SThierry Reding 	/* pre-charge all used lanes */
733c1763937SThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
734c1763937SThierry Reding 
735c1763937SThierry Reding 	if (lanes <= 2)
736c1763937SThierry Reding 		value &= ~(SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[3]) |
737c1763937SThierry Reding 			   SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[2]));
738c1763937SThierry Reding 	else
739c1763937SThierry Reding 		value |= SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[3]) |
740c1763937SThierry Reding 			 SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[2]);
741c1763937SThierry Reding 
742c1763937SThierry Reding 	if (lanes <= 1)
743c1763937SThierry Reding 		value &= ~SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[1]);
744c1763937SThierry Reding 	else
745c1763937SThierry Reding 		value |= SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[1]);
746c1763937SThierry Reding 
747c1763937SThierry Reding 	if (lanes == 0)
748c1763937SThierry Reding 		value &= ~SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[0]);
749c1763937SThierry Reding 	else
750c1763937SThierry Reding 		value |= SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[0]);
751c1763937SThierry Reding 
752c1763937SThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
753c1763937SThierry Reding 
754c1763937SThierry Reding 	usleep_range(15, 100);
7556b6b6042SThierry Reding 
756880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
7576b6b6042SThierry Reding 	value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
7586b6b6042SThierry Reding 		   SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
759880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
7606b6b6042SThierry Reding }
7616b6b6042SThierry Reding 
762c1763937SThierry Reding static void tegra_sor_dp_term_calibrate(struct tegra_sor *sor)
763c1763937SThierry Reding {
764c1763937SThierry Reding 	u32 mask = 0x08, adj = 0, value;
7656b6b6042SThierry Reding 
766c1763937SThierry Reding 	/* enable pad calibration logic */
767c1763937SThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
768c1763937SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
769c1763937SThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
7706b6b6042SThierry Reding 
771c1763937SThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll1);
772c1763937SThierry Reding 	value |= SOR_PLL1_TMDS_TERM;
773c1763937SThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll1);
7746b6b6042SThierry Reding 
775c1763937SThierry Reding 	while (mask) {
776c1763937SThierry Reding 		adj |= mask;
7776b6b6042SThierry Reding 
778c1763937SThierry Reding 		value = tegra_sor_readl(sor, sor->soc->regs->pll1);
779c1763937SThierry Reding 		value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
780c1763937SThierry Reding 		value |= SOR_PLL1_TMDS_TERMADJ(adj);
781c1763937SThierry Reding 		tegra_sor_writel(sor, value, sor->soc->regs->pll1);
782c1763937SThierry Reding 
783c1763937SThierry Reding 		usleep_range(100, 200);
784c1763937SThierry Reding 
785c1763937SThierry Reding 		value = tegra_sor_readl(sor, sor->soc->regs->pll1);
786c1763937SThierry Reding 		if (value & SOR_PLL1_TERM_COMPOUT)
787c1763937SThierry Reding 			adj &= ~mask;
788c1763937SThierry Reding 
789c1763937SThierry Reding 		mask >>= 1;
7906b6b6042SThierry Reding 	}
7916b6b6042SThierry Reding 
792c1763937SThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll1);
793c1763937SThierry Reding 	value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
794c1763937SThierry Reding 	value |= SOR_PLL1_TMDS_TERMADJ(adj);
795c1763937SThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll1);
7966b6b6042SThierry Reding 
797c1763937SThierry Reding 	/* disable pad calibration logic */
798c1763937SThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
799c1763937SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
800c1763937SThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
801c1763937SThierry Reding }
8026b6b6042SThierry Reding 
803c1763937SThierry Reding static int tegra_sor_dp_link_apply_training(struct drm_dp_link *link)
804c1763937SThierry Reding {
805c1763937SThierry Reding 	struct tegra_sor *sor = container_of(link, struct tegra_sor, link);
806c1763937SThierry Reding 	u32 voltage_swing = 0, pre_emphasis = 0, post_cursor = 0;
807c1763937SThierry Reding 	const struct tegra_sor_soc *soc = sor->soc;
808c1763937SThierry Reding 	u32 pattern = 0, tx_pu = 0, value;
809c1763937SThierry Reding 	unsigned int i;
8106b6b6042SThierry Reding 
811c1763937SThierry Reding 	for (value = 0, i = 0; i < link->lanes; i++) {
812c1763937SThierry Reding 		u8 vs = link->train.request.voltage_swing[i];
813c1763937SThierry Reding 		u8 pe = link->train.request.pre_emphasis[i];
814c1763937SThierry Reding 		u8 pc = link->train.request.post_cursor[i];
815c1763937SThierry Reding 		u8 shift = sor->soc->lane_map[i] << 3;
816c1763937SThierry Reding 
817c1763937SThierry Reding 		voltage_swing |= soc->voltage_swing[pc][vs][pe] << shift;
818c1763937SThierry Reding 		pre_emphasis |= soc->pre_emphasis[pc][vs][pe] << shift;
819c1763937SThierry Reding 		post_cursor |= soc->post_cursor[pc][vs][pe] << shift;
820c1763937SThierry Reding 
821c1763937SThierry Reding 		if (sor->soc->tx_pu[pc][vs][pe] > tx_pu)
822c1763937SThierry Reding 			tx_pu = sor->soc->tx_pu[pc][vs][pe];
823c1763937SThierry Reding 
824c1763937SThierry Reding 		switch (link->train.pattern) {
825c1763937SThierry Reding 		case DP_TRAINING_PATTERN_DISABLE:
826c1763937SThierry Reding 			value = SOR_DP_TPG_SCRAMBLER_GALIOS |
8276b6b6042SThierry Reding 				SOR_DP_TPG_PATTERN_NONE;
828c1763937SThierry Reding 			break;
829c1763937SThierry Reding 
830c1763937SThierry Reding 		case DP_TRAINING_PATTERN_1:
831c1763937SThierry Reding 			value = SOR_DP_TPG_SCRAMBLER_NONE |
832c1763937SThierry Reding 				SOR_DP_TPG_PATTERN_TRAIN1;
833c1763937SThierry Reding 			break;
834c1763937SThierry Reding 
835c1763937SThierry Reding 		case DP_TRAINING_PATTERN_2:
836c1763937SThierry Reding 			value = SOR_DP_TPG_SCRAMBLER_NONE |
837c1763937SThierry Reding 				SOR_DP_TPG_PATTERN_TRAIN2;
838c1763937SThierry Reding 			break;
839c1763937SThierry Reding 
840c1763937SThierry Reding 		case DP_TRAINING_PATTERN_3:
841c1763937SThierry Reding 			value = SOR_DP_TPG_SCRAMBLER_NONE |
842c1763937SThierry Reding 				SOR_DP_TPG_PATTERN_TRAIN3;
843c1763937SThierry Reding 			break;
844c1763937SThierry Reding 
845c1763937SThierry Reding 		default:
846c1763937SThierry Reding 			return -EINVAL;
8476b6b6042SThierry Reding 		}
8486b6b6042SThierry Reding 
849c1763937SThierry Reding 		if (link->caps.channel_coding)
850c1763937SThierry Reding 			value |= SOR_DP_TPG_CHANNEL_CODING;
8516b6b6042SThierry Reding 
852c1763937SThierry Reding 		pattern = pattern << 8 | value;
853c1763937SThierry Reding 	}
8546b6b6042SThierry Reding 
855c1763937SThierry Reding 	tegra_sor_writel(sor, voltage_swing, SOR_LANE_DRIVE_CURRENT0);
856c1763937SThierry Reding 	tegra_sor_writel(sor, pre_emphasis, SOR_LANE_PREEMPHASIS0);
857c1763937SThierry Reding 
858c1763937SThierry Reding 	if (link->caps.tps3_supported)
859c1763937SThierry Reding 		tegra_sor_writel(sor, post_cursor, SOR_LANE_POSTCURSOR0);
860c1763937SThierry Reding 
861c1763937SThierry Reding 	tegra_sor_writel(sor, pattern, SOR_DP_TPG);
862c1763937SThierry Reding 
863c1763937SThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
864c1763937SThierry Reding 	value &= ~SOR_DP_PADCTL_TX_PU_MASK;
865c1763937SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU_ENABLE;
866c1763937SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU(tx_pu);
867c1763937SThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
868c1763937SThierry Reding 
869c1763937SThierry Reding 	usleep_range(20, 100);
8706b6b6042SThierry Reding 
8716b6b6042SThierry Reding 	return 0;
8726b6b6042SThierry Reding }
8736b6b6042SThierry Reding 
874c1763937SThierry Reding static int tegra_sor_dp_link_configure(struct drm_dp_link *link)
875c1763937SThierry Reding {
876c1763937SThierry Reding 	struct tegra_sor *sor = container_of(link, struct tegra_sor, link);
877c1763937SThierry Reding 	unsigned int rate, lanes;
878c1763937SThierry Reding 	u32 value;
879c1763937SThierry Reding 	int err;
880c1763937SThierry Reding 
881c1763937SThierry Reding 	rate = drm_dp_link_rate_to_bw_code(link->rate);
882c1763937SThierry Reding 	lanes = link->lanes;
883c1763937SThierry Reding 
884c1763937SThierry Reding 	/* configure link speed and lane count */
885c1763937SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
886c1763937SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
887c1763937SThierry Reding 	value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
888c1763937SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
889c1763937SThierry Reding 
890c1763937SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
891c1763937SThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
892c1763937SThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
893c1763937SThierry Reding 
894c1763937SThierry Reding 	if (link->caps.enhanced_framing)
895c1763937SThierry Reding 		value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
896c1763937SThierry Reding 
897c1763937SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
898c1763937SThierry Reding 
899c1763937SThierry Reding 	usleep_range(400, 1000);
900c1763937SThierry Reding 
901c1763937SThierry Reding 	/* configure load pulse position adjustment */
902c1763937SThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll1);
903c1763937SThierry Reding 	value &= ~SOR_PLL1_LOADADJ_MASK;
904c1763937SThierry Reding 
905c1763937SThierry Reding 	switch (rate) {
906c1763937SThierry Reding 	case DP_LINK_BW_1_62:
907c1763937SThierry Reding 		value |= SOR_PLL1_LOADADJ(0x3);
908c1763937SThierry Reding 		break;
909c1763937SThierry Reding 
910c1763937SThierry Reding 	case DP_LINK_BW_2_7:
911c1763937SThierry Reding 		value |= SOR_PLL1_LOADADJ(0x4);
912c1763937SThierry Reding 		break;
913c1763937SThierry Reding 
914c1763937SThierry Reding 	case DP_LINK_BW_5_4:
915c1763937SThierry Reding 		value |= SOR_PLL1_LOADADJ(0x6);
916c1763937SThierry Reding 		break;
917c1763937SThierry Reding 	}
918c1763937SThierry Reding 
919c1763937SThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll1);
920c1763937SThierry Reding 
921c1763937SThierry Reding 	/* use alternate scrambler reset for eDP */
922c1763937SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
923c1763937SThierry Reding 
924c1763937SThierry Reding 	if (link->edp == 0)
925c1763937SThierry Reding 		value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
926c1763937SThierry Reding 	else
927c1763937SThierry Reding 		value |= SOR_DP_SPARE_PANEL_INTERNAL;
928c1763937SThierry Reding 
929c1763937SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
930c1763937SThierry Reding 
931c1763937SThierry Reding 	err = tegra_sor_power_down_lanes(sor);
932c1763937SThierry Reding 	if (err < 0) {
933c1763937SThierry Reding 		dev_err(sor->dev, "failed to power down lanes: %d\n", err);
934c1763937SThierry Reding 		return err;
935c1763937SThierry Reding 	}
936c1763937SThierry Reding 
937c1763937SThierry Reding 	/* power up and pre-charge lanes */
938c1763937SThierry Reding 	err = tegra_sor_power_up_lanes(sor, lanes);
939c1763937SThierry Reding 	if (err < 0) {
940c1763937SThierry Reding 		dev_err(sor->dev, "failed to power up %u lane%s: %d\n",
941c1763937SThierry Reding 			lanes, (lanes != 1) ? "s" : "", err);
942c1763937SThierry Reding 		return err;
943c1763937SThierry Reding 	}
944c1763937SThierry Reding 
945c1763937SThierry Reding 	tegra_sor_dp_precharge(sor, lanes);
946c1763937SThierry Reding 
947c1763937SThierry Reding 	return 0;
948c1763937SThierry Reding }
949c1763937SThierry Reding 
950c1763937SThierry Reding static const struct drm_dp_link_ops tegra_sor_dp_link_ops = {
951c1763937SThierry Reding 	.apply_training = tegra_sor_dp_link_apply_training,
952c1763937SThierry Reding 	.configure = tegra_sor_dp_link_configure,
953c1763937SThierry Reding };
954c1763937SThierry Reding 
9556b6b6042SThierry Reding static void tegra_sor_super_update(struct tegra_sor *sor)
9566b6b6042SThierry Reding {
957a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
958a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 1, SOR_SUPER_STATE0);
959a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
9606b6b6042SThierry Reding }
9616b6b6042SThierry Reding 
9626b6b6042SThierry Reding static void tegra_sor_update(struct tegra_sor *sor)
9636b6b6042SThierry Reding {
964a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE0);
965a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 1, SOR_STATE0);
966a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE0);
9676b6b6042SThierry Reding }
9686b6b6042SThierry Reding 
9696b6b6042SThierry Reding static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
9706b6b6042SThierry Reding {
97128fe2076SThierry Reding 	u32 value;
9726b6b6042SThierry Reding 
9736b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWM_DIV);
9746b6b6042SThierry Reding 	value &= ~SOR_PWM_DIV_MASK;
9756b6b6042SThierry Reding 	value |= 0x400; /* period */
9766b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWM_DIV);
9776b6b6042SThierry Reding 
9786b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWM_CTL);
9796b6b6042SThierry Reding 	value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
9806b6b6042SThierry Reding 	value |= 0x400; /* duty cycle */
9816b6b6042SThierry Reding 	value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
9826b6b6042SThierry Reding 	value |= SOR_PWM_CTL_TRIGGER;
9836b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWM_CTL);
9846b6b6042SThierry Reding 
9856b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
9866b6b6042SThierry Reding 
9876b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
9886b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWM_CTL);
9896b6b6042SThierry Reding 		if ((value & SOR_PWM_CTL_TRIGGER) == 0)
9906b6b6042SThierry Reding 			return 0;
9916b6b6042SThierry Reding 
9926b6b6042SThierry Reding 		usleep_range(25, 100);
9936b6b6042SThierry Reding 	}
9946b6b6042SThierry Reding 
9956b6b6042SThierry Reding 	return -ETIMEDOUT;
9966b6b6042SThierry Reding }
9976b6b6042SThierry Reding 
9986b6b6042SThierry Reding static int tegra_sor_attach(struct tegra_sor *sor)
9996b6b6042SThierry Reding {
10006b6b6042SThierry Reding 	unsigned long value, timeout;
10016b6b6042SThierry Reding 
10026b6b6042SThierry Reding 	/* wake up in normal mode */
1003a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
10046b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
10056b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_MODE_NORMAL;
1006a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
10076b6b6042SThierry Reding 	tegra_sor_super_update(sor);
10086b6b6042SThierry Reding 
10096b6b6042SThierry Reding 	/* attach */
1010a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
10116b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_ATTACHED;
1012a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
10136b6b6042SThierry Reding 	tegra_sor_super_update(sor);
10146b6b6042SThierry Reding 
10156b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
10166b6b6042SThierry Reding 
10176b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
10186b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
10196b6b6042SThierry Reding 		if ((value & SOR_TEST_ATTACHED) != 0)
10206b6b6042SThierry Reding 			return 0;
10216b6b6042SThierry Reding 
10226b6b6042SThierry Reding 		usleep_range(25, 100);
10236b6b6042SThierry Reding 	}
10246b6b6042SThierry Reding 
10256b6b6042SThierry Reding 	return -ETIMEDOUT;
10266b6b6042SThierry Reding }
10276b6b6042SThierry Reding 
10286b6b6042SThierry Reding static int tegra_sor_wakeup(struct tegra_sor *sor)
10296b6b6042SThierry Reding {
10306b6b6042SThierry Reding 	unsigned long value, timeout;
10316b6b6042SThierry Reding 
10326b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
10336b6b6042SThierry Reding 
10346b6b6042SThierry Reding 	/* wait for head to wake up */
10356b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
10366b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
10376b6b6042SThierry Reding 		value &= SOR_TEST_HEAD_MODE_MASK;
10386b6b6042SThierry Reding 
10396b6b6042SThierry Reding 		if (value == SOR_TEST_HEAD_MODE_AWAKE)
10406b6b6042SThierry Reding 			return 0;
10416b6b6042SThierry Reding 
10426b6b6042SThierry Reding 		usleep_range(25, 100);
10436b6b6042SThierry Reding 	}
10446b6b6042SThierry Reding 
10456b6b6042SThierry Reding 	return -ETIMEDOUT;
10466b6b6042SThierry Reding }
10476b6b6042SThierry Reding 
10486b6b6042SThierry Reding static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
10496b6b6042SThierry Reding {
105028fe2076SThierry Reding 	u32 value;
10516b6b6042SThierry Reding 
10526b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWR);
10536b6b6042SThierry Reding 	value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
10546b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWR);
10556b6b6042SThierry Reding 
10566b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
10576b6b6042SThierry Reding 
10586b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
10596b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
10606b6b6042SThierry Reding 		if ((value & SOR_PWR_TRIGGER) == 0)
10616b6b6042SThierry Reding 			return 0;
10626b6b6042SThierry Reding 
10636b6b6042SThierry Reding 		usleep_range(25, 100);
10646b6b6042SThierry Reding 	}
10656b6b6042SThierry Reding 
10666b6b6042SThierry Reding 	return -ETIMEDOUT;
10676b6b6042SThierry Reding }
10686b6b6042SThierry Reding 
106934fa183bSThierry Reding struct tegra_sor_params {
107034fa183bSThierry Reding 	/* number of link clocks per line */
107134fa183bSThierry Reding 	unsigned int num_clocks;
107234fa183bSThierry Reding 	/* ratio between input and output */
107334fa183bSThierry Reding 	u64 ratio;
107434fa183bSThierry Reding 	/* precision factor */
107534fa183bSThierry Reding 	u64 precision;
107634fa183bSThierry Reding 
107734fa183bSThierry Reding 	unsigned int active_polarity;
107834fa183bSThierry Reding 	unsigned int active_count;
107934fa183bSThierry Reding 	unsigned int active_frac;
108034fa183bSThierry Reding 	unsigned int tu_size;
108134fa183bSThierry Reding 	unsigned int error;
108234fa183bSThierry Reding };
108334fa183bSThierry Reding 
108434fa183bSThierry Reding static int tegra_sor_compute_params(struct tegra_sor *sor,
108534fa183bSThierry Reding 				    struct tegra_sor_params *params,
108634fa183bSThierry Reding 				    unsigned int tu_size)
108734fa183bSThierry Reding {
108834fa183bSThierry Reding 	u64 active_sym, active_count, frac, approx;
108934fa183bSThierry Reding 	u32 active_polarity, active_frac = 0;
109034fa183bSThierry Reding 	const u64 f = params->precision;
109134fa183bSThierry Reding 	s64 error;
109234fa183bSThierry Reding 
109334fa183bSThierry Reding 	active_sym = params->ratio * tu_size;
109434fa183bSThierry Reding 	active_count = div_u64(active_sym, f) * f;
109534fa183bSThierry Reding 	frac = active_sym - active_count;
109634fa183bSThierry Reding 
109734fa183bSThierry Reding 	/* fraction < 0.5 */
109834fa183bSThierry Reding 	if (frac >= (f / 2)) {
109934fa183bSThierry Reding 		active_polarity = 1;
110034fa183bSThierry Reding 		frac = f - frac;
110134fa183bSThierry Reding 	} else {
110234fa183bSThierry Reding 		active_polarity = 0;
110334fa183bSThierry Reding 	}
110434fa183bSThierry Reding 
110534fa183bSThierry Reding 	if (frac != 0) {
110634fa183bSThierry Reding 		frac = div_u64(f * f,  frac); /* 1/fraction */
110734fa183bSThierry Reding 		if (frac <= (15 * f)) {
110834fa183bSThierry Reding 			active_frac = div_u64(frac, f);
110934fa183bSThierry Reding 
111034fa183bSThierry Reding 			/* round up */
111134fa183bSThierry Reding 			if (active_polarity)
111234fa183bSThierry Reding 				active_frac++;
111334fa183bSThierry Reding 		} else {
111434fa183bSThierry Reding 			active_frac = active_polarity ? 1 : 15;
111534fa183bSThierry Reding 		}
111634fa183bSThierry Reding 	}
111734fa183bSThierry Reding 
111834fa183bSThierry Reding 	if (active_frac == 1)
111934fa183bSThierry Reding 		active_polarity = 0;
112034fa183bSThierry Reding 
112134fa183bSThierry Reding 	if (active_polarity == 1) {
112234fa183bSThierry Reding 		if (active_frac) {
112334fa183bSThierry Reding 			approx = active_count + (active_frac * (f - 1)) * f;
112434fa183bSThierry Reding 			approx = div_u64(approx, active_frac * f);
112534fa183bSThierry Reding 		} else {
112634fa183bSThierry Reding 			approx = active_count + f;
112734fa183bSThierry Reding 		}
112834fa183bSThierry Reding 	} else {
112934fa183bSThierry Reding 		if (active_frac)
113034fa183bSThierry Reding 			approx = active_count + div_u64(f, active_frac);
113134fa183bSThierry Reding 		else
113234fa183bSThierry Reding 			approx = active_count;
113334fa183bSThierry Reding 	}
113434fa183bSThierry Reding 
113534fa183bSThierry Reding 	error = div_s64(active_sym - approx, tu_size);
113634fa183bSThierry Reding 	error *= params->num_clocks;
113734fa183bSThierry Reding 
113879211c8eSAndrew Morton 	if (error <= 0 && abs(error) < params->error) {
113934fa183bSThierry Reding 		params->active_count = div_u64(active_count, f);
114034fa183bSThierry Reding 		params->active_polarity = active_polarity;
114134fa183bSThierry Reding 		params->active_frac = active_frac;
114279211c8eSAndrew Morton 		params->error = abs(error);
114334fa183bSThierry Reding 		params->tu_size = tu_size;
114434fa183bSThierry Reding 
114534fa183bSThierry Reding 		if (error == 0)
114634fa183bSThierry Reding 			return true;
114734fa183bSThierry Reding 	}
114834fa183bSThierry Reding 
114934fa183bSThierry Reding 	return false;
115034fa183bSThierry Reding }
115134fa183bSThierry Reding 
1152a198359eSThierry Reding static int tegra_sor_compute_config(struct tegra_sor *sor,
115380444495SThierry Reding 				    const struct drm_display_mode *mode,
115434fa183bSThierry Reding 				    struct tegra_sor_config *config,
115534fa183bSThierry Reding 				    struct drm_dp_link *link)
115634fa183bSThierry Reding {
115734fa183bSThierry Reding 	const u64 f = 100000, link_rate = link->rate * 1000;
115834fa183bSThierry Reding 	const u64 pclk = mode->clock * 1000;
11597890b576SThierry Reding 	u64 input, output, watermark, num;
116034fa183bSThierry Reding 	struct tegra_sor_params params;
116134fa183bSThierry Reding 	u32 num_syms_per_line;
116234fa183bSThierry Reding 	unsigned int i;
116334fa183bSThierry Reding 
1164c728e2d4SThierry Reding 	if (!link_rate || !link->lanes || !pclk || !config->bits_per_pixel)
116534fa183bSThierry Reding 		return -EINVAL;
116634fa183bSThierry Reding 
116734fa183bSThierry Reding 	input = pclk * config->bits_per_pixel;
1168c728e2d4SThierry Reding 	output = link_rate * 8 * link->lanes;
116934fa183bSThierry Reding 
117034fa183bSThierry Reding 	if (input >= output)
117134fa183bSThierry Reding 		return -ERANGE;
117234fa183bSThierry Reding 
117334fa183bSThierry Reding 	memset(&params, 0, sizeof(params));
117434fa183bSThierry Reding 	params.ratio = div64_u64(input * f, output);
117534fa183bSThierry Reding 	params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
117634fa183bSThierry Reding 	params.precision = f;
117734fa183bSThierry Reding 	params.error = 64 * f;
117834fa183bSThierry Reding 	params.tu_size = 64;
117934fa183bSThierry Reding 
118034fa183bSThierry Reding 	for (i = params.tu_size; i >= 32; i--)
118134fa183bSThierry Reding 		if (tegra_sor_compute_params(sor, &params, i))
118234fa183bSThierry Reding 			break;
118334fa183bSThierry Reding 
118434fa183bSThierry Reding 	if (params.active_frac == 0) {
118534fa183bSThierry Reding 		config->active_polarity = 0;
118634fa183bSThierry Reding 		config->active_count = params.active_count;
118734fa183bSThierry Reding 
118834fa183bSThierry Reding 		if (!params.active_polarity)
118934fa183bSThierry Reding 			config->active_count--;
119034fa183bSThierry Reding 
119134fa183bSThierry Reding 		config->tu_size = params.tu_size;
119234fa183bSThierry Reding 		config->active_frac = 1;
119334fa183bSThierry Reding 	} else {
119434fa183bSThierry Reding 		config->active_polarity = params.active_polarity;
119534fa183bSThierry Reding 		config->active_count = params.active_count;
119634fa183bSThierry Reding 		config->active_frac = params.active_frac;
119734fa183bSThierry Reding 		config->tu_size = params.tu_size;
119834fa183bSThierry Reding 	}
119934fa183bSThierry Reding 
120034fa183bSThierry Reding 	dev_dbg(sor->dev,
120134fa183bSThierry Reding 		"polarity: %d active count: %d tu size: %d active frac: %d\n",
120234fa183bSThierry Reding 		config->active_polarity, config->active_count,
120334fa183bSThierry Reding 		config->tu_size, config->active_frac);
120434fa183bSThierry Reding 
120534fa183bSThierry Reding 	watermark = params.ratio * config->tu_size * (f - params.ratio);
120634fa183bSThierry Reding 	watermark = div_u64(watermark, f);
120734fa183bSThierry Reding 
120834fa183bSThierry Reding 	watermark = div_u64(watermark + params.error, f);
120934fa183bSThierry Reding 	config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
121034fa183bSThierry Reding 	num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
1211c728e2d4SThierry Reding 			    (link->lanes * 8);
121234fa183bSThierry Reding 
121334fa183bSThierry Reding 	if (config->watermark > 30) {
121434fa183bSThierry Reding 		config->watermark = 30;
121534fa183bSThierry Reding 		dev_err(sor->dev,
121634fa183bSThierry Reding 			"unable to compute TU size, forcing watermark to %u\n",
121734fa183bSThierry Reding 			config->watermark);
121834fa183bSThierry Reding 	} else if (config->watermark > num_syms_per_line) {
121934fa183bSThierry Reding 		config->watermark = num_syms_per_line;
122034fa183bSThierry Reding 		dev_err(sor->dev, "watermark too high, forcing to %u\n",
122134fa183bSThierry Reding 			config->watermark);
122234fa183bSThierry Reding 	}
122334fa183bSThierry Reding 
12247890b576SThierry Reding 	/* compute the number of symbols per horizontal blanking interval */
12257890b576SThierry Reding 	num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
12267890b576SThierry Reding 	config->hblank_symbols = div_u64(num, pclk);
12277890b576SThierry Reding 
122827ba465cSThierry Reding 	if (link->caps.enhanced_framing)
12297890b576SThierry Reding 		config->hblank_symbols -= 3;
12307890b576SThierry Reding 
1231c728e2d4SThierry Reding 	config->hblank_symbols -= 12 / link->lanes;
12327890b576SThierry Reding 
12337890b576SThierry Reding 	/* compute the number of symbols per vertical blanking interval */
12347890b576SThierry Reding 	num = (mode->hdisplay - 25) * link_rate;
12357890b576SThierry Reding 	config->vblank_symbols = div_u64(num, pclk);
1236c728e2d4SThierry Reding 	config->vblank_symbols -= 36 / link->lanes + 4;
12377890b576SThierry Reding 
12387890b576SThierry Reding 	dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
12397890b576SThierry Reding 		config->vblank_symbols);
12407890b576SThierry Reding 
124134fa183bSThierry Reding 	return 0;
124234fa183bSThierry Reding }
124334fa183bSThierry Reding 
1244402f6bcdSThierry Reding static void tegra_sor_apply_config(struct tegra_sor *sor,
1245402f6bcdSThierry Reding 				   const struct tegra_sor_config *config)
1246402f6bcdSThierry Reding {
1247402f6bcdSThierry Reding 	u32 value;
1248402f6bcdSThierry Reding 
1249402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
1250402f6bcdSThierry Reding 	value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
1251402f6bcdSThierry Reding 	value |= SOR_DP_LINKCTL_TU_SIZE(config->tu_size);
1252402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
1253402f6bcdSThierry Reding 
1254402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_CONFIG0);
1255402f6bcdSThierry Reding 	value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
1256402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_WATERMARK(config->watermark);
1257402f6bcdSThierry Reding 
1258402f6bcdSThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
1259402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config->active_count);
1260402f6bcdSThierry Reding 
1261402f6bcdSThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
1262402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config->active_frac);
1263402f6bcdSThierry Reding 
1264402f6bcdSThierry Reding 	if (config->active_polarity)
1265402f6bcdSThierry Reding 		value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
1266402f6bcdSThierry Reding 	else
1267402f6bcdSThierry Reding 		value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
1268402f6bcdSThierry Reding 
1269402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
1270402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
1271402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_CONFIG0);
1272402f6bcdSThierry Reding 
1273402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
1274402f6bcdSThierry Reding 	value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
1275402f6bcdSThierry Reding 	value |= config->hblank_symbols & 0xffff;
1276402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
1277402f6bcdSThierry Reding 
1278402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
1279402f6bcdSThierry Reding 	value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
1280402f6bcdSThierry Reding 	value |= config->vblank_symbols & 0xffff;
1281402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
1282402f6bcdSThierry Reding }
1283402f6bcdSThierry Reding 
12842bd1dd39SThierry Reding static void tegra_sor_mode_set(struct tegra_sor *sor,
12852bd1dd39SThierry Reding 			       const struct drm_display_mode *mode,
1286c31efa7aSThierry Reding 			       struct tegra_sor_state *state)
12872bd1dd39SThierry Reding {
12882bd1dd39SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(sor->output.encoder.crtc);
12892bd1dd39SThierry Reding 	unsigned int vbe, vse, hbe, hse, vbs, hbs;
12902bd1dd39SThierry Reding 	u32 value;
12912bd1dd39SThierry Reding 
12922bd1dd39SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
12932bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_PIXELDEPTH_MASK;
12942bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
12952bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_OWNER_MASK;
12962bd1dd39SThierry Reding 
12972bd1dd39SThierry Reding 	value |= SOR_STATE_ASY_CRC_MODE_COMPLETE |
12982bd1dd39SThierry Reding 		 SOR_STATE_ASY_OWNER(dc->pipe + 1);
12992bd1dd39SThierry Reding 
13002bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PHSYNC)
13012bd1dd39SThierry Reding 		value &= ~SOR_STATE_ASY_HSYNCPOL;
13022bd1dd39SThierry Reding 
13032bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
13042bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_HSYNCPOL;
13052bd1dd39SThierry Reding 
13062bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PVSYNC)
13072bd1dd39SThierry Reding 		value &= ~SOR_STATE_ASY_VSYNCPOL;
13082bd1dd39SThierry Reding 
13092bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
13102bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_VSYNCPOL;
13112bd1dd39SThierry Reding 
1312c31efa7aSThierry Reding 	switch (state->bpc) {
1313c31efa7aSThierry Reding 	case 16:
1314c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_48_444;
1315c31efa7aSThierry Reding 		break;
1316c31efa7aSThierry Reding 
1317c31efa7aSThierry Reding 	case 12:
1318c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_36_444;
1319c31efa7aSThierry Reding 		break;
1320c31efa7aSThierry Reding 
1321c31efa7aSThierry Reding 	case 10:
1322c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_30_444;
1323c31efa7aSThierry Reding 		break;
1324c31efa7aSThierry Reding 
13252bd1dd39SThierry Reding 	case 8:
13262bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
13272bd1dd39SThierry Reding 		break;
13282bd1dd39SThierry Reding 
13292bd1dd39SThierry Reding 	case 6:
13302bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
13312bd1dd39SThierry Reding 		break;
13322bd1dd39SThierry Reding 
13332bd1dd39SThierry Reding 	default:
1334c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
13352bd1dd39SThierry Reding 		break;
13362bd1dd39SThierry Reding 	}
13372bd1dd39SThierry Reding 
13382bd1dd39SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
13392bd1dd39SThierry Reding 
13402bd1dd39SThierry Reding 	/*
13412bd1dd39SThierry Reding 	 * TODO: The video timing programming below doesn't seem to match the
13422bd1dd39SThierry Reding 	 * register definitions.
13432bd1dd39SThierry Reding 	 */
13442bd1dd39SThierry Reding 
13452bd1dd39SThierry Reding 	value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
1346880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state1 + dc->pipe);
13472bd1dd39SThierry Reding 
13482bd1dd39SThierry Reding 	/* sync end = sync width - 1 */
13492bd1dd39SThierry Reding 	vse = mode->vsync_end - mode->vsync_start - 1;
13502bd1dd39SThierry Reding 	hse = mode->hsync_end - mode->hsync_start - 1;
13512bd1dd39SThierry Reding 
13522bd1dd39SThierry Reding 	value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
1353880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state2 + dc->pipe);
13542bd1dd39SThierry Reding 
13552bd1dd39SThierry Reding 	/* blank end = sync end + back porch */
13562bd1dd39SThierry Reding 	vbe = vse + (mode->vtotal - mode->vsync_end);
13572bd1dd39SThierry Reding 	hbe = hse + (mode->htotal - mode->hsync_end);
13582bd1dd39SThierry Reding 
13592bd1dd39SThierry Reding 	value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
1360880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state3 + dc->pipe);
13612bd1dd39SThierry Reding 
13622bd1dd39SThierry Reding 	/* blank start = blank end + active */
13632bd1dd39SThierry Reding 	vbs = vbe + mode->vdisplay;
13642bd1dd39SThierry Reding 	hbs = hbe + mode->hdisplay;
13652bd1dd39SThierry Reding 
13662bd1dd39SThierry Reding 	value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
1367880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state4 + dc->pipe);
13682bd1dd39SThierry Reding 
13692bd1dd39SThierry Reding 	/* XXX interlacing support */
1370880cee0bSThierry Reding 	tegra_sor_writel(sor, 0x001, sor->soc->regs->head_state5 + dc->pipe);
13712bd1dd39SThierry Reding }
13722bd1dd39SThierry Reding 
13736fad8f66SThierry Reding static int tegra_sor_detach(struct tegra_sor *sor)
13746b6b6042SThierry Reding {
13756fad8f66SThierry Reding 	unsigned long value, timeout;
13766fad8f66SThierry Reding 
13776fad8f66SThierry Reding 	/* switch to safe mode */
1378a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
13796fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_MODE_NORMAL;
1380a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
13816fad8f66SThierry Reding 	tegra_sor_super_update(sor);
13826fad8f66SThierry Reding 
13836fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
13846fad8f66SThierry Reding 
13856fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
13866fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
13876fad8f66SThierry Reding 		if (value & SOR_PWR_MODE_SAFE)
13886fad8f66SThierry Reding 			break;
13896fad8f66SThierry Reding 	}
13906fad8f66SThierry Reding 
13916fad8f66SThierry Reding 	if ((value & SOR_PWR_MODE_SAFE) == 0)
13926fad8f66SThierry Reding 		return -ETIMEDOUT;
13936fad8f66SThierry Reding 
13946fad8f66SThierry Reding 	/* go to sleep */
1395a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
13966fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
1397a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
13986fad8f66SThierry Reding 	tegra_sor_super_update(sor);
13996fad8f66SThierry Reding 
14006fad8f66SThierry Reding 	/* detach */
1401a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
14026fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_ATTACHED;
1403a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
14046fad8f66SThierry Reding 	tegra_sor_super_update(sor);
14056fad8f66SThierry Reding 
14066fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
14076fad8f66SThierry Reding 
14086fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
14096fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
14106fad8f66SThierry Reding 		if ((value & SOR_TEST_ATTACHED) == 0)
14116fad8f66SThierry Reding 			break;
14126fad8f66SThierry Reding 
14136fad8f66SThierry Reding 		usleep_range(25, 100);
14146fad8f66SThierry Reding 	}
14156fad8f66SThierry Reding 
14166fad8f66SThierry Reding 	if ((value & SOR_TEST_ATTACHED) != 0)
14176fad8f66SThierry Reding 		return -ETIMEDOUT;
14186fad8f66SThierry Reding 
14196fad8f66SThierry Reding 	return 0;
14206fad8f66SThierry Reding }
14216fad8f66SThierry Reding 
14226fad8f66SThierry Reding static int tegra_sor_power_down(struct tegra_sor *sor)
14236fad8f66SThierry Reding {
14246fad8f66SThierry Reding 	unsigned long value, timeout;
14256fad8f66SThierry Reding 	int err;
14266fad8f66SThierry Reding 
14276fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWR);
14286fad8f66SThierry Reding 	value &= ~SOR_PWR_NORMAL_STATE_PU;
14296fad8f66SThierry Reding 	value |= SOR_PWR_TRIGGER;
14306fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWR);
14316fad8f66SThierry Reding 
14326fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
14336fad8f66SThierry Reding 
14346fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
14356fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
14366fad8f66SThierry Reding 		if ((value & SOR_PWR_TRIGGER) == 0)
14376fad8f66SThierry Reding 			return 0;
14386fad8f66SThierry Reding 
14396fad8f66SThierry Reding 		usleep_range(25, 100);
14406fad8f66SThierry Reding 	}
14416fad8f66SThierry Reding 
14426fad8f66SThierry Reding 	if ((value & SOR_PWR_TRIGGER) != 0)
14436fad8f66SThierry Reding 		return -ETIMEDOUT;
14446fad8f66SThierry Reding 
144525bb2cecSThierry Reding 	/* switch to safe parent clock */
144625bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
1447e1335e2fSThierry Reding 	if (err < 0) {
14486fad8f66SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
1449e1335e2fSThierry Reding 		return err;
1450e1335e2fSThierry Reding 	}
14516fad8f66SThierry Reding 
1452880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1453a9a9e4fdSThierry Reding 	value |= SOR_PLL2_PORT_POWERDOWN;
1454880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
14556fad8f66SThierry Reding 
14566fad8f66SThierry Reding 	usleep_range(20, 100);
14576fad8f66SThierry Reding 
1458880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
1459a9a9e4fdSThierry Reding 	value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
1460880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
14616fad8f66SThierry Reding 
1462880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1463a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD;
1464a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1465880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
14666fad8f66SThierry Reding 
14676fad8f66SThierry Reding 	usleep_range(20, 100);
14686fad8f66SThierry Reding 
14696fad8f66SThierry Reding 	return 0;
14706fad8f66SThierry Reding }
14716fad8f66SThierry Reding 
14726fad8f66SThierry Reding static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
14736fad8f66SThierry Reding {
14746fad8f66SThierry Reding 	u32 value;
14756fad8f66SThierry Reding 
14766fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
14776fad8f66SThierry Reding 
14786fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
1479a9a9e4fdSThierry Reding 		value = tegra_sor_readl(sor, SOR_CRCA);
1480a9a9e4fdSThierry Reding 		if (value & SOR_CRCA_VALID)
14816fad8f66SThierry Reding 			return 0;
14826fad8f66SThierry Reding 
14836fad8f66SThierry Reding 		usleep_range(100, 200);
14846fad8f66SThierry Reding 	}
14856fad8f66SThierry Reding 
14866fad8f66SThierry Reding 	return -ETIMEDOUT;
14876fad8f66SThierry Reding }
14886fad8f66SThierry Reding 
1489530239a8SThierry Reding static int tegra_sor_show_crc(struct seq_file *s, void *data)
14906fad8f66SThierry Reding {
1491530239a8SThierry Reding 	struct drm_info_node *node = s->private;
1492530239a8SThierry Reding 	struct tegra_sor *sor = node->info_ent->data;
1493850bab44SThierry Reding 	struct drm_crtc *crtc = sor->output.encoder.crtc;
1494850bab44SThierry Reding 	struct drm_device *drm = node->minor->dev;
1495530239a8SThierry Reding 	int err = 0;
14966fad8f66SThierry Reding 	u32 value;
14976fad8f66SThierry Reding 
1498850bab44SThierry Reding 	drm_modeset_lock_all(drm);
14996fad8f66SThierry Reding 
1500850bab44SThierry Reding 	if (!crtc || !crtc->state->active) {
1501850bab44SThierry Reding 		err = -EBUSY;
15026fad8f66SThierry Reding 		goto unlock;
15036fad8f66SThierry Reding 	}
15046fad8f66SThierry Reding 
1505a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
15066fad8f66SThierry Reding 	value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
1507a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
15086fad8f66SThierry Reding 
15096fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
15106fad8f66SThierry Reding 	value |= SOR_CRC_CNTRL_ENABLE;
15116fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
15126fad8f66SThierry Reding 
15136fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_TEST);
15146fad8f66SThierry Reding 	value &= ~SOR_TEST_CRC_POST_SERIALIZE;
15156fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_TEST);
15166fad8f66SThierry Reding 
15176fad8f66SThierry Reding 	err = tegra_sor_crc_wait(sor, 100);
15186fad8f66SThierry Reding 	if (err < 0)
15196fad8f66SThierry Reding 		goto unlock;
15206fad8f66SThierry Reding 
1521a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, SOR_CRCA_RESET, SOR_CRCA);
1522a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_CRCB);
15236fad8f66SThierry Reding 
1524530239a8SThierry Reding 	seq_printf(s, "%08x\n", value);
15256fad8f66SThierry Reding 
15266fad8f66SThierry Reding unlock:
1527850bab44SThierry Reding 	drm_modeset_unlock_all(drm);
15286fad8f66SThierry Reding 	return err;
15296fad8f66SThierry Reding }
15306fad8f66SThierry Reding 
1531062f5b2cSThierry Reding #define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
1532062f5b2cSThierry Reding 
1533062f5b2cSThierry Reding static const struct debugfs_reg32 tegra_sor_regs[] = {
1534062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CTXSW),
1535062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SUPER_STATE0),
1536062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SUPER_STATE1),
1537062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_STATE0),
1538062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_STATE1),
1539062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE0(0)),
1540062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE0(1)),
1541062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE1(0)),
1542062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE1(1)),
1543062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE2(0)),
1544062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE2(1)),
1545062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE3(0)),
1546062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE3(1)),
1547062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE4(0)),
1548062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE4(1)),
1549062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE5(0)),
1550062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE5(1)),
1551062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CRC_CNTRL),
1552062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_DEBUG_MVID),
1553062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CLK_CNTRL),
1554062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CAP),
1555062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PWR),
1556062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_TEST),
1557062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL0),
1558062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL1),
1559062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL2),
1560062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL3),
1561062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CSTM),
1562062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LVDS),
1563062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CRCA),
1564062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CRCB),
1565062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_BLANK),
1566062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_CTL),
1567062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_SEQ_CTL),
1568062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(0)),
1569062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(1)),
1570062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(2)),
1571062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(3)),
1572062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(4)),
1573062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(5)),
1574062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(6)),
1575062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(7)),
1576062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(8)),
1577062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(9)),
1578062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(10)),
1579062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(11)),
1580062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(12)),
1581062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(13)),
1582062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(14)),
1583062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(15)),
1584062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PWM_DIV),
1585062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PWM_CTL),
1586062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_A0),
1587062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_A1),
1588062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_B0),
1589062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_B1),
1590062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_A0),
1591062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_A1),
1592062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_B0),
1593062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_B1),
1594062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_A0),
1595062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_A1),
1596062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_B0),
1597062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_B1),
1598062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_A0),
1599062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_A1),
1600062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_B0),
1601062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_B1),
1602062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_A0),
1603062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_A1),
1604062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_B0),
1605062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_B1),
1606062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_TRIG),
1607062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_MSCHECK),
1608062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_XBAR_CTRL),
1609062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_XBAR_POL),
1610062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LINKCTL0),
1611062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LINKCTL1),
1612062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT0),
1613062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT1),
1614062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT0),
1615062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT1),
1616062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_PREEMPHASIS0),
1617062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_PREEMPHASIS1),
1618062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS0),
1619062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS1),
1620062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_POSTCURSOR0),
1621062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_POSTCURSOR1),
1622062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_CONFIG0),
1623062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_CONFIG1),
1624062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_MN0),
1625062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_MN1),
1626062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_PADCTL0),
1627062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_PADCTL1),
1628c57997bcSThierry Reding 	DEBUGFS_REG32(SOR_DP_PADCTL2),
1629062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_DEBUG0),
1630062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_DEBUG1),
1631062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_SPARE0),
1632062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_SPARE1),
1633062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_AUDIO_CTRL),
1634062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_AUDIO_HBLANK_SYMBOLS),
1635062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_AUDIO_VBLANK_SYMBOLS),
1636062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_HEADER),
1637062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK0),
1638062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK1),
1639062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK2),
1640062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK3),
1641062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK4),
1642062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK5),
1643062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK6),
1644062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_TPG),
1645062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_TPG_CONFIG),
1646062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LQ_CSTM0),
1647062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LQ_CSTM1),
1648062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LQ_CSTM2),
1649062f5b2cSThierry Reding };
1650062f5b2cSThierry Reding 
1651dab16336SThierry Reding static int tegra_sor_show_regs(struct seq_file *s, void *data)
1652dab16336SThierry Reding {
1653dab16336SThierry Reding 	struct drm_info_node *node = s->private;
1654dab16336SThierry Reding 	struct tegra_sor *sor = node->info_ent->data;
1655850bab44SThierry Reding 	struct drm_crtc *crtc = sor->output.encoder.crtc;
1656850bab44SThierry Reding 	struct drm_device *drm = node->minor->dev;
1657062f5b2cSThierry Reding 	unsigned int i;
1658850bab44SThierry Reding 	int err = 0;
1659850bab44SThierry Reding 
1660850bab44SThierry Reding 	drm_modeset_lock_all(drm);
1661850bab44SThierry Reding 
1662850bab44SThierry Reding 	if (!crtc || !crtc->state->active) {
1663850bab44SThierry Reding 		err = -EBUSY;
1664850bab44SThierry Reding 		goto unlock;
1665850bab44SThierry Reding 	}
1666dab16336SThierry Reding 
1667062f5b2cSThierry Reding 	for (i = 0; i < ARRAY_SIZE(tegra_sor_regs); i++) {
1668062f5b2cSThierry Reding 		unsigned int offset = tegra_sor_regs[i].offset;
1669dab16336SThierry Reding 
1670062f5b2cSThierry Reding 		seq_printf(s, "%-38s %#05x %08x\n", tegra_sor_regs[i].name,
1671062f5b2cSThierry Reding 			   offset, tegra_sor_readl(sor, offset));
1672062f5b2cSThierry Reding 	}
1673dab16336SThierry Reding 
1674850bab44SThierry Reding unlock:
1675850bab44SThierry Reding 	drm_modeset_unlock_all(drm);
1676850bab44SThierry Reding 	return err;
1677dab16336SThierry Reding }
1678dab16336SThierry Reding 
1679dab16336SThierry Reding static const struct drm_info_list debugfs_files[] = {
1680530239a8SThierry Reding 	{ "crc", tegra_sor_show_crc, 0, NULL },
1681dab16336SThierry Reding 	{ "regs", tegra_sor_show_regs, 0, NULL },
1682dab16336SThierry Reding };
1683dab16336SThierry Reding 
16845b8e043bSThierry Reding static int tegra_sor_late_register(struct drm_connector *connector)
16856fad8f66SThierry Reding {
16865b8e043bSThierry Reding 	struct tegra_output *output = connector_to_output(connector);
16875b8e043bSThierry Reding 	unsigned int i, count = ARRAY_SIZE(debugfs_files);
16885b8e043bSThierry Reding 	struct drm_minor *minor = connector->dev->primary;
16895b8e043bSThierry Reding 	struct dentry *root = connector->debugfs_entry;
16905b8e043bSThierry Reding 	struct tegra_sor *sor = to_sor(output);
16916fad8f66SThierry Reding 
1692dab16336SThierry Reding 	sor->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1693dab16336SThierry Reding 				     GFP_KERNEL);
16945b8e043bSThierry Reding 	if (!sor->debugfs_files)
16955b8e043bSThierry Reding 		return -ENOMEM;
16966fad8f66SThierry Reding 
16975b8e043bSThierry Reding 	for (i = 0; i < count; i++)
1698dab16336SThierry Reding 		sor->debugfs_files[i].data = sor;
1699dab16336SThierry Reding 
1700ad6d94f2SWambui Karuga 	drm_debugfs_create_files(sor->debugfs_files, count, root, minor);
1701dab16336SThierry Reding 
1702530239a8SThierry Reding 	return 0;
17036fad8f66SThierry Reding }
17046fad8f66SThierry Reding 
17055b8e043bSThierry Reding static void tegra_sor_early_unregister(struct drm_connector *connector)
17066fad8f66SThierry Reding {
17075b8e043bSThierry Reding 	struct tegra_output *output = connector_to_output(connector);
17085b8e043bSThierry Reding 	unsigned int count = ARRAY_SIZE(debugfs_files);
17095b8e043bSThierry Reding 	struct tegra_sor *sor = to_sor(output);
1710d92e6009SThierry Reding 
17115b8e043bSThierry Reding 	drm_debugfs_remove_files(sor->debugfs_files, count,
17125b8e043bSThierry Reding 				 connector->dev->primary);
1713dab16336SThierry Reding 	kfree(sor->debugfs_files);
1714066d30f8SThierry Reding 	sor->debugfs_files = NULL;
17156fad8f66SThierry Reding }
17166fad8f66SThierry Reding 
1717c31efa7aSThierry Reding static void tegra_sor_connector_reset(struct drm_connector *connector)
1718c31efa7aSThierry Reding {
1719c31efa7aSThierry Reding 	struct tegra_sor_state *state;
1720c31efa7aSThierry Reding 
1721c31efa7aSThierry Reding 	state = kzalloc(sizeof(*state), GFP_KERNEL);
1722c31efa7aSThierry Reding 	if (!state)
1723c31efa7aSThierry Reding 		return;
1724c31efa7aSThierry Reding 
1725c31efa7aSThierry Reding 	if (connector->state) {
1726c31efa7aSThierry Reding 		__drm_atomic_helper_connector_destroy_state(connector->state);
1727c31efa7aSThierry Reding 		kfree(connector->state);
1728c31efa7aSThierry Reding 	}
1729c31efa7aSThierry Reding 
1730c31efa7aSThierry Reding 	__drm_atomic_helper_connector_reset(connector, &state->base);
1731c31efa7aSThierry Reding }
1732c31efa7aSThierry Reding 
17336fad8f66SThierry Reding static enum drm_connector_status
17346fad8f66SThierry Reding tegra_sor_connector_detect(struct drm_connector *connector, bool force)
17356fad8f66SThierry Reding {
17366fad8f66SThierry Reding 	struct tegra_output *output = connector_to_output(connector);
17376fad8f66SThierry Reding 	struct tegra_sor *sor = to_sor(output);
17386fad8f66SThierry Reding 
17399542c237SThierry Reding 	if (sor->aux)
17409542c237SThierry Reding 		return drm_dp_aux_detect(sor->aux);
17416fad8f66SThierry Reding 
1742459cc2c6SThierry Reding 	return tegra_output_connector_detect(connector, force);
17436fad8f66SThierry Reding }
17446fad8f66SThierry Reding 
1745c31efa7aSThierry Reding static struct drm_connector_state *
1746c31efa7aSThierry Reding tegra_sor_connector_duplicate_state(struct drm_connector *connector)
1747c31efa7aSThierry Reding {
1748c31efa7aSThierry Reding 	struct tegra_sor_state *state = to_sor_state(connector->state);
1749c31efa7aSThierry Reding 	struct tegra_sor_state *copy;
1750c31efa7aSThierry Reding 
1751c31efa7aSThierry Reding 	copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
1752c31efa7aSThierry Reding 	if (!copy)
1753c31efa7aSThierry Reding 		return NULL;
1754c31efa7aSThierry Reding 
1755c31efa7aSThierry Reding 	__drm_atomic_helper_connector_duplicate_state(connector, &copy->base);
1756c31efa7aSThierry Reding 
1757c31efa7aSThierry Reding 	return &copy->base;
1758c31efa7aSThierry Reding }
1759c31efa7aSThierry Reding 
17606fad8f66SThierry Reding static const struct drm_connector_funcs tegra_sor_connector_funcs = {
1761c31efa7aSThierry Reding 	.reset = tegra_sor_connector_reset,
17626fad8f66SThierry Reding 	.detect = tegra_sor_connector_detect,
17636fad8f66SThierry Reding 	.fill_modes = drm_helper_probe_single_connector_modes,
17646fad8f66SThierry Reding 	.destroy = tegra_output_connector_destroy,
1765c31efa7aSThierry Reding 	.atomic_duplicate_state = tegra_sor_connector_duplicate_state,
17664aa3df71SThierry Reding 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
17675b8e043bSThierry Reding 	.late_register = tegra_sor_late_register,
17685b8e043bSThierry Reding 	.early_unregister = tegra_sor_early_unregister,
17696fad8f66SThierry Reding };
17706fad8f66SThierry Reding 
17716fad8f66SThierry Reding static int tegra_sor_connector_get_modes(struct drm_connector *connector)
17726fad8f66SThierry Reding {
17736fad8f66SThierry Reding 	struct tegra_output *output = connector_to_output(connector);
17746fad8f66SThierry Reding 	struct tegra_sor *sor = to_sor(output);
17756fad8f66SThierry Reding 	int err;
17766fad8f66SThierry Reding 
17779542c237SThierry Reding 	if (sor->aux)
17789542c237SThierry Reding 		drm_dp_aux_enable(sor->aux);
17796fad8f66SThierry Reding 
17806fad8f66SThierry Reding 	err = tegra_output_connector_get_modes(connector);
17816fad8f66SThierry Reding 
17829542c237SThierry Reding 	if (sor->aux)
17839542c237SThierry Reding 		drm_dp_aux_disable(sor->aux);
17846fad8f66SThierry Reding 
17856fad8f66SThierry Reding 	return err;
17866fad8f66SThierry Reding }
17876fad8f66SThierry Reding 
17886fad8f66SThierry Reding static enum drm_mode_status
17896fad8f66SThierry Reding tegra_sor_connector_mode_valid(struct drm_connector *connector,
17906fad8f66SThierry Reding 			       struct drm_display_mode *mode)
17916fad8f66SThierry Reding {
17926fad8f66SThierry Reding 	return MODE_OK;
17936fad8f66SThierry Reding }
17946fad8f66SThierry Reding 
17956fad8f66SThierry Reding static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
17966fad8f66SThierry Reding 	.get_modes = tegra_sor_connector_get_modes,
17976fad8f66SThierry Reding 	.mode_valid = tegra_sor_connector_mode_valid,
17986fad8f66SThierry Reding };
17996fad8f66SThierry Reding 
180082f1511cSThierry Reding static int
180182f1511cSThierry Reding tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
180282f1511cSThierry Reding 			       struct drm_crtc_state *crtc_state,
180382f1511cSThierry Reding 			       struct drm_connector_state *conn_state)
180482f1511cSThierry Reding {
180582f1511cSThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
1806c31efa7aSThierry Reding 	struct tegra_sor_state *state = to_sor_state(conn_state);
180782f1511cSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
180882f1511cSThierry Reding 	unsigned long pclk = crtc_state->mode.clock * 1000;
180982f1511cSThierry Reding 	struct tegra_sor *sor = to_sor(output);
1810c31efa7aSThierry Reding 	struct drm_display_info *info;
181182f1511cSThierry Reding 	int err;
181282f1511cSThierry Reding 
1813c31efa7aSThierry Reding 	info = &output->connector.display_info;
1814c31efa7aSThierry Reding 
181536e90221SThierry Reding 	/*
181636e90221SThierry Reding 	 * For HBR2 modes, the SOR brick needs to use the x20 multiplier, so
181736e90221SThierry Reding 	 * the pixel clock must be corrected accordingly.
181836e90221SThierry Reding 	 */
181936e90221SThierry Reding 	if (pclk >= 340000000) {
182036e90221SThierry Reding 		state->link_speed = 20;
182136e90221SThierry Reding 		state->pclk = pclk / 2;
182236e90221SThierry Reding 	} else {
182336e90221SThierry Reding 		state->link_speed = 10;
182436e90221SThierry Reding 		state->pclk = pclk;
182536e90221SThierry Reding 	}
182636e90221SThierry Reding 
182782f1511cSThierry Reding 	err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
182882f1511cSThierry Reding 					 pclk, 0);
182982f1511cSThierry Reding 	if (err < 0) {
183082f1511cSThierry Reding 		dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
183182f1511cSThierry Reding 		return err;
183282f1511cSThierry Reding 	}
183382f1511cSThierry Reding 
1834c31efa7aSThierry Reding 	switch (info->bpc) {
1835c31efa7aSThierry Reding 	case 8:
1836c31efa7aSThierry Reding 	case 6:
1837c31efa7aSThierry Reding 		state->bpc = info->bpc;
1838c31efa7aSThierry Reding 		break;
1839c31efa7aSThierry Reding 
1840c31efa7aSThierry Reding 	default:
1841c31efa7aSThierry Reding 		DRM_DEBUG_KMS("%u bits-per-color not supported\n", info->bpc);
1842c31efa7aSThierry Reding 		state->bpc = 8;
1843c31efa7aSThierry Reding 		break;
1844c31efa7aSThierry Reding 	}
1845c31efa7aSThierry Reding 
184682f1511cSThierry Reding 	return 0;
184782f1511cSThierry Reding }
184882f1511cSThierry Reding 
1849459cc2c6SThierry Reding static inline u32 tegra_sor_hdmi_subpack(const u8 *ptr, size_t size)
1850459cc2c6SThierry Reding {
1851459cc2c6SThierry Reding 	u32 value = 0;
1852459cc2c6SThierry Reding 	size_t i;
1853459cc2c6SThierry Reding 
1854459cc2c6SThierry Reding 	for (i = size; i > 0; i--)
1855459cc2c6SThierry Reding 		value = (value << 8) | ptr[i - 1];
1856459cc2c6SThierry Reding 
1857459cc2c6SThierry Reding 	return value;
1858459cc2c6SThierry Reding }
1859459cc2c6SThierry Reding 
1860459cc2c6SThierry Reding static void tegra_sor_hdmi_write_infopack(struct tegra_sor *sor,
1861459cc2c6SThierry Reding 					  const void *data, size_t size)
1862459cc2c6SThierry Reding {
1863459cc2c6SThierry Reding 	const u8 *ptr = data;
1864459cc2c6SThierry Reding 	unsigned long offset;
1865459cc2c6SThierry Reding 	size_t i, j;
1866459cc2c6SThierry Reding 	u32 value;
1867459cc2c6SThierry Reding 
1868459cc2c6SThierry Reding 	switch (ptr[0]) {
1869459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_AVI:
1870459cc2c6SThierry Reding 		offset = SOR_HDMI_AVI_INFOFRAME_HEADER;
1871459cc2c6SThierry Reding 		break;
1872459cc2c6SThierry Reding 
1873459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_AUDIO:
1874459cc2c6SThierry Reding 		offset = SOR_HDMI_AUDIO_INFOFRAME_HEADER;
1875459cc2c6SThierry Reding 		break;
1876459cc2c6SThierry Reding 
1877459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_VENDOR:
1878459cc2c6SThierry Reding 		offset = SOR_HDMI_VSI_INFOFRAME_HEADER;
1879459cc2c6SThierry Reding 		break;
1880459cc2c6SThierry Reding 
1881459cc2c6SThierry Reding 	default:
1882459cc2c6SThierry Reding 		dev_err(sor->dev, "unsupported infoframe type: %02x\n",
1883459cc2c6SThierry Reding 			ptr[0]);
1884459cc2c6SThierry Reding 		return;
1885459cc2c6SThierry Reding 	}
1886459cc2c6SThierry Reding 
1887459cc2c6SThierry Reding 	value = INFOFRAME_HEADER_TYPE(ptr[0]) |
1888459cc2c6SThierry Reding 		INFOFRAME_HEADER_VERSION(ptr[1]) |
1889459cc2c6SThierry Reding 		INFOFRAME_HEADER_LEN(ptr[2]);
1890459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, offset);
1891459cc2c6SThierry Reding 	offset++;
1892459cc2c6SThierry Reding 
1893459cc2c6SThierry Reding 	/*
1894459cc2c6SThierry Reding 	 * Each subpack contains 7 bytes, divided into:
1895459cc2c6SThierry Reding 	 * - subpack_low: bytes 0 - 3
1896459cc2c6SThierry Reding 	 * - subpack_high: bytes 4 - 6 (with byte 7 padded to 0x00)
1897459cc2c6SThierry Reding 	 */
1898459cc2c6SThierry Reding 	for (i = 3, j = 0; i < size; i += 7, j += 8) {
1899459cc2c6SThierry Reding 		size_t rem = size - i, num = min_t(size_t, rem, 4);
1900459cc2c6SThierry Reding 
1901459cc2c6SThierry Reding 		value = tegra_sor_hdmi_subpack(&ptr[i], num);
1902459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, offset++);
1903459cc2c6SThierry Reding 
1904459cc2c6SThierry Reding 		num = min_t(size_t, rem - num, 3);
1905459cc2c6SThierry Reding 
1906459cc2c6SThierry Reding 		value = tegra_sor_hdmi_subpack(&ptr[i + 4], num);
1907459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, offset++);
1908459cc2c6SThierry Reding 	}
1909459cc2c6SThierry Reding }
1910459cc2c6SThierry Reding 
1911459cc2c6SThierry Reding static int
1912459cc2c6SThierry Reding tegra_sor_hdmi_setup_avi_infoframe(struct tegra_sor *sor,
1913459cc2c6SThierry Reding 				   const struct drm_display_mode *mode)
1914459cc2c6SThierry Reding {
1915459cc2c6SThierry Reding 	u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
1916459cc2c6SThierry Reding 	struct hdmi_avi_infoframe frame;
1917459cc2c6SThierry Reding 	u32 value;
1918459cc2c6SThierry Reding 	int err;
1919459cc2c6SThierry Reding 
1920459cc2c6SThierry Reding 	/* disable AVI infoframe */
1921459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
1922459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_SINGLE;
1923459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_OTHER;
1924459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_ENABLE;
1925459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
1926459cc2c6SThierry Reding 
192713d0add3SVille Syrjälä 	err = drm_hdmi_avi_infoframe_from_display_mode(&frame,
192813d0add3SVille Syrjälä 						       &sor->output.connector, mode);
1929459cc2c6SThierry Reding 	if (err < 0) {
1930459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
1931459cc2c6SThierry Reding 		return err;
1932459cc2c6SThierry Reding 	}
1933459cc2c6SThierry Reding 
1934459cc2c6SThierry Reding 	err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
1935459cc2c6SThierry Reding 	if (err < 0) {
1936459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to pack AVI infoframe: %d\n", err);
1937459cc2c6SThierry Reding 		return err;
1938459cc2c6SThierry Reding 	}
1939459cc2c6SThierry Reding 
1940459cc2c6SThierry Reding 	tegra_sor_hdmi_write_infopack(sor, buffer, err);
1941459cc2c6SThierry Reding 
1942459cc2c6SThierry Reding 	/* enable AVI infoframe */
1943459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
1944459cc2c6SThierry Reding 	value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
1945459cc2c6SThierry Reding 	value |= INFOFRAME_CTRL_ENABLE;
1946459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
1947459cc2c6SThierry Reding 
1948459cc2c6SThierry Reding 	return 0;
1949459cc2c6SThierry Reding }
1950459cc2c6SThierry Reding 
19518e2988a7SThierry Reding static void tegra_sor_write_eld(struct tegra_sor *sor)
19528e2988a7SThierry Reding {
19538e2988a7SThierry Reding 	size_t length = drm_eld_size(sor->output.connector.eld), i;
19548e2988a7SThierry Reding 
19558e2988a7SThierry Reding 	for (i = 0; i < length; i++)
19568e2988a7SThierry Reding 		tegra_sor_writel(sor, i << 8 | sor->output.connector.eld[i],
19578e2988a7SThierry Reding 				 SOR_AUDIO_HDA_ELD_BUFWR);
19588e2988a7SThierry Reding 
19598e2988a7SThierry Reding 	/*
19608e2988a7SThierry Reding 	 * The HDA codec will always report an ELD buffer size of 96 bytes and
19618e2988a7SThierry Reding 	 * the HDA codec driver will check that each byte read from the buffer
19628e2988a7SThierry Reding 	 * is valid. Therefore every byte must be written, even if no 96 bytes
19638e2988a7SThierry Reding 	 * were parsed from EDID.
19648e2988a7SThierry Reding 	 */
19658e2988a7SThierry Reding 	for (i = length; i < 96; i++)
19668e2988a7SThierry Reding 		tegra_sor_writel(sor, i << 8 | 0, SOR_AUDIO_HDA_ELD_BUFWR);
19678e2988a7SThierry Reding }
19688e2988a7SThierry Reding 
19698e2988a7SThierry Reding static void tegra_sor_audio_prepare(struct tegra_sor *sor)
19708e2988a7SThierry Reding {
19718e2988a7SThierry Reding 	u32 value;
19728e2988a7SThierry Reding 
1973f1f20eb9SThierry Reding 	/*
1974f1f20eb9SThierry Reding 	 * Enable and unmask the HDA codec SCRATCH0 register interrupt. This
1975f1f20eb9SThierry Reding 	 * is used for interoperability between the HDA codec driver and the
1976f1f20eb9SThierry Reding 	 * HDMI/DP driver.
1977f1f20eb9SThierry Reding 	 */
1978f1f20eb9SThierry Reding 	value = SOR_INT_CODEC_SCRATCH1 | SOR_INT_CODEC_SCRATCH0;
1979f1f20eb9SThierry Reding 	tegra_sor_writel(sor, value, SOR_INT_ENABLE);
1980f1f20eb9SThierry Reding 	tegra_sor_writel(sor, value, SOR_INT_MASK);
1981f1f20eb9SThierry Reding 
19828e2988a7SThierry Reding 	tegra_sor_write_eld(sor);
19838e2988a7SThierry Reding 
19848e2988a7SThierry Reding 	value = SOR_AUDIO_HDA_PRESENSE_ELDV | SOR_AUDIO_HDA_PRESENSE_PD;
19858e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_AUDIO_HDA_PRESENSE);
19868e2988a7SThierry Reding }
19878e2988a7SThierry Reding 
19888e2988a7SThierry Reding static void tegra_sor_audio_unprepare(struct tegra_sor *sor)
19898e2988a7SThierry Reding {
19908e2988a7SThierry Reding 	tegra_sor_writel(sor, 0, SOR_AUDIO_HDA_PRESENSE);
1991f1f20eb9SThierry Reding 	tegra_sor_writel(sor, 0, SOR_INT_MASK);
1992f1f20eb9SThierry Reding 	tegra_sor_writel(sor, 0, SOR_INT_ENABLE);
19938e2988a7SThierry Reding }
19948e2988a7SThierry Reding 
1995a9087cf2SThierry Reding static void tegra_sor_audio_enable(struct tegra_sor *sor)
1996a9087cf2SThierry Reding {
1997a9087cf2SThierry Reding 	u32 value;
1998a9087cf2SThierry Reding 
1999a9087cf2SThierry Reding 	value = tegra_sor_readl(sor, SOR_AUDIO_CNTRL);
2000a9087cf2SThierry Reding 
2001a9087cf2SThierry Reding 	/* select HDA audio input */
2002a9087cf2SThierry Reding 	value &= ~SOR_AUDIO_CNTRL_SOURCE_SELECT(SOURCE_SELECT_MASK);
2003a9087cf2SThierry Reding 	value |= SOR_AUDIO_CNTRL_SOURCE_SELECT(SOURCE_SELECT_HDA);
2004a9087cf2SThierry Reding 
2005a9087cf2SThierry Reding 	/* inject null samples */
2006a9087cf2SThierry Reding 	if (sor->format.channels != 2)
2007a9087cf2SThierry Reding 		value &= ~SOR_AUDIO_CNTRL_INJECT_NULLSMPL;
2008a9087cf2SThierry Reding 	else
2009a9087cf2SThierry Reding 		value |= SOR_AUDIO_CNTRL_INJECT_NULLSMPL;
2010a9087cf2SThierry Reding 
2011a9087cf2SThierry Reding 	value |= SOR_AUDIO_CNTRL_AFIFO_FLUSH;
2012a9087cf2SThierry Reding 
2013a9087cf2SThierry Reding 	tegra_sor_writel(sor, value, SOR_AUDIO_CNTRL);
2014a9087cf2SThierry Reding 
2015a9087cf2SThierry Reding 	/* enable advertising HBR capability */
2016a9087cf2SThierry Reding 	tegra_sor_writel(sor, SOR_AUDIO_SPARE_HBR_ENABLE, SOR_AUDIO_SPARE);
2017a9087cf2SThierry Reding }
2018a9087cf2SThierry Reding 
20198e2988a7SThierry Reding static int tegra_sor_hdmi_enable_audio_infoframe(struct tegra_sor *sor)
20208e2988a7SThierry Reding {
20218e2988a7SThierry Reding 	u8 buffer[HDMI_INFOFRAME_SIZE(AUDIO)];
20228e2988a7SThierry Reding 	struct hdmi_audio_infoframe frame;
20238e2988a7SThierry Reding 	u32 value;
20248e2988a7SThierry Reding 	int err;
20258e2988a7SThierry Reding 
20268e2988a7SThierry Reding 	err = hdmi_audio_infoframe_init(&frame);
20278e2988a7SThierry Reding 	if (err < 0) {
20288e2988a7SThierry Reding 		dev_err(sor->dev, "failed to setup audio infoframe: %d\n", err);
20298e2988a7SThierry Reding 		return err;
20308e2988a7SThierry Reding 	}
20318e2988a7SThierry Reding 
2032fad7b806SThierry Reding 	frame.channels = sor->format.channels;
20338e2988a7SThierry Reding 
20348e2988a7SThierry Reding 	err = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
20358e2988a7SThierry Reding 	if (err < 0) {
20368e2988a7SThierry Reding 		dev_err(sor->dev, "failed to pack audio infoframe: %d\n", err);
20378e2988a7SThierry Reding 		return err;
20388e2988a7SThierry Reding 	}
20398e2988a7SThierry Reding 
20408e2988a7SThierry Reding 	tegra_sor_hdmi_write_infopack(sor, buffer, err);
20418e2988a7SThierry Reding 
20428e2988a7SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
20438e2988a7SThierry Reding 	value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
20448e2988a7SThierry Reding 	value |= INFOFRAME_CTRL_ENABLE;
20458e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
20468e2988a7SThierry Reding 
20478e2988a7SThierry Reding 	return 0;
20488e2988a7SThierry Reding }
20498e2988a7SThierry Reding 
20508e2988a7SThierry Reding static void tegra_sor_hdmi_audio_enable(struct tegra_sor *sor)
20518e2988a7SThierry Reding {
20528e2988a7SThierry Reding 	u32 value;
20538e2988a7SThierry Reding 
2054a9087cf2SThierry Reding 	tegra_sor_audio_enable(sor);
20558e2988a7SThierry Reding 
20568e2988a7SThierry Reding 	tegra_sor_writel(sor, 0, SOR_HDMI_ACR_CTRL);
20578e2988a7SThierry Reding 
20588e2988a7SThierry Reding 	value = SOR_HDMI_SPARE_ACR_PRIORITY_HIGH |
20598e2988a7SThierry Reding 		SOR_HDMI_SPARE_CTS_RESET(1) |
20608e2988a7SThierry Reding 		SOR_HDMI_SPARE_HW_CTS_ENABLE;
20618e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_SPARE);
20628e2988a7SThierry Reding 
20638e2988a7SThierry Reding 	/* enable HW CTS */
20648e2988a7SThierry Reding 	value = SOR_HDMI_ACR_SUBPACK_LOW_SB1(0);
20658e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_ACR_0441_SUBPACK_LOW);
20668e2988a7SThierry Reding 
20678e2988a7SThierry Reding 	/* allow packet to be sent */
20688e2988a7SThierry Reding 	value = SOR_HDMI_ACR_SUBPACK_HIGH_ENABLE;
20698e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_ACR_0441_SUBPACK_HIGH);
20708e2988a7SThierry Reding 
20718e2988a7SThierry Reding 	/* reset N counter and enable lookup */
20728e2988a7SThierry Reding 	value = SOR_HDMI_AUDIO_N_RESET | SOR_HDMI_AUDIO_N_LOOKUP;
20738e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_N);
20748e2988a7SThierry Reding 
2075fad7b806SThierry Reding 	value = (24000 * 4096) / (128 * sor->format.sample_rate / 1000);
20768e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_0320);
20778e2988a7SThierry Reding 	tegra_sor_writel(sor, 4096, SOR_AUDIO_NVAL_0320);
20788e2988a7SThierry Reding 
20798e2988a7SThierry Reding 	tegra_sor_writel(sor, 20000, SOR_AUDIO_AVAL_0441);
20808e2988a7SThierry Reding 	tegra_sor_writel(sor, 4704, SOR_AUDIO_NVAL_0441);
20818e2988a7SThierry Reding 
20828e2988a7SThierry Reding 	tegra_sor_writel(sor, 20000, SOR_AUDIO_AVAL_0882);
20838e2988a7SThierry Reding 	tegra_sor_writel(sor, 9408, SOR_AUDIO_NVAL_0882);
20848e2988a7SThierry Reding 
20858e2988a7SThierry Reding 	tegra_sor_writel(sor, 20000, SOR_AUDIO_AVAL_1764);
20868e2988a7SThierry Reding 	tegra_sor_writel(sor, 18816, SOR_AUDIO_NVAL_1764);
20878e2988a7SThierry Reding 
2088fad7b806SThierry Reding 	value = (24000 * 6144) / (128 * sor->format.sample_rate / 1000);
20898e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_0480);
20908e2988a7SThierry Reding 	tegra_sor_writel(sor, 6144, SOR_AUDIO_NVAL_0480);
20918e2988a7SThierry Reding 
2092fad7b806SThierry Reding 	value = (24000 * 12288) / (128 * sor->format.sample_rate / 1000);
20938e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_0960);
20948e2988a7SThierry Reding 	tegra_sor_writel(sor, 12288, SOR_AUDIO_NVAL_0960);
20958e2988a7SThierry Reding 
2096fad7b806SThierry Reding 	value = (24000 * 24576) / (128 * sor->format.sample_rate / 1000);
20978e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_1920);
20988e2988a7SThierry Reding 	tegra_sor_writel(sor, 24576, SOR_AUDIO_NVAL_1920);
20998e2988a7SThierry Reding 
21008e2988a7SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_N);
21018e2988a7SThierry Reding 	value &= ~SOR_HDMI_AUDIO_N_RESET;
21028e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_N);
21038e2988a7SThierry Reding 
21048e2988a7SThierry Reding 	tegra_sor_hdmi_enable_audio_infoframe(sor);
21058e2988a7SThierry Reding }
21068e2988a7SThierry Reding 
2107459cc2c6SThierry Reding static void tegra_sor_hdmi_disable_audio_infoframe(struct tegra_sor *sor)
2108459cc2c6SThierry Reding {
2109459cc2c6SThierry Reding 	u32 value;
2110459cc2c6SThierry Reding 
2111459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2112459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_ENABLE;
2113459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2114459cc2c6SThierry Reding }
2115459cc2c6SThierry Reding 
21168e2988a7SThierry Reding static void tegra_sor_hdmi_audio_disable(struct tegra_sor *sor)
21178e2988a7SThierry Reding {
21188e2988a7SThierry Reding 	tegra_sor_hdmi_disable_audio_infoframe(sor);
21198e2988a7SThierry Reding }
21208e2988a7SThierry Reding 
2121459cc2c6SThierry Reding static struct tegra_sor_hdmi_settings *
2122459cc2c6SThierry Reding tegra_sor_hdmi_find_settings(struct tegra_sor *sor, unsigned long frequency)
2123459cc2c6SThierry Reding {
2124459cc2c6SThierry Reding 	unsigned int i;
2125459cc2c6SThierry Reding 
2126459cc2c6SThierry Reding 	for (i = 0; i < sor->num_settings; i++)
2127459cc2c6SThierry Reding 		if (frequency <= sor->settings[i].frequency)
2128459cc2c6SThierry Reding 			return &sor->settings[i];
2129459cc2c6SThierry Reding 
2130459cc2c6SThierry Reding 	return NULL;
2131459cc2c6SThierry Reding }
2132459cc2c6SThierry Reding 
213336e90221SThierry Reding static void tegra_sor_hdmi_disable_scrambling(struct tegra_sor *sor)
213436e90221SThierry Reding {
213536e90221SThierry Reding 	u32 value;
213636e90221SThierry Reding 
213736e90221SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI2_CTRL);
213836e90221SThierry Reding 	value &= ~SOR_HDMI2_CTRL_CLOCK_MODE_DIV_BY_4;
213936e90221SThierry Reding 	value &= ~SOR_HDMI2_CTRL_SCRAMBLE;
214036e90221SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI2_CTRL);
214136e90221SThierry Reding }
214236e90221SThierry Reding 
214336e90221SThierry Reding static void tegra_sor_hdmi_scdc_disable(struct tegra_sor *sor)
214436e90221SThierry Reding {
214536e90221SThierry Reding 	struct i2c_adapter *ddc = sor->output.ddc;
214636e90221SThierry Reding 
214736e90221SThierry Reding 	drm_scdc_set_high_tmds_clock_ratio(ddc, false);
214836e90221SThierry Reding 	drm_scdc_set_scrambling(ddc, false);
214936e90221SThierry Reding 
215036e90221SThierry Reding 	tegra_sor_hdmi_disable_scrambling(sor);
215136e90221SThierry Reding }
215236e90221SThierry Reding 
215336e90221SThierry Reding static void tegra_sor_hdmi_scdc_stop(struct tegra_sor *sor)
215436e90221SThierry Reding {
215536e90221SThierry Reding 	if (sor->scdc_enabled) {
215636e90221SThierry Reding 		cancel_delayed_work_sync(&sor->scdc);
215736e90221SThierry Reding 		tegra_sor_hdmi_scdc_disable(sor);
215836e90221SThierry Reding 	}
215936e90221SThierry Reding }
216036e90221SThierry Reding 
216136e90221SThierry Reding static void tegra_sor_hdmi_enable_scrambling(struct tegra_sor *sor)
216236e90221SThierry Reding {
216336e90221SThierry Reding 	u32 value;
216436e90221SThierry Reding 
216536e90221SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI2_CTRL);
216636e90221SThierry Reding 	value |= SOR_HDMI2_CTRL_CLOCK_MODE_DIV_BY_4;
216736e90221SThierry Reding 	value |= SOR_HDMI2_CTRL_SCRAMBLE;
216836e90221SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI2_CTRL);
216936e90221SThierry Reding }
217036e90221SThierry Reding 
217136e90221SThierry Reding static void tegra_sor_hdmi_scdc_enable(struct tegra_sor *sor)
217236e90221SThierry Reding {
217336e90221SThierry Reding 	struct i2c_adapter *ddc = sor->output.ddc;
217436e90221SThierry Reding 
217536e90221SThierry Reding 	drm_scdc_set_high_tmds_clock_ratio(ddc, true);
217636e90221SThierry Reding 	drm_scdc_set_scrambling(ddc, true);
217736e90221SThierry Reding 
217836e90221SThierry Reding 	tegra_sor_hdmi_enable_scrambling(sor);
217936e90221SThierry Reding }
218036e90221SThierry Reding 
218136e90221SThierry Reding static void tegra_sor_hdmi_scdc_work(struct work_struct *work)
218236e90221SThierry Reding {
218336e90221SThierry Reding 	struct tegra_sor *sor = container_of(work, struct tegra_sor, scdc.work);
218436e90221SThierry Reding 	struct i2c_adapter *ddc = sor->output.ddc;
218536e90221SThierry Reding 
218636e90221SThierry Reding 	if (!drm_scdc_get_scrambling_status(ddc)) {
218736e90221SThierry Reding 		DRM_DEBUG_KMS("SCDC not scrambled\n");
218836e90221SThierry Reding 		tegra_sor_hdmi_scdc_enable(sor);
218936e90221SThierry Reding 	}
219036e90221SThierry Reding 
219136e90221SThierry Reding 	schedule_delayed_work(&sor->scdc, msecs_to_jiffies(5000));
219236e90221SThierry Reding }
219336e90221SThierry Reding 
219436e90221SThierry Reding static void tegra_sor_hdmi_scdc_start(struct tegra_sor *sor)
219536e90221SThierry Reding {
219636e90221SThierry Reding 	struct drm_scdc *scdc = &sor->output.connector.display_info.hdmi.scdc;
219736e90221SThierry Reding 	struct drm_display_mode *mode;
219836e90221SThierry Reding 
219936e90221SThierry Reding 	mode = &sor->output.encoder.crtc->state->adjusted_mode;
220036e90221SThierry Reding 
220136e90221SThierry Reding 	if (mode->clock >= 340000 && scdc->supported) {
220236e90221SThierry Reding 		schedule_delayed_work(&sor->scdc, msecs_to_jiffies(5000));
220336e90221SThierry Reding 		tegra_sor_hdmi_scdc_enable(sor);
220436e90221SThierry Reding 		sor->scdc_enabled = true;
220536e90221SThierry Reding 	}
220636e90221SThierry Reding }
220736e90221SThierry Reding 
2208459cc2c6SThierry Reding static void tegra_sor_hdmi_disable(struct drm_encoder *encoder)
2209459cc2c6SThierry Reding {
2210459cc2c6SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
2211459cc2c6SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
2212459cc2c6SThierry Reding 	struct tegra_sor *sor = to_sor(output);
2213459cc2c6SThierry Reding 	u32 value;
2214459cc2c6SThierry Reding 	int err;
2215459cc2c6SThierry Reding 
22168e2988a7SThierry Reding 	tegra_sor_audio_unprepare(sor);
221736e90221SThierry Reding 	tegra_sor_hdmi_scdc_stop(sor);
221836e90221SThierry Reding 
2219459cc2c6SThierry Reding 	err = tegra_sor_detach(sor);
2220459cc2c6SThierry Reding 	if (err < 0)
2221459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to detach SOR: %d\n", err);
2222459cc2c6SThierry Reding 
2223459cc2c6SThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE1);
2224459cc2c6SThierry Reding 	tegra_sor_update(sor);
2225459cc2c6SThierry Reding 
2226459cc2c6SThierry Reding 	/* disable display to SOR clock */
2227459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
2228c57997bcSThierry Reding 
2229c57997bcSThierry Reding 	if (!sor->soc->has_nvdisplay)
2230d278e4a9SThierry Reding 		value &= ~SOR1_TIMING_CYA;
2231d278e4a9SThierry Reding 
2232c57997bcSThierry Reding 	value &= ~SOR_ENABLE(sor->index);
2233c57997bcSThierry Reding 
2234459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2235459cc2c6SThierry Reding 
2236459cc2c6SThierry Reding 	tegra_dc_commit(dc);
2237459cc2c6SThierry Reding 
2238459cc2c6SThierry Reding 	err = tegra_sor_power_down(sor);
2239459cc2c6SThierry Reding 	if (err < 0)
2240459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power down SOR: %d\n", err);
2241459cc2c6SThierry Reding 
2242c57997bcSThierry Reding 	err = tegra_io_pad_power_disable(sor->pad);
2243459cc2c6SThierry Reding 	if (err < 0)
2244c57997bcSThierry Reding 		dev_err(sor->dev, "failed to power off I/O pad: %d\n", err);
2245459cc2c6SThierry Reding 
2246fd67e9c6SThierry Reding 	host1x_client_suspend(&sor->client);
2247459cc2c6SThierry Reding }
2248459cc2c6SThierry Reding 
2249459cc2c6SThierry Reding static void tegra_sor_hdmi_enable(struct drm_encoder *encoder)
2250459cc2c6SThierry Reding {
2251459cc2c6SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
2252459cc2c6SThierry Reding 	unsigned int h_ref_to_sync = 1, pulse_start, max_ac;
2253459cc2c6SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
2254459cc2c6SThierry Reding 	struct tegra_sor_hdmi_settings *settings;
2255459cc2c6SThierry Reding 	struct tegra_sor *sor = to_sor(output);
2256c31efa7aSThierry Reding 	struct tegra_sor_state *state;
2257459cc2c6SThierry Reding 	struct drm_display_mode *mode;
225836e90221SThierry Reding 	unsigned long rate, pclk;
225930b49435SThierry Reding 	unsigned int div, i;
2260459cc2c6SThierry Reding 	u32 value;
2261459cc2c6SThierry Reding 	int err;
2262459cc2c6SThierry Reding 
2263c31efa7aSThierry Reding 	state = to_sor_state(output->connector.state);
2264459cc2c6SThierry Reding 	mode = &encoder->crtc->state->adjusted_mode;
226536e90221SThierry Reding 	pclk = mode->clock * 1000;
2266459cc2c6SThierry Reding 
2267fd67e9c6SThierry Reding 	err = host1x_client_resume(&sor->client);
2268fd67e9c6SThierry Reding 	if (err < 0) {
2269fd67e9c6SThierry Reding 		dev_err(sor->dev, "failed to resume: %d\n", err);
2270fd67e9c6SThierry Reding 		return;
2271fd67e9c6SThierry Reding 	}
2272459cc2c6SThierry Reding 
227325bb2cecSThierry Reding 	/* switch to safe parent clock */
227425bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
2275e1335e2fSThierry Reding 	if (err < 0) {
2276459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
2277e1335e2fSThierry Reding 		return;
2278e1335e2fSThierry Reding 	}
2279459cc2c6SThierry Reding 
2280459cc2c6SThierry Reding 	div = clk_get_rate(sor->clk) / 1000000 * 4;
2281459cc2c6SThierry Reding 
2282c57997bcSThierry Reding 	err = tegra_io_pad_power_enable(sor->pad);
2283459cc2c6SThierry Reding 	if (err < 0)
2284c57997bcSThierry Reding 		dev_err(sor->dev, "failed to power on I/O pad: %d\n", err);
2285459cc2c6SThierry Reding 
2286459cc2c6SThierry Reding 	usleep_range(20, 100);
2287459cc2c6SThierry Reding 
2288880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
2289459cc2c6SThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
2290880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
2291459cc2c6SThierry Reding 
2292459cc2c6SThierry Reding 	usleep_range(20, 100);
2293459cc2c6SThierry Reding 
2294880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll3);
2295459cc2c6SThierry Reding 	value &= ~SOR_PLL3_PLL_VDD_MODE_3V3;
2296880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll3);
2297459cc2c6SThierry Reding 
2298880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
2299459cc2c6SThierry Reding 	value &= ~SOR_PLL0_VCOPD;
2300459cc2c6SThierry Reding 	value &= ~SOR_PLL0_PWR;
2301880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
2302459cc2c6SThierry Reding 
2303880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
2304459cc2c6SThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
2305880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
2306459cc2c6SThierry Reding 
2307459cc2c6SThierry Reding 	usleep_range(200, 400);
2308459cc2c6SThierry Reding 
2309880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
2310459cc2c6SThierry Reding 	value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
2311459cc2c6SThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
2312880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
2313459cc2c6SThierry Reding 
2314459cc2c6SThierry Reding 	usleep_range(20, 100);
2315459cc2c6SThierry Reding 
2316880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2317459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
2318459cc2c6SThierry Reding 		 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2;
2319880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2320459cc2c6SThierry Reding 
2321459cc2c6SThierry Reding 	while (true) {
2322459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2323459cc2c6SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_STATE_BUSY) == 0)
2324459cc2c6SThierry Reding 			break;
2325459cc2c6SThierry Reding 
2326459cc2c6SThierry Reding 		usleep_range(250, 1000);
2327459cc2c6SThierry Reding 	}
2328459cc2c6SThierry Reding 
2329459cc2c6SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
2330459cc2c6SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_UP | SOR_LANE_SEQ_CTL_DELAY(5);
2331459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
2332459cc2c6SThierry Reding 
2333459cc2c6SThierry Reding 	while (true) {
2334459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2335459cc2c6SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
2336459cc2c6SThierry Reding 			break;
2337459cc2c6SThierry Reding 
2338459cc2c6SThierry Reding 		usleep_range(250, 1000);
2339459cc2c6SThierry Reding 	}
2340459cc2c6SThierry Reding 
2341459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
2342459cc2c6SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
2343459cc2c6SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
2344459cc2c6SThierry Reding 
234536e90221SThierry Reding 	if (mode->clock < 340000) {
234636e90221SThierry Reding 		DRM_DEBUG_KMS("setting 2.7 GHz link speed\n");
2347459cc2c6SThierry Reding 		value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G2_70;
234836e90221SThierry Reding 	} else {
234936e90221SThierry Reding 		DRM_DEBUG_KMS("setting 5.4 GHz link speed\n");
2350459cc2c6SThierry Reding 		value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G5_40;
235136e90221SThierry Reding 	}
2352459cc2c6SThierry Reding 
2353459cc2c6SThierry Reding 	value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
2354459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
2355459cc2c6SThierry Reding 
2356c57997bcSThierry Reding 	/* SOR pad PLL stabilization time */
2357c57997bcSThierry Reding 	usleep_range(250, 1000);
2358c57997bcSThierry Reding 
2359c57997bcSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
2360c57997bcSThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
2361c57997bcSThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(4);
2362c57997bcSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
2363c57997bcSThierry Reding 
2364459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
2365c57997bcSThierry Reding 	value &= ~SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
2366459cc2c6SThierry Reding 	value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
2367c57997bcSThierry Reding 	value &= ~SOR_DP_SPARE_SEQ_ENABLE;
2368c57997bcSThierry Reding 	value &= ~SOR_DP_SPARE_MACRO_SOR_CLK;
2369459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
2370459cc2c6SThierry Reding 
2371459cc2c6SThierry Reding 	value = SOR_SEQ_CTL_PU_PC(0) | SOR_SEQ_CTL_PU_PC_ALT(0) |
2372459cc2c6SThierry Reding 		SOR_SEQ_CTL_PD_PC(8) | SOR_SEQ_CTL_PD_PC_ALT(8);
2373459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_CTL);
2374459cc2c6SThierry Reding 
2375459cc2c6SThierry Reding 	value = SOR_SEQ_INST_DRIVE_PWM_OUT_LO | SOR_SEQ_INST_HALT |
2376459cc2c6SThierry Reding 		SOR_SEQ_INST_WAIT_VSYNC | SOR_SEQ_INST_WAIT(1);
2377459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_INST(0));
2378459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_INST(8));
2379459cc2c6SThierry Reding 
2380c57997bcSThierry Reding 	if (!sor->soc->has_nvdisplay) {
2381459cc2c6SThierry Reding 		/* program the reference clock */
2382459cc2c6SThierry Reding 		value = SOR_REFCLK_DIV_INT(div) | SOR_REFCLK_DIV_FRAC(div);
2383459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, SOR_REFCLK);
2384c57997bcSThierry Reding 	}
2385459cc2c6SThierry Reding 
238630b49435SThierry Reding 	/* XXX not in TRM */
238730b49435SThierry Reding 	for (value = 0, i = 0; i < 5; i++)
23886d6c815dSThierry Reding 		value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->xbar_cfg[i]) |
238930b49435SThierry Reding 			 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
2390459cc2c6SThierry Reding 
2391459cc2c6SThierry Reding 	tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
239230b49435SThierry Reding 	tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
2393459cc2c6SThierry Reding 
239461417aaaSThierry Reding 	/*
239561417aaaSThierry Reding 	 * Switch the pad clock to the DP clock. Note that we cannot actually
239661417aaaSThierry Reding 	 * do this because Tegra186 and later don't support clk_set_parent()
239761417aaaSThierry Reding 	 * on the sorX_pad_clkout clocks. We already do the equivalent above
239861417aaaSThierry Reding 	 * using the DP_CLK_SEL mux of the SOR_CLK_CNTRL register.
239961417aaaSThierry Reding 	 */
240061417aaaSThierry Reding #if 0
240161417aaaSThierry Reding 	err = clk_set_parent(sor->clk_pad, sor->clk_dp);
2402e1335e2fSThierry Reding 	if (err < 0) {
240361417aaaSThierry Reding 		dev_err(sor->dev, "failed to select pad parent clock: %d\n",
240461417aaaSThierry Reding 			err);
240561417aaaSThierry Reding 		return;
240661417aaaSThierry Reding 	}
240761417aaaSThierry Reding #endif
240861417aaaSThierry Reding 
240961417aaaSThierry Reding 	/* switch the SOR clock to the pad clock */
241061417aaaSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_pad);
241161417aaaSThierry Reding 	if (err < 0) {
241261417aaaSThierry Reding 		dev_err(sor->dev, "failed to select SOR parent clock: %d\n",
241361417aaaSThierry Reding 			err);
2414e1335e2fSThierry Reding 		return;
2415e1335e2fSThierry Reding 	}
2416e1335e2fSThierry Reding 
241761417aaaSThierry Reding 	/* switch the output clock to the parent pixel clock */
241861417aaaSThierry Reding 	err = clk_set_parent(sor->clk, sor->clk_parent);
2419e1335e2fSThierry Reding 	if (err < 0) {
242061417aaaSThierry Reding 		dev_err(sor->dev, "failed to select output parent clock: %d\n",
242161417aaaSThierry Reding 			err);
2422e1335e2fSThierry Reding 		return;
2423e1335e2fSThierry Reding 	}
2424459cc2c6SThierry Reding 
242536e90221SThierry Reding 	/* adjust clock rate for HDMI 2.0 modes */
242636e90221SThierry Reding 	rate = clk_get_rate(sor->clk_parent);
242736e90221SThierry Reding 
242836e90221SThierry Reding 	if (mode->clock >= 340000)
242936e90221SThierry Reding 		rate /= 2;
243036e90221SThierry Reding 
243136e90221SThierry Reding 	DRM_DEBUG_KMS("setting clock to %lu Hz, mode: %lu Hz\n", rate, pclk);
243236e90221SThierry Reding 
243336e90221SThierry Reding 	clk_set_rate(sor->clk, rate);
2434c57997bcSThierry Reding 
2435c57997bcSThierry Reding 	if (!sor->soc->has_nvdisplay) {
2436459cc2c6SThierry Reding 		value = SOR_INPUT_CONTROL_HDMI_SRC_SELECT(dc->pipe);
2437459cc2c6SThierry Reding 
2438459cc2c6SThierry Reding 		/* XXX is this the proper check? */
2439459cc2c6SThierry Reding 		if (mode->clock < 75000)
2440459cc2c6SThierry Reding 			value |= SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_LIMITED;
2441459cc2c6SThierry Reding 
2442459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, SOR_INPUT_CONTROL);
2443c57997bcSThierry Reding 	}
2444459cc2c6SThierry Reding 
2445459cc2c6SThierry Reding 	max_ac = ((mode->htotal - mode->hdisplay) - SOR_REKEY - 18) / 32;
2446459cc2c6SThierry Reding 
2447459cc2c6SThierry Reding 	value = SOR_HDMI_CTRL_ENABLE | SOR_HDMI_CTRL_MAX_AC_PACKET(max_ac) |
2448459cc2c6SThierry Reding 		SOR_HDMI_CTRL_AUDIO_LAYOUT | SOR_HDMI_CTRL_REKEY(SOR_REKEY);
2449459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_CTRL);
2450459cc2c6SThierry Reding 
2451c57997bcSThierry Reding 	if (!dc->soc->has_nvdisplay) {
2452459cc2c6SThierry Reding 		/* H_PULSE2 setup */
2453c57997bcSThierry Reding 		pulse_start = h_ref_to_sync +
2454c57997bcSThierry Reding 			      (mode->hsync_end - mode->hsync_start) +
2455459cc2c6SThierry Reding 			      (mode->htotal - mode->hsync_end) - 10;
2456459cc2c6SThierry Reding 
2457459cc2c6SThierry Reding 		value = PULSE_LAST_END_A | PULSE_QUAL_VACTIVE |
2458459cc2c6SThierry Reding 			PULSE_POLARITY_HIGH | PULSE_MODE_NORMAL;
2459459cc2c6SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_CONTROL);
2460459cc2c6SThierry Reding 
2461459cc2c6SThierry Reding 		value = PULSE_END(pulse_start + 8) | PULSE_START(pulse_start);
2462459cc2c6SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_POSITION_A);
2463459cc2c6SThierry Reding 
2464459cc2c6SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_DISP_SIGNAL_OPTIONS0);
2465459cc2c6SThierry Reding 		value |= H_PULSE2_ENABLE;
2466459cc2c6SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_SIGNAL_OPTIONS0);
2467c57997bcSThierry Reding 	}
2468459cc2c6SThierry Reding 
2469459cc2c6SThierry Reding 	/* infoframe setup */
2470459cc2c6SThierry Reding 	err = tegra_sor_hdmi_setup_avi_infoframe(sor, mode);
2471459cc2c6SThierry Reding 	if (err < 0)
2472459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
2473459cc2c6SThierry Reding 
2474459cc2c6SThierry Reding 	/* XXX HDMI audio support not implemented yet */
2475459cc2c6SThierry Reding 	tegra_sor_hdmi_disable_audio_infoframe(sor);
2476459cc2c6SThierry Reding 
2477459cc2c6SThierry Reding 	/* use single TMDS protocol */
2478459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
2479459cc2c6SThierry Reding 	value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
2480459cc2c6SThierry Reding 	value |= SOR_STATE_ASY_PROTOCOL_SINGLE_TMDS_A;
2481459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
2482459cc2c6SThierry Reding 
2483459cc2c6SThierry Reding 	/* power up pad calibration */
2484880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2485459cc2c6SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
2486880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2487459cc2c6SThierry Reding 
2488459cc2c6SThierry Reding 	/* production settings */
2489459cc2c6SThierry Reding 	settings = tegra_sor_hdmi_find_settings(sor, mode->clock * 1000);
2490db8b42fbSDan Carpenter 	if (!settings) {
2491db8b42fbSDan Carpenter 		dev_err(sor->dev, "no settings for pixel clock %d Hz\n",
2492db8b42fbSDan Carpenter 			mode->clock * 1000);
2493459cc2c6SThierry Reding 		return;
2494459cc2c6SThierry Reding 	}
2495459cc2c6SThierry Reding 
2496880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
2497459cc2c6SThierry Reding 	value &= ~SOR_PLL0_ICHPMP_MASK;
2498c57997bcSThierry Reding 	value &= ~SOR_PLL0_FILTER_MASK;
2499459cc2c6SThierry Reding 	value &= ~SOR_PLL0_VCOCAP_MASK;
2500459cc2c6SThierry Reding 	value |= SOR_PLL0_ICHPMP(settings->ichpmp);
2501c57997bcSThierry Reding 	value |= SOR_PLL0_FILTER(settings->filter);
2502459cc2c6SThierry Reding 	value |= SOR_PLL0_VCOCAP(settings->vcocap);
2503880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
2504459cc2c6SThierry Reding 
2505c57997bcSThierry Reding 	/* XXX not in TRM */
2506880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll1);
2507459cc2c6SThierry Reding 	value &= ~SOR_PLL1_LOADADJ_MASK;
2508c57997bcSThierry Reding 	value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
2509459cc2c6SThierry Reding 	value |= SOR_PLL1_LOADADJ(settings->loadadj);
2510c57997bcSThierry Reding 	value |= SOR_PLL1_TMDS_TERMADJ(settings->tmds_termadj);
2511c57997bcSThierry Reding 	value |= SOR_PLL1_TMDS_TERM;
2512880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll1);
2513459cc2c6SThierry Reding 
2514880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll3);
2515c57997bcSThierry Reding 	value &= ~SOR_PLL3_BG_TEMP_COEF_MASK;
2516459cc2c6SThierry Reding 	value &= ~SOR_PLL3_BG_VREF_LEVEL_MASK;
2517c57997bcSThierry Reding 	value &= ~SOR_PLL3_AVDD10_LEVEL_MASK;
2518c57997bcSThierry Reding 	value &= ~SOR_PLL3_AVDD14_LEVEL_MASK;
2519c57997bcSThierry Reding 	value |= SOR_PLL3_BG_TEMP_COEF(settings->bg_temp_coef);
2520c57997bcSThierry Reding 	value |= SOR_PLL3_BG_VREF_LEVEL(settings->bg_vref_level);
2521c57997bcSThierry Reding 	value |= SOR_PLL3_AVDD10_LEVEL(settings->avdd10_level);
2522c57997bcSThierry Reding 	value |= SOR_PLL3_AVDD14_LEVEL(settings->avdd14_level);
2523880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll3);
2524459cc2c6SThierry Reding 
2525c57997bcSThierry Reding 	value = settings->drive_current[3] << 24 |
2526c57997bcSThierry Reding 		settings->drive_current[2] << 16 |
2527c57997bcSThierry Reding 		settings->drive_current[1] <<  8 |
2528c57997bcSThierry Reding 		settings->drive_current[0] <<  0;
2529459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
2530459cc2c6SThierry Reding 
2531c57997bcSThierry Reding 	value = settings->preemphasis[3] << 24 |
2532c57997bcSThierry Reding 		settings->preemphasis[2] << 16 |
2533c57997bcSThierry Reding 		settings->preemphasis[1] <<  8 |
2534c57997bcSThierry Reding 		settings->preemphasis[0] <<  0;
2535459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
2536459cc2c6SThierry Reding 
2537880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2538459cc2c6SThierry Reding 	value &= ~SOR_DP_PADCTL_TX_PU_MASK;
2539459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU_ENABLE;
2540c57997bcSThierry Reding 	value |= SOR_DP_PADCTL_TX_PU(settings->tx_pu_value);
2541880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2542459cc2c6SThierry Reding 
2543c57997bcSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl2);
2544c57997bcSThierry Reding 	value &= ~SOR_DP_PADCTL_SPAREPLL_MASK;
2545c57997bcSThierry Reding 	value |= SOR_DP_PADCTL_SPAREPLL(settings->sparepll);
2546c57997bcSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl2);
2547c57997bcSThierry Reding 
2548459cc2c6SThierry Reding 	/* power down pad calibration */
2549880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2550459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
2551880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2552459cc2c6SThierry Reding 
2553c57997bcSThierry Reding 	if (!dc->soc->has_nvdisplay) {
2554459cc2c6SThierry Reding 		/* miscellaneous display controller settings */
2555459cc2c6SThierry Reding 		value = VSYNC_H_POSITION(1);
2556459cc2c6SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_TIMING_OPTIONS);
2557c57997bcSThierry Reding 	}
2558459cc2c6SThierry Reding 
2559459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_COLOR_CONTROL);
2560459cc2c6SThierry Reding 	value &= ~DITHER_CONTROL_MASK;
2561459cc2c6SThierry Reding 	value &= ~BASE_COLOR_SIZE_MASK;
2562459cc2c6SThierry Reding 
2563c31efa7aSThierry Reding 	switch (state->bpc) {
2564459cc2c6SThierry Reding 	case 6:
2565459cc2c6SThierry Reding 		value |= BASE_COLOR_SIZE_666;
2566459cc2c6SThierry Reding 		break;
2567459cc2c6SThierry Reding 
2568459cc2c6SThierry Reding 	case 8:
2569459cc2c6SThierry Reding 		value |= BASE_COLOR_SIZE_888;
2570459cc2c6SThierry Reding 		break;
2571459cc2c6SThierry Reding 
2572c57997bcSThierry Reding 	case 10:
2573c57997bcSThierry Reding 		value |= BASE_COLOR_SIZE_101010;
2574c57997bcSThierry Reding 		break;
2575c57997bcSThierry Reding 
2576c57997bcSThierry Reding 	case 12:
2577c57997bcSThierry Reding 		value |= BASE_COLOR_SIZE_121212;
2578c57997bcSThierry Reding 		break;
2579c57997bcSThierry Reding 
2580459cc2c6SThierry Reding 	default:
2581c31efa7aSThierry Reding 		WARN(1, "%u bits-per-color not supported\n", state->bpc);
2582c31efa7aSThierry Reding 		value |= BASE_COLOR_SIZE_888;
2583459cc2c6SThierry Reding 		break;
2584459cc2c6SThierry Reding 	}
2585459cc2c6SThierry Reding 
2586459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_COLOR_CONTROL);
2587459cc2c6SThierry Reding 
2588c57997bcSThierry Reding 	/* XXX set display head owner */
2589c57997bcSThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
2590c57997bcSThierry Reding 	value &= ~SOR_STATE_ASY_OWNER_MASK;
2591c57997bcSThierry Reding 	value |= SOR_STATE_ASY_OWNER(1 + dc->pipe);
2592c57997bcSThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
2593c57997bcSThierry Reding 
2594459cc2c6SThierry Reding 	err = tegra_sor_power_up(sor, 250);
2595459cc2c6SThierry Reding 	if (err < 0)
2596459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
2597459cc2c6SThierry Reding 
25982bd1dd39SThierry Reding 	/* configure dynamic range of output */
2599880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
2600459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_RANGECOMPRESS_MASK;
2601459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_DYNRANGE_MASK;
2602880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
2603459cc2c6SThierry Reding 
26042bd1dd39SThierry Reding 	/* configure colorspace */
2605880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
2606459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_COLORSPACE_MASK;
2607459cc2c6SThierry Reding 	value |= SOR_HEAD_STATE_COLORSPACE_RGB;
2608880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
2609459cc2c6SThierry Reding 
2610c31efa7aSThierry Reding 	tegra_sor_mode_set(sor, mode, state);
2611459cc2c6SThierry Reding 
2612459cc2c6SThierry Reding 	tegra_sor_update(sor);
2613459cc2c6SThierry Reding 
2614c57997bcSThierry Reding 	/* program preamble timing in SOR (XXX) */
2615c57997bcSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
2616c57997bcSThierry Reding 	value &= ~SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
2617c57997bcSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
2618c57997bcSThierry Reding 
2619459cc2c6SThierry Reding 	err = tegra_sor_attach(sor);
2620459cc2c6SThierry Reding 	if (err < 0)
2621459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to attach SOR: %d\n", err);
2622459cc2c6SThierry Reding 
2623459cc2c6SThierry Reding 	/* enable display to SOR clock and generate HDMI preamble */
2624459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
2625c57997bcSThierry Reding 
2626c57997bcSThierry Reding 	if (!sor->soc->has_nvdisplay)
2627d278e4a9SThierry Reding 		value |= SOR1_TIMING_CYA;
2628d278e4a9SThierry Reding 
2629c57997bcSThierry Reding 	value |= SOR_ENABLE(sor->index);
2630c57997bcSThierry Reding 
2631459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2632459cc2c6SThierry Reding 
2633c57997bcSThierry Reding 	if (dc->soc->has_nvdisplay) {
2634c57997bcSThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_CORE_SOR_SET_CONTROL(sor->index));
2635c57997bcSThierry Reding 		value &= ~PROTOCOL_MASK;
2636c57997bcSThierry Reding 		value |= PROTOCOL_SINGLE_TMDS_A;
2637c57997bcSThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_CORE_SOR_SET_CONTROL(sor->index));
2638c57997bcSThierry Reding 	}
2639c57997bcSThierry Reding 
2640459cc2c6SThierry Reding 	tegra_dc_commit(dc);
2641459cc2c6SThierry Reding 
2642459cc2c6SThierry Reding 	err = tegra_sor_wakeup(sor);
2643459cc2c6SThierry Reding 	if (err < 0)
2644459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
264536e90221SThierry Reding 
264636e90221SThierry Reding 	tegra_sor_hdmi_scdc_start(sor);
26478e2988a7SThierry Reding 	tegra_sor_audio_prepare(sor);
2648459cc2c6SThierry Reding }
2649459cc2c6SThierry Reding 
2650459cc2c6SThierry Reding static const struct drm_encoder_helper_funcs tegra_sor_hdmi_helpers = {
2651459cc2c6SThierry Reding 	.disable = tegra_sor_hdmi_disable,
2652459cc2c6SThierry Reding 	.enable = tegra_sor_hdmi_enable,
2653459cc2c6SThierry Reding 	.atomic_check = tegra_sor_encoder_atomic_check,
2654459cc2c6SThierry Reding };
2655459cc2c6SThierry Reding 
26560472c21bSThierry Reding static void tegra_sor_dp_disable(struct drm_encoder *encoder)
26570472c21bSThierry Reding {
26580472c21bSThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
26590472c21bSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
26600472c21bSThierry Reding 	struct tegra_sor *sor = to_sor(output);
26610472c21bSThierry Reding 	u32 value;
26620472c21bSThierry Reding 	int err;
26630472c21bSThierry Reding 
2664d278e4a9SThierry Reding 	if (output->panel)
2665d278e4a9SThierry Reding 		drm_panel_disable(output->panel);
2666d278e4a9SThierry Reding 
266768a2ebb5SThierry Reding 	/*
266868a2ebb5SThierry Reding 	 * Do not attempt to power down a DP link if we're not connected since
266968a2ebb5SThierry Reding 	 * the AUX transactions would just be timing out.
267068a2ebb5SThierry Reding 	 */
267168a2ebb5SThierry Reding 	if (output->connector.status != connector_status_disconnected) {
26720472c21bSThierry Reding 		err = drm_dp_link_power_down(sor->aux, &sor->link);
26730472c21bSThierry Reding 		if (err < 0)
267468a2ebb5SThierry Reding 			dev_err(sor->dev, "failed to power down link: %d\n",
267568a2ebb5SThierry Reding 				err);
267668a2ebb5SThierry Reding 	}
26770472c21bSThierry Reding 
26780472c21bSThierry Reding 	err = tegra_sor_detach(sor);
26790472c21bSThierry Reding 	if (err < 0)
26800472c21bSThierry Reding 		dev_err(sor->dev, "failed to detach SOR: %d\n", err);
26810472c21bSThierry Reding 
26820472c21bSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE1);
26830472c21bSThierry Reding 	tegra_sor_update(sor);
26840472c21bSThierry Reding 
26850472c21bSThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
26860472c21bSThierry Reding 	value &= ~SOR_ENABLE(sor->index);
26870472c21bSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
26880472c21bSThierry Reding 	tegra_dc_commit(dc);
26890472c21bSThierry Reding 
26900472c21bSThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
26910472c21bSThierry Reding 	value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
26920472c21bSThierry Reding 	value &= ~SOR_STATE_ASY_SUBOWNER_MASK;
26930472c21bSThierry Reding 	value &= ~SOR_STATE_ASY_OWNER_MASK;
26940472c21bSThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
26950472c21bSThierry Reding 	tegra_sor_update(sor);
26960472c21bSThierry Reding 
26970472c21bSThierry Reding 	/* switch to safe parent clock */
26980472c21bSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
26990472c21bSThierry Reding 	if (err < 0)
27000472c21bSThierry Reding 		dev_err(sor->dev, "failed to set safe clock: %d\n", err);
27010472c21bSThierry Reding 
27020472c21bSThierry Reding 	err = tegra_sor_power_down(sor);
27030472c21bSThierry Reding 	if (err < 0)
27040472c21bSThierry Reding 		dev_err(sor->dev, "failed to power down SOR: %d\n", err);
27050472c21bSThierry Reding 
27060472c21bSThierry Reding 	err = tegra_io_pad_power_disable(sor->pad);
27070472c21bSThierry Reding 	if (err < 0)
27080472c21bSThierry Reding 		dev_err(sor->dev, "failed to power off I/O pad: %d\n", err);
27090472c21bSThierry Reding 
27100472c21bSThierry Reding 	err = drm_dp_aux_disable(sor->aux);
27110472c21bSThierry Reding 	if (err < 0)
27120472c21bSThierry Reding 		dev_err(sor->dev, "failed disable DPAUX: %d\n", err);
27130472c21bSThierry Reding 
2714d278e4a9SThierry Reding 	if (output->panel)
2715d278e4a9SThierry Reding 		drm_panel_unprepare(output->panel);
2716d278e4a9SThierry Reding 
2717fd67e9c6SThierry Reding 	host1x_client_suspend(&sor->client);
27180472c21bSThierry Reding }
27190472c21bSThierry Reding 
27200472c21bSThierry Reding static void tegra_sor_dp_enable(struct drm_encoder *encoder)
27210472c21bSThierry Reding {
27220472c21bSThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
27230472c21bSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
27240472c21bSThierry Reding 	struct tegra_sor *sor = to_sor(output);
27250472c21bSThierry Reding 	struct tegra_sor_config config;
27260472c21bSThierry Reding 	struct tegra_sor_state *state;
27270472c21bSThierry Reding 	struct drm_display_mode *mode;
27280472c21bSThierry Reding 	struct drm_display_info *info;
27290472c21bSThierry Reding 	unsigned int i;
27300472c21bSThierry Reding 	u32 value;
27310472c21bSThierry Reding 	int err;
27320472c21bSThierry Reding 
27330472c21bSThierry Reding 	state = to_sor_state(output->connector.state);
27340472c21bSThierry Reding 	mode = &encoder->crtc->state->adjusted_mode;
27350472c21bSThierry Reding 	info = &output->connector.display_info;
27360472c21bSThierry Reding 
2737fd67e9c6SThierry Reding 	err = host1x_client_resume(&sor->client);
2738fd67e9c6SThierry Reding 	if (err < 0) {
2739fd67e9c6SThierry Reding 		dev_err(sor->dev, "failed to resume: %d\n", err);
2740fd67e9c6SThierry Reding 		return;
2741fd67e9c6SThierry Reding 	}
27420472c21bSThierry Reding 
27430472c21bSThierry Reding 	/* switch to safe parent clock */
27440472c21bSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
27450472c21bSThierry Reding 	if (err < 0)
27460472c21bSThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
27470472c21bSThierry Reding 
27480472c21bSThierry Reding 	err = tegra_io_pad_power_enable(sor->pad);
27490472c21bSThierry Reding 	if (err < 0)
27500472c21bSThierry Reding 		dev_err(sor->dev, "failed to power on LVDS rail: %d\n", err);
27510472c21bSThierry Reding 
27520472c21bSThierry Reding 	usleep_range(20, 100);
27530472c21bSThierry Reding 
27540472c21bSThierry Reding 	err = drm_dp_aux_enable(sor->aux);
27550472c21bSThierry Reding 	if (err < 0)
27560472c21bSThierry Reding 		dev_err(sor->dev, "failed to enable DPAUX: %d\n", err);
27570472c21bSThierry Reding 
27580472c21bSThierry Reding 	err = drm_dp_link_probe(sor->aux, &sor->link);
27590472c21bSThierry Reding 	if (err < 0)
27600472c21bSThierry Reding 		dev_err(sor->dev, "failed to probe DP link: %d\n", err);
27610472c21bSThierry Reding 
2762d278e4a9SThierry Reding 	tegra_sor_filter_rates(sor);
2763d278e4a9SThierry Reding 
27640472c21bSThierry Reding 	err = drm_dp_link_choose(&sor->link, mode, info);
27650472c21bSThierry Reding 	if (err < 0)
27660472c21bSThierry Reding 		dev_err(sor->dev, "failed to choose link: %d\n", err);
27670472c21bSThierry Reding 
2768d278e4a9SThierry Reding 	if (output->panel)
2769d278e4a9SThierry Reding 		drm_panel_prepare(output->panel);
2770d278e4a9SThierry Reding 
27710472c21bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
27720472c21bSThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
27730472c21bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
27740472c21bSThierry Reding 
27750472c21bSThierry Reding 	usleep_range(20, 40);
27760472c21bSThierry Reding 
27770472c21bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll3);
27780472c21bSThierry Reding 	value |= SOR_PLL3_PLL_VDD_MODE_3V3;
27790472c21bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll3);
27800472c21bSThierry Reding 
27810472c21bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
27820472c21bSThierry Reding 	value &= ~(SOR_PLL0_VCOPD | SOR_PLL0_PWR);
27830472c21bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
27840472c21bSThierry Reding 
27850472c21bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
27860472c21bSThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
27870472c21bSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD;
27880472c21bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
27890472c21bSThierry Reding 
27900472c21bSThierry Reding 	usleep_range(200, 400);
27910472c21bSThierry Reding 
27920472c21bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
27930472c21bSThierry Reding 	value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
27940472c21bSThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
27950472c21bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
27960472c21bSThierry Reding 
27970472c21bSThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
27980472c21bSThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
2799d278e4a9SThierry Reding 
2800d278e4a9SThierry Reding 	if (output->panel)
2801d278e4a9SThierry Reding 		value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
2802d278e4a9SThierry Reding 	else
28030472c21bSThierry Reding 		value |= SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_DPCLK;
2804d278e4a9SThierry Reding 
28050472c21bSThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
28060472c21bSThierry Reding 
28070472c21bSThierry Reding 	usleep_range(200, 400);
28080472c21bSThierry Reding 
28090472c21bSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
28100472c21bSThierry Reding 	/* XXX not in TRM */
2811d278e4a9SThierry Reding 	if (output->panel)
2812d278e4a9SThierry Reding 		value |= SOR_DP_SPARE_PANEL_INTERNAL;
2813d278e4a9SThierry Reding 	else
28140472c21bSThierry Reding 		value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
2815d278e4a9SThierry Reding 
28160472c21bSThierry Reding 	value |= SOR_DP_SPARE_SEQ_ENABLE;
28170472c21bSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
28180472c21bSThierry Reding 
28190472c21bSThierry Reding 	/* XXX not in TRM */
28200472c21bSThierry Reding 	tegra_sor_writel(sor, 0, SOR_LVDS);
28210472c21bSThierry Reding 
28220472c21bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
28230472c21bSThierry Reding 	value &= ~SOR_PLL0_ICHPMP_MASK;
28240472c21bSThierry Reding 	value &= ~SOR_PLL0_VCOCAP_MASK;
28250472c21bSThierry Reding 	value |= SOR_PLL0_ICHPMP(0x1);
28260472c21bSThierry Reding 	value |= SOR_PLL0_VCOCAP(0x3);
28270472c21bSThierry Reding 	value |= SOR_PLL0_RESISTOR_EXT;
28280472c21bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
28290472c21bSThierry Reding 
28300472c21bSThierry Reding 	/* XXX not in TRM */
28310472c21bSThierry Reding 	for (value = 0, i = 0; i < 5; i++)
28320472c21bSThierry Reding 		value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->soc->xbar_cfg[i]) |
28330472c21bSThierry Reding 			 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
28340472c21bSThierry Reding 
28350472c21bSThierry Reding 	tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
28360472c21bSThierry Reding 	tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
28370472c21bSThierry Reding 
283861417aaaSThierry Reding 	/*
283961417aaaSThierry Reding 	 * Switch the pad clock to the DP clock. Note that we cannot actually
284061417aaaSThierry Reding 	 * do this because Tegra186 and later don't support clk_set_parent()
284161417aaaSThierry Reding 	 * on the sorX_pad_clkout clocks. We already do the equivalent above
284261417aaaSThierry Reding 	 * using the DP_CLK_SEL mux of the SOR_CLK_CNTRL register.
284361417aaaSThierry Reding 	 */
284461417aaaSThierry Reding #if 0
284561417aaaSThierry Reding 	err = clk_set_parent(sor->clk_pad, sor->clk_parent);
284661417aaaSThierry Reding 	if (err < 0) {
284761417aaaSThierry Reding 		dev_err(sor->dev, "failed to select pad parent clock: %d\n",
284861417aaaSThierry Reding 			err);
284961417aaaSThierry Reding 		return;
285061417aaaSThierry Reding 	}
285161417aaaSThierry Reding #endif
285261417aaaSThierry Reding 
285361417aaaSThierry Reding 	/* switch the SOR clock to the pad clock */
28540472c21bSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_pad);
28550472c21bSThierry Reding 	if (err < 0) {
285661417aaaSThierry Reding 		dev_err(sor->dev, "failed to select SOR parent clock: %d\n",
285761417aaaSThierry Reding 			err);
28580472c21bSThierry Reding 		return;
28590472c21bSThierry Reding 	}
28600472c21bSThierry Reding 
286161417aaaSThierry Reding 	/* switch the output clock to the parent pixel clock */
28620472c21bSThierry Reding 	err = clk_set_parent(sor->clk, sor->clk_parent);
28630472c21bSThierry Reding 	if (err < 0) {
286461417aaaSThierry Reding 		dev_err(sor->dev, "failed to select output parent clock: %d\n",
286561417aaaSThierry Reding 			err);
28660472c21bSThierry Reding 		return;
28670472c21bSThierry Reding 	}
28680472c21bSThierry Reding 
28690472c21bSThierry Reding 	/* use DP-A protocol */
28700472c21bSThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
28710472c21bSThierry Reding 	value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
28720472c21bSThierry Reding 	value |= SOR_STATE_ASY_PROTOCOL_DP_A;
28730472c21bSThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
28740472c21bSThierry Reding 
28750472c21bSThierry Reding 	/* enable port */
28760472c21bSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
28770472c21bSThierry Reding 	value |= SOR_DP_LINKCTL_ENABLE;
28780472c21bSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
28790472c21bSThierry Reding 
28800472c21bSThierry Reding 	tegra_sor_dp_term_calibrate(sor);
28810472c21bSThierry Reding 
28820472c21bSThierry Reding 	err = drm_dp_link_train(&sor->link);
28830472c21bSThierry Reding 	if (err < 0)
28840472c21bSThierry Reding 		dev_err(sor->dev, "link training failed: %d\n", err);
28850472c21bSThierry Reding 	else
28860472c21bSThierry Reding 		dev_dbg(sor->dev, "link training succeeded\n");
28870472c21bSThierry Reding 
28880472c21bSThierry Reding 	err = drm_dp_link_power_up(sor->aux, &sor->link);
28890472c21bSThierry Reding 	if (err < 0)
28900472c21bSThierry Reding 		dev_err(sor->dev, "failed to power up DP link: %d\n", err);
28910472c21bSThierry Reding 
28920472c21bSThierry Reding 	/* compute configuration */
28930472c21bSThierry Reding 	memset(&config, 0, sizeof(config));
28940472c21bSThierry Reding 	config.bits_per_pixel = state->bpc * 3;
28950472c21bSThierry Reding 
28960472c21bSThierry Reding 	err = tegra_sor_compute_config(sor, mode, &config, &sor->link);
28970472c21bSThierry Reding 	if (err < 0)
28980472c21bSThierry Reding 		dev_err(sor->dev, "failed to compute configuration: %d\n", err);
28990472c21bSThierry Reding 
29000472c21bSThierry Reding 	tegra_sor_apply_config(sor, &config);
29010472c21bSThierry Reding 	tegra_sor_mode_set(sor, mode, state);
2902d278e4a9SThierry Reding 
2903d278e4a9SThierry Reding 	if (output->panel) {
2904d278e4a9SThierry Reding 		/* CSTM (LVDS, link A/B, upper) */
2905d278e4a9SThierry Reding 		value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
2906d278e4a9SThierry Reding 			SOR_CSTM_UPPER;
2907d278e4a9SThierry Reding 		tegra_sor_writel(sor, value, SOR_CSTM);
2908d278e4a9SThierry Reding 
2909d278e4a9SThierry Reding 		/* PWM setup */
2910d278e4a9SThierry Reding 		err = tegra_sor_setup_pwm(sor, 250);
2911d278e4a9SThierry Reding 		if (err < 0)
2912d278e4a9SThierry Reding 			dev_err(sor->dev, "failed to setup PWM: %d\n", err);
2913d278e4a9SThierry Reding 	}
2914d278e4a9SThierry Reding 
29150472c21bSThierry Reding 	tegra_sor_update(sor);
29160472c21bSThierry Reding 
29170472c21bSThierry Reding 	err = tegra_sor_power_up(sor, 250);
29180472c21bSThierry Reding 	if (err < 0)
29190472c21bSThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
29200472c21bSThierry Reding 
29210472c21bSThierry Reding 	/* attach and wake up */
29220472c21bSThierry Reding 	err = tegra_sor_attach(sor);
29230472c21bSThierry Reding 	if (err < 0)
29240472c21bSThierry Reding 		dev_err(sor->dev, "failed to attach SOR: %d\n", err);
29250472c21bSThierry Reding 
29260472c21bSThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
29270472c21bSThierry Reding 	value |= SOR_ENABLE(sor->index);
29280472c21bSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
29290472c21bSThierry Reding 
29300472c21bSThierry Reding 	tegra_dc_commit(dc);
29310472c21bSThierry Reding 
29320472c21bSThierry Reding 	err = tegra_sor_wakeup(sor);
29330472c21bSThierry Reding 	if (err < 0)
29340472c21bSThierry Reding 		dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
2935d278e4a9SThierry Reding 
2936d278e4a9SThierry Reding 	if (output->panel)
2937d278e4a9SThierry Reding 		drm_panel_enable(output->panel);
29380472c21bSThierry Reding }
29390472c21bSThierry Reding 
29400472c21bSThierry Reding static const struct drm_encoder_helper_funcs tegra_sor_dp_helpers = {
29410472c21bSThierry Reding 	.disable = tegra_sor_dp_disable,
29420472c21bSThierry Reding 	.enable = tegra_sor_dp_enable,
29430472c21bSThierry Reding 	.atomic_check = tegra_sor_encoder_atomic_check,
29440472c21bSThierry Reding };
29450472c21bSThierry Reding 
29461c3cc0dfSThierry Reding static int tegra_sor_hdmi_probe(struct tegra_sor *sor)
29471c3cc0dfSThierry Reding {
29481c3cc0dfSThierry Reding 	int err;
29491c3cc0dfSThierry Reding 
29501c3cc0dfSThierry Reding 	sor->avdd_io_supply = devm_regulator_get(sor->dev, "avdd-io");
29511c3cc0dfSThierry Reding 	if (IS_ERR(sor->avdd_io_supply)) {
29521c3cc0dfSThierry Reding 		dev_err(sor->dev, "cannot get AVDD I/O supply: %ld\n",
29531c3cc0dfSThierry Reding 			PTR_ERR(sor->avdd_io_supply));
29541c3cc0dfSThierry Reding 		return PTR_ERR(sor->avdd_io_supply);
29551c3cc0dfSThierry Reding 	}
29561c3cc0dfSThierry Reding 
29571c3cc0dfSThierry Reding 	err = regulator_enable(sor->avdd_io_supply);
29581c3cc0dfSThierry Reding 	if (err < 0) {
29591c3cc0dfSThierry Reding 		dev_err(sor->dev, "failed to enable AVDD I/O supply: %d\n",
29601c3cc0dfSThierry Reding 			err);
29611c3cc0dfSThierry Reding 		return err;
29621c3cc0dfSThierry Reding 	}
29631c3cc0dfSThierry Reding 
29641c3cc0dfSThierry Reding 	sor->vdd_pll_supply = devm_regulator_get(sor->dev, "vdd-pll");
29651c3cc0dfSThierry Reding 	if (IS_ERR(sor->vdd_pll_supply)) {
29661c3cc0dfSThierry Reding 		dev_err(sor->dev, "cannot get VDD PLL supply: %ld\n",
29671c3cc0dfSThierry Reding 			PTR_ERR(sor->vdd_pll_supply));
29681c3cc0dfSThierry Reding 		return PTR_ERR(sor->vdd_pll_supply);
29691c3cc0dfSThierry Reding 	}
29701c3cc0dfSThierry Reding 
29711c3cc0dfSThierry Reding 	err = regulator_enable(sor->vdd_pll_supply);
29721c3cc0dfSThierry Reding 	if (err < 0) {
29731c3cc0dfSThierry Reding 		dev_err(sor->dev, "failed to enable VDD PLL supply: %d\n",
29741c3cc0dfSThierry Reding 			err);
29751c3cc0dfSThierry Reding 		return err;
29761c3cc0dfSThierry Reding 	}
29771c3cc0dfSThierry Reding 
29781c3cc0dfSThierry Reding 	sor->hdmi_supply = devm_regulator_get(sor->dev, "hdmi");
29791c3cc0dfSThierry Reding 	if (IS_ERR(sor->hdmi_supply)) {
29801c3cc0dfSThierry Reding 		dev_err(sor->dev, "cannot get HDMI supply: %ld\n",
29811c3cc0dfSThierry Reding 			PTR_ERR(sor->hdmi_supply));
29821c3cc0dfSThierry Reding 		return PTR_ERR(sor->hdmi_supply);
29831c3cc0dfSThierry Reding 	}
29841c3cc0dfSThierry Reding 
29851c3cc0dfSThierry Reding 	err = regulator_enable(sor->hdmi_supply);
29861c3cc0dfSThierry Reding 	if (err < 0) {
29871c3cc0dfSThierry Reding 		dev_err(sor->dev, "failed to enable HDMI supply: %d\n", err);
29881c3cc0dfSThierry Reding 		return err;
29891c3cc0dfSThierry Reding 	}
29901c3cc0dfSThierry Reding 
29911c3cc0dfSThierry Reding 	INIT_DELAYED_WORK(&sor->scdc, tegra_sor_hdmi_scdc_work);
29921c3cc0dfSThierry Reding 
29931c3cc0dfSThierry Reding 	return 0;
29941c3cc0dfSThierry Reding }
29951c3cc0dfSThierry Reding 
29961c3cc0dfSThierry Reding static int tegra_sor_hdmi_remove(struct tegra_sor *sor)
29971c3cc0dfSThierry Reding {
29981c3cc0dfSThierry Reding 	regulator_disable(sor->hdmi_supply);
29991c3cc0dfSThierry Reding 	regulator_disable(sor->vdd_pll_supply);
30001c3cc0dfSThierry Reding 	regulator_disable(sor->avdd_io_supply);
30011c3cc0dfSThierry Reding 
30021c3cc0dfSThierry Reding 	return 0;
30031c3cc0dfSThierry Reding }
30041c3cc0dfSThierry Reding 
30051c3cc0dfSThierry Reding static const struct tegra_sor_ops tegra_sor_hdmi_ops = {
30061c3cc0dfSThierry Reding 	.name = "HDMI",
30071c3cc0dfSThierry Reding 	.probe = tegra_sor_hdmi_probe,
30081c3cc0dfSThierry Reding 	.remove = tegra_sor_hdmi_remove,
300985d0c4b5SThierry Reding 	.audio_enable = tegra_sor_hdmi_audio_enable,
301085d0c4b5SThierry Reding 	.audio_disable = tegra_sor_hdmi_audio_disable,
30111c3cc0dfSThierry Reding };
30121c3cc0dfSThierry Reding 
30131c3cc0dfSThierry Reding static int tegra_sor_dp_probe(struct tegra_sor *sor)
30141c3cc0dfSThierry Reding {
30151c3cc0dfSThierry Reding 	int err;
30161c3cc0dfSThierry Reding 
30171c3cc0dfSThierry Reding 	sor->avdd_io_supply = devm_regulator_get(sor->dev, "avdd-io-hdmi-dp");
30181c3cc0dfSThierry Reding 	if (IS_ERR(sor->avdd_io_supply))
30191c3cc0dfSThierry Reding 		return PTR_ERR(sor->avdd_io_supply);
30201c3cc0dfSThierry Reding 
30211c3cc0dfSThierry Reding 	err = regulator_enable(sor->avdd_io_supply);
30221c3cc0dfSThierry Reding 	if (err < 0)
30231c3cc0dfSThierry Reding 		return err;
30241c3cc0dfSThierry Reding 
30251c3cc0dfSThierry Reding 	sor->vdd_pll_supply = devm_regulator_get(sor->dev, "vdd-hdmi-dp-pll");
30261c3cc0dfSThierry Reding 	if (IS_ERR(sor->vdd_pll_supply))
30271c3cc0dfSThierry Reding 		return PTR_ERR(sor->vdd_pll_supply);
30281c3cc0dfSThierry Reding 
30291c3cc0dfSThierry Reding 	err = regulator_enable(sor->vdd_pll_supply);
30301c3cc0dfSThierry Reding 	if (err < 0)
30311c3cc0dfSThierry Reding 		return err;
30321c3cc0dfSThierry Reding 
30331c3cc0dfSThierry Reding 	return 0;
30341c3cc0dfSThierry Reding }
30351c3cc0dfSThierry Reding 
30361c3cc0dfSThierry Reding static int tegra_sor_dp_remove(struct tegra_sor *sor)
30371c3cc0dfSThierry Reding {
30381c3cc0dfSThierry Reding 	regulator_disable(sor->vdd_pll_supply);
30391c3cc0dfSThierry Reding 	regulator_disable(sor->avdd_io_supply);
30401c3cc0dfSThierry Reding 
30411c3cc0dfSThierry Reding 	return 0;
30421c3cc0dfSThierry Reding }
30431c3cc0dfSThierry Reding 
30441c3cc0dfSThierry Reding static const struct tegra_sor_ops tegra_sor_dp_ops = {
30451c3cc0dfSThierry Reding 	.name = "DP",
30461c3cc0dfSThierry Reding 	.probe = tegra_sor_dp_probe,
30471c3cc0dfSThierry Reding 	.remove = tegra_sor_dp_remove,
30481c3cc0dfSThierry Reding };
30491c3cc0dfSThierry Reding 
30506b6b6042SThierry Reding static int tegra_sor_init(struct host1x_client *client)
30516b6b6042SThierry Reding {
3052608f43adSThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->host);
3053459cc2c6SThierry Reding 	const struct drm_encoder_helper_funcs *helpers = NULL;
30546b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
3055459cc2c6SThierry Reding 	int connector = DRM_MODE_CONNECTOR_Unknown;
3056459cc2c6SThierry Reding 	int encoder = DRM_MODE_ENCODER_NONE;
30576b6b6042SThierry Reding 	int err;
30586b6b6042SThierry Reding 
30599542c237SThierry Reding 	if (!sor->aux) {
30601c3cc0dfSThierry Reding 		if (sor->ops == &tegra_sor_hdmi_ops) {
3061459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_HDMIA;
3062459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
3063459cc2c6SThierry Reding 			helpers = &tegra_sor_hdmi_helpers;
3064459cc2c6SThierry Reding 		} else if (sor->soc->supports_lvds) {
3065459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_LVDS;
3066459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_LVDS;
3067459cc2c6SThierry Reding 		}
3068459cc2c6SThierry Reding 	} else {
3069d278e4a9SThierry Reding 		if (sor->output.panel) {
3070459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_eDP;
3071459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
3072d278e4a9SThierry Reding 			helpers = &tegra_sor_dp_helpers;
30731c3cc0dfSThierry Reding 		} else {
3074459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_DisplayPort;
3075459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
30760472c21bSThierry Reding 			helpers = &tegra_sor_dp_helpers;
3077459cc2c6SThierry Reding 		}
3078c1763937SThierry Reding 
3079c1763937SThierry Reding 		sor->link.ops = &tegra_sor_dp_link_ops;
3080c1763937SThierry Reding 		sor->link.aux = sor->aux;
3081459cc2c6SThierry Reding 	}
30826b6b6042SThierry Reding 
30836b6b6042SThierry Reding 	sor->output.dev = sor->dev;
30846b6b6042SThierry Reding 
3085bb9d681bSAndrzej Pietrasiewicz 	drm_connector_init_with_ddc(drm, &sor->output.connector,
30866fad8f66SThierry Reding 				    &tegra_sor_connector_funcs,
3087bb9d681bSAndrzej Pietrasiewicz 				    connector,
3088bb9d681bSAndrzej Pietrasiewicz 				    sor->output.ddc);
30896fad8f66SThierry Reding 	drm_connector_helper_add(&sor->output.connector,
30906fad8f66SThierry Reding 				 &tegra_sor_connector_helper_funcs);
30916fad8f66SThierry Reding 	sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
30926fad8f66SThierry Reding 
3093*4d0e95e0SThomas Zimmermann 	drm_simple_encoder_init(drm, &sor->output.encoder, encoder);
3094459cc2c6SThierry Reding 	drm_encoder_helper_add(&sor->output.encoder, helpers);
30956fad8f66SThierry Reding 
3096cde4c44dSDaniel Vetter 	drm_connector_attach_encoder(&sor->output.connector,
30976fad8f66SThierry Reding 					  &sor->output.encoder);
30986fad8f66SThierry Reding 	drm_connector_register(&sor->output.connector);
30996fad8f66SThierry Reding 
3100ea130b24SThierry Reding 	err = tegra_output_init(drm, &sor->output);
3101ea130b24SThierry Reding 	if (err < 0) {
3102ea130b24SThierry Reding 		dev_err(client->dev, "failed to initialize output: %d\n", err);
3103ea130b24SThierry Reding 		return err;
3104ea130b24SThierry Reding 	}
31056fad8f66SThierry Reding 
3106c57997bcSThierry Reding 	tegra_output_find_possible_crtcs(&sor->output, drm);
31076b6b6042SThierry Reding 
31089542c237SThierry Reding 	if (sor->aux) {
31099542c237SThierry Reding 		err = drm_dp_aux_attach(sor->aux, &sor->output);
31106b6b6042SThierry Reding 		if (err < 0) {
31116b6b6042SThierry Reding 			dev_err(sor->dev, "failed to attach DP: %d\n", err);
31126b6b6042SThierry Reding 			return err;
31136b6b6042SThierry Reding 		}
31146b6b6042SThierry Reding 	}
31156b6b6042SThierry Reding 
3116535a65dbSTomeu Vizoso 	/*
3117535a65dbSTomeu Vizoso 	 * XXX: Remove this reset once proper hand-over from firmware to
3118535a65dbSTomeu Vizoso 	 * kernel is possible.
3119535a65dbSTomeu Vizoso 	 */
3120f8c79120SJon Hunter 	if (sor->rst) {
312111c632e1SThierry Reding 		err = reset_control_acquire(sor->rst);
312211c632e1SThierry Reding 		if (err < 0) {
312311c632e1SThierry Reding 			dev_err(sor->dev, "failed to acquire SOR reset: %d\n",
312411c632e1SThierry Reding 				err);
312511c632e1SThierry Reding 			return err;
312611c632e1SThierry Reding 		}
312711c632e1SThierry Reding 
3128535a65dbSTomeu Vizoso 		err = reset_control_assert(sor->rst);
3129535a65dbSTomeu Vizoso 		if (err < 0) {
3130f8c79120SJon Hunter 			dev_err(sor->dev, "failed to assert SOR reset: %d\n",
3131f8c79120SJon Hunter 				err);
3132535a65dbSTomeu Vizoso 			return err;
3133535a65dbSTomeu Vizoso 		}
3134f8c79120SJon Hunter 	}
3135535a65dbSTomeu Vizoso 
31366fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk);
31376fad8f66SThierry Reding 	if (err < 0) {
31386fad8f66SThierry Reding 		dev_err(sor->dev, "failed to enable clock: %d\n", err);
31396fad8f66SThierry Reding 		return err;
31406fad8f66SThierry Reding 	}
31416fad8f66SThierry Reding 
3142535a65dbSTomeu Vizoso 	usleep_range(1000, 3000);
3143535a65dbSTomeu Vizoso 
3144f8c79120SJon Hunter 	if (sor->rst) {
3145535a65dbSTomeu Vizoso 		err = reset_control_deassert(sor->rst);
3146535a65dbSTomeu Vizoso 		if (err < 0) {
3147f8c79120SJon Hunter 			dev_err(sor->dev, "failed to deassert SOR reset: %d\n",
3148f8c79120SJon Hunter 				err);
3149535a65dbSTomeu Vizoso 			return err;
3150535a65dbSTomeu Vizoso 		}
315111c632e1SThierry Reding 
315211c632e1SThierry Reding 		reset_control_release(sor->rst);
3153f8c79120SJon Hunter 	}
3154535a65dbSTomeu Vizoso 
31556fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk_safe);
31566fad8f66SThierry Reding 	if (err < 0)
31576fad8f66SThierry Reding 		return err;
31586fad8f66SThierry Reding 
31596fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk_dp);
31606fad8f66SThierry Reding 	if (err < 0)
31616fad8f66SThierry Reding 		return err;
31626fad8f66SThierry Reding 
31636b6b6042SThierry Reding 	return 0;
31646b6b6042SThierry Reding }
31656b6b6042SThierry Reding 
31666b6b6042SThierry Reding static int tegra_sor_exit(struct host1x_client *client)
31676b6b6042SThierry Reding {
31686b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
31696b6b6042SThierry Reding 	int err;
31706b6b6042SThierry Reding 
3171328ec69eSThierry Reding 	tegra_output_exit(&sor->output);
3172328ec69eSThierry Reding 
31739542c237SThierry Reding 	if (sor->aux) {
31749542c237SThierry Reding 		err = drm_dp_aux_detach(sor->aux);
31756b6b6042SThierry Reding 		if (err < 0) {
31766b6b6042SThierry Reding 			dev_err(sor->dev, "failed to detach DP: %d\n", err);
31776b6b6042SThierry Reding 			return err;
31786b6b6042SThierry Reding 		}
31796b6b6042SThierry Reding 	}
31806b6b6042SThierry Reding 
31816fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk_safe);
31826fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk_dp);
31836fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk);
31846fad8f66SThierry Reding 
31856b6b6042SThierry Reding 	return 0;
31866b6b6042SThierry Reding }
31876b6b6042SThierry Reding 
3188fd67e9c6SThierry Reding static int tegra_sor_runtime_suspend(struct host1x_client *client)
3189fd67e9c6SThierry Reding {
3190fd67e9c6SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
3191fd67e9c6SThierry Reding 	struct device *dev = client->dev;
3192fd67e9c6SThierry Reding 	int err;
3193fd67e9c6SThierry Reding 
3194fd67e9c6SThierry Reding 	if (sor->rst) {
3195fd67e9c6SThierry Reding 		err = reset_control_assert(sor->rst);
3196fd67e9c6SThierry Reding 		if (err < 0) {
3197fd67e9c6SThierry Reding 			dev_err(dev, "failed to assert reset: %d\n", err);
3198fd67e9c6SThierry Reding 			return err;
3199fd67e9c6SThierry Reding 		}
3200fd67e9c6SThierry Reding 
3201fd67e9c6SThierry Reding 		reset_control_release(sor->rst);
3202fd67e9c6SThierry Reding 	}
3203fd67e9c6SThierry Reding 
3204fd67e9c6SThierry Reding 	usleep_range(1000, 2000);
3205fd67e9c6SThierry Reding 
3206fd67e9c6SThierry Reding 	clk_disable_unprepare(sor->clk);
3207fd67e9c6SThierry Reding 	pm_runtime_put_sync(dev);
3208fd67e9c6SThierry Reding 
3209fd67e9c6SThierry Reding 	return 0;
3210fd67e9c6SThierry Reding }
3211fd67e9c6SThierry Reding 
3212fd67e9c6SThierry Reding static int tegra_sor_runtime_resume(struct host1x_client *client)
3213fd67e9c6SThierry Reding {
3214fd67e9c6SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
3215fd67e9c6SThierry Reding 	struct device *dev = client->dev;
3216fd67e9c6SThierry Reding 	int err;
3217fd67e9c6SThierry Reding 
3218fd67e9c6SThierry Reding 	err = pm_runtime_get_sync(dev);
3219fd67e9c6SThierry Reding 	if (err < 0) {
3220fd67e9c6SThierry Reding 		dev_err(dev, "failed to get runtime PM: %d\n", err);
3221fd67e9c6SThierry Reding 		return err;
3222fd67e9c6SThierry Reding 	}
3223fd67e9c6SThierry Reding 
3224fd67e9c6SThierry Reding 	err = clk_prepare_enable(sor->clk);
3225fd67e9c6SThierry Reding 	if (err < 0) {
3226fd67e9c6SThierry Reding 		dev_err(dev, "failed to enable clock: %d\n", err);
3227fd67e9c6SThierry Reding 		goto put_rpm;
3228fd67e9c6SThierry Reding 	}
3229fd67e9c6SThierry Reding 
3230fd67e9c6SThierry Reding 	usleep_range(1000, 2000);
3231fd67e9c6SThierry Reding 
3232fd67e9c6SThierry Reding 	if (sor->rst) {
3233fd67e9c6SThierry Reding 		err = reset_control_acquire(sor->rst);
3234fd67e9c6SThierry Reding 		if (err < 0) {
3235fd67e9c6SThierry Reding 			dev_err(dev, "failed to acquire reset: %d\n", err);
3236fd67e9c6SThierry Reding 			goto disable_clk;
3237fd67e9c6SThierry Reding 		}
3238fd67e9c6SThierry Reding 
3239fd67e9c6SThierry Reding 		err = reset_control_deassert(sor->rst);
3240fd67e9c6SThierry Reding 		if (err < 0) {
3241fd67e9c6SThierry Reding 			dev_err(dev, "failed to deassert reset: %d\n", err);
3242fd67e9c6SThierry Reding 			goto release_reset;
3243fd67e9c6SThierry Reding 		}
3244fd67e9c6SThierry Reding 	}
3245fd67e9c6SThierry Reding 
3246fd67e9c6SThierry Reding 	return 0;
3247fd67e9c6SThierry Reding 
3248fd67e9c6SThierry Reding release_reset:
3249fd67e9c6SThierry Reding 	reset_control_release(sor->rst);
3250fd67e9c6SThierry Reding disable_clk:
3251fd67e9c6SThierry Reding 	clk_disable_unprepare(sor->clk);
3252fd67e9c6SThierry Reding put_rpm:
3253fd67e9c6SThierry Reding 	pm_runtime_put_sync(dev);
3254fd67e9c6SThierry Reding 	return err;
3255fd67e9c6SThierry Reding }
3256fd67e9c6SThierry Reding 
32576b6b6042SThierry Reding static const struct host1x_client_ops sor_client_ops = {
32586b6b6042SThierry Reding 	.init = tegra_sor_init,
32596b6b6042SThierry Reding 	.exit = tegra_sor_exit,
3260fd67e9c6SThierry Reding 	.suspend = tegra_sor_runtime_suspend,
3261fd67e9c6SThierry Reding 	.resume = tegra_sor_runtime_resume,
32626b6b6042SThierry Reding };
32636b6b6042SThierry Reding 
326430b49435SThierry Reding static const u8 tegra124_sor_xbar_cfg[5] = {
326530b49435SThierry Reding 	0, 1, 2, 3, 4
326630b49435SThierry Reding };
326730b49435SThierry Reding 
3268880cee0bSThierry Reding static const struct tegra_sor_regs tegra124_sor_regs = {
3269880cee0bSThierry Reding 	.head_state0 = 0x05,
3270880cee0bSThierry Reding 	.head_state1 = 0x07,
3271880cee0bSThierry Reding 	.head_state2 = 0x09,
3272880cee0bSThierry Reding 	.head_state3 = 0x0b,
3273880cee0bSThierry Reding 	.head_state4 = 0x0d,
3274880cee0bSThierry Reding 	.head_state5 = 0x0f,
3275880cee0bSThierry Reding 	.pll0 = 0x17,
3276880cee0bSThierry Reding 	.pll1 = 0x18,
3277880cee0bSThierry Reding 	.pll2 = 0x19,
3278880cee0bSThierry Reding 	.pll3 = 0x1a,
3279880cee0bSThierry Reding 	.dp_padctl0 = 0x5c,
3280880cee0bSThierry Reding 	.dp_padctl2 = 0x73,
3281880cee0bSThierry Reding };
3282880cee0bSThierry Reding 
3283c1763937SThierry Reding /* Tegra124 and Tegra132 have lanes 0 and 2 swapped. */
3284c1763937SThierry Reding static const u8 tegra124_sor_lane_map[4] = {
3285c1763937SThierry Reding 	2, 1, 0, 3,
3286c1763937SThierry Reding };
3287c1763937SThierry Reding 
3288c1763937SThierry Reding static const u8 tegra124_sor_voltage_swing[4][4][4] = {
3289c1763937SThierry Reding 	{
3290c1763937SThierry Reding 		{ 0x13, 0x19, 0x1e, 0x28 },
3291c1763937SThierry Reding 		{ 0x1e, 0x25, 0x2d, },
3292c1763937SThierry Reding 		{ 0x28, 0x32, },
3293c1763937SThierry Reding 		{ 0x3c, },
3294c1763937SThierry Reding 	}, {
3295c1763937SThierry Reding 		{ 0x12, 0x17, 0x1b, 0x25 },
3296c1763937SThierry Reding 		{ 0x1c, 0x23, 0x2a, },
3297c1763937SThierry Reding 		{ 0x25, 0x2f, },
3298c1763937SThierry Reding 		{ 0x39, }
3299c1763937SThierry Reding 	}, {
3300c1763937SThierry Reding 		{ 0x12, 0x16, 0x1a, 0x22 },
3301c1763937SThierry Reding 		{ 0x1b, 0x20, 0x27, },
3302c1763937SThierry Reding 		{ 0x24, 0x2d, },
3303c1763937SThierry Reding 		{ 0x36, },
3304c1763937SThierry Reding 	}, {
3305c1763937SThierry Reding 		{ 0x11, 0x14, 0x17, 0x1f },
3306c1763937SThierry Reding 		{ 0x19, 0x1e, 0x24, },
3307c1763937SThierry Reding 		{ 0x22, 0x2a, },
3308c1763937SThierry Reding 		{ 0x32, },
3309c1763937SThierry Reding 	},
3310c1763937SThierry Reding };
3311c1763937SThierry Reding 
3312c1763937SThierry Reding static const u8 tegra124_sor_pre_emphasis[4][4][4] = {
3313c1763937SThierry Reding 	{
3314c1763937SThierry Reding 		{ 0x00, 0x09, 0x13, 0x25 },
3315c1763937SThierry Reding 		{ 0x00, 0x0f, 0x1e, },
3316c1763937SThierry Reding 		{ 0x00, 0x14, },
3317c1763937SThierry Reding 		{ 0x00, },
3318c1763937SThierry Reding 	}, {
3319c1763937SThierry Reding 		{ 0x00, 0x0a, 0x14, 0x28 },
3320c1763937SThierry Reding 		{ 0x00, 0x0f, 0x1e, },
3321c1763937SThierry Reding 		{ 0x00, 0x14, },
3322c1763937SThierry Reding 		{ 0x00 },
3323c1763937SThierry Reding 	}, {
3324c1763937SThierry Reding 		{ 0x00, 0x0a, 0x14, 0x28 },
3325c1763937SThierry Reding 		{ 0x00, 0x0f, 0x1e, },
3326c1763937SThierry Reding 		{ 0x00, 0x14, },
3327c1763937SThierry Reding 		{ 0x00, },
3328c1763937SThierry Reding 	}, {
3329c1763937SThierry Reding 		{ 0x00, 0x0a, 0x14, 0x28 },
3330c1763937SThierry Reding 		{ 0x00, 0x0f, 0x1e, },
3331c1763937SThierry Reding 		{ 0x00, 0x14, },
3332c1763937SThierry Reding 		{ 0x00, },
3333c1763937SThierry Reding 	},
3334c1763937SThierry Reding };
3335c1763937SThierry Reding 
3336c1763937SThierry Reding static const u8 tegra124_sor_post_cursor[4][4][4] = {
3337c1763937SThierry Reding 	{
3338c1763937SThierry Reding 		{ 0x00, 0x00, 0x00, 0x00 },
3339c1763937SThierry Reding 		{ 0x00, 0x00, 0x00, },
3340c1763937SThierry Reding 		{ 0x00, 0x00, },
3341c1763937SThierry Reding 		{ 0x00, },
3342c1763937SThierry Reding 	}, {
3343c1763937SThierry Reding 		{ 0x02, 0x02, 0x04, 0x05 },
3344c1763937SThierry Reding 		{ 0x02, 0x04, 0x05, },
3345c1763937SThierry Reding 		{ 0x04, 0x05, },
3346c1763937SThierry Reding 		{ 0x05, },
3347c1763937SThierry Reding 	}, {
3348c1763937SThierry Reding 		{ 0x04, 0x05, 0x08, 0x0b },
3349c1763937SThierry Reding 		{ 0x05, 0x09, 0x0b, },
3350c1763937SThierry Reding 		{ 0x08, 0x0a, },
3351c1763937SThierry Reding 		{ 0x0b, },
3352c1763937SThierry Reding 	}, {
3353c1763937SThierry Reding 		{ 0x05, 0x09, 0x0b, 0x12 },
3354c1763937SThierry Reding 		{ 0x09, 0x0d, 0x12, },
3355c1763937SThierry Reding 		{ 0x0b, 0x0f, },
3356c1763937SThierry Reding 		{ 0x12, },
3357c1763937SThierry Reding 	},
3358c1763937SThierry Reding };
3359c1763937SThierry Reding 
3360c1763937SThierry Reding static const u8 tegra124_sor_tx_pu[4][4][4] = {
3361c1763937SThierry Reding 	{
3362c1763937SThierry Reding 		{ 0x20, 0x30, 0x40, 0x60 },
3363c1763937SThierry Reding 		{ 0x30, 0x40, 0x60, },
3364c1763937SThierry Reding 		{ 0x40, 0x60, },
3365c1763937SThierry Reding 		{ 0x60, },
3366c1763937SThierry Reding 	}, {
3367c1763937SThierry Reding 		{ 0x20, 0x20, 0x30, 0x50 },
3368c1763937SThierry Reding 		{ 0x30, 0x40, 0x50, },
3369c1763937SThierry Reding 		{ 0x40, 0x50, },
3370c1763937SThierry Reding 		{ 0x60, },
3371c1763937SThierry Reding 	}, {
3372c1763937SThierry Reding 		{ 0x20, 0x20, 0x30, 0x40, },
3373c1763937SThierry Reding 		{ 0x30, 0x30, 0x40, },
3374c1763937SThierry Reding 		{ 0x40, 0x50, },
3375c1763937SThierry Reding 		{ 0x60, },
3376c1763937SThierry Reding 	}, {
3377c1763937SThierry Reding 		{ 0x20, 0x20, 0x20, 0x40, },
3378c1763937SThierry Reding 		{ 0x30, 0x30, 0x40, },
3379c1763937SThierry Reding 		{ 0x40, 0x40, },
3380c1763937SThierry Reding 		{ 0x60, },
3381c1763937SThierry Reding 	},
3382c1763937SThierry Reding };
3383c1763937SThierry Reding 
3384459cc2c6SThierry Reding static const struct tegra_sor_soc tegra124_sor = {
3385459cc2c6SThierry Reding 	.supports_lvds = true,
3386459cc2c6SThierry Reding 	.supports_hdmi = false,
3387d278e4a9SThierry Reding 	.supports_dp = true,
3388d278e4a9SThierry Reding 	.supports_audio = false,
3389d278e4a9SThierry Reding 	.supports_hdcp = false,
3390880cee0bSThierry Reding 	.regs = &tegra124_sor_regs,
3391c57997bcSThierry Reding 	.has_nvdisplay = false,
339230b49435SThierry Reding 	.xbar_cfg = tegra124_sor_xbar_cfg,
3393c1763937SThierry Reding 	.lane_map = tegra124_sor_lane_map,
3394c1763937SThierry Reding 	.voltage_swing = tegra124_sor_voltage_swing,
3395c1763937SThierry Reding 	.pre_emphasis = tegra124_sor_pre_emphasis,
3396c1763937SThierry Reding 	.post_cursor = tegra124_sor_post_cursor,
3397c1763937SThierry Reding 	.tx_pu = tegra124_sor_tx_pu,
3398c1763937SThierry Reding };
3399c1763937SThierry Reding 
3400c1763937SThierry Reding static const u8 tegra132_sor_pre_emphasis[4][4][4] = {
3401c1763937SThierry Reding 	{
3402c1763937SThierry Reding 		{ 0x00, 0x08, 0x12, 0x24 },
3403c1763937SThierry Reding 		{ 0x01, 0x0e, 0x1d, },
3404c1763937SThierry Reding 		{ 0x01, 0x13, },
3405c1763937SThierry Reding 		{ 0x00, },
3406c1763937SThierry Reding 	}, {
3407c1763937SThierry Reding 		{ 0x00, 0x08, 0x12, 0x24 },
3408c1763937SThierry Reding 		{ 0x00, 0x0e, 0x1d, },
3409c1763937SThierry Reding 		{ 0x00, 0x13, },
3410c1763937SThierry Reding 		{ 0x00 },
3411c1763937SThierry Reding 	}, {
3412c1763937SThierry Reding 		{ 0x00, 0x08, 0x12, 0x24 },
3413c1763937SThierry Reding 		{ 0x00, 0x0e, 0x1d, },
3414c1763937SThierry Reding 		{ 0x00, 0x13, },
3415c1763937SThierry Reding 		{ 0x00, },
3416c1763937SThierry Reding 	}, {
3417c1763937SThierry Reding 		{ 0x00, 0x08, 0x12, 0x24 },
3418c1763937SThierry Reding 		{ 0x00, 0x0e, 0x1d, },
3419c1763937SThierry Reding 		{ 0x00, 0x13, },
3420c1763937SThierry Reding 		{ 0x00, },
3421c1763937SThierry Reding 	},
3422c1763937SThierry Reding };
3423c1763937SThierry Reding 
3424c1763937SThierry Reding static const struct tegra_sor_soc tegra132_sor = {
3425c1763937SThierry Reding 	.supports_lvds = true,
3426c1763937SThierry Reding 	.supports_hdmi = false,
3427d278e4a9SThierry Reding 	.supports_dp = true,
3428d278e4a9SThierry Reding 	.supports_audio = false,
3429d278e4a9SThierry Reding 	.supports_hdcp = false,
3430c1763937SThierry Reding 	.regs = &tegra124_sor_regs,
3431c1763937SThierry Reding 	.has_nvdisplay = false,
3432c1763937SThierry Reding 	.xbar_cfg = tegra124_sor_xbar_cfg,
3433c1763937SThierry Reding 	.lane_map = tegra124_sor_lane_map,
3434c1763937SThierry Reding 	.voltage_swing = tegra124_sor_voltage_swing,
3435c1763937SThierry Reding 	.pre_emphasis = tegra132_sor_pre_emphasis,
3436c1763937SThierry Reding 	.post_cursor = tegra124_sor_post_cursor,
3437c1763937SThierry Reding 	.tx_pu = tegra124_sor_tx_pu,
3438459cc2c6SThierry Reding };
3439459cc2c6SThierry Reding 
3440880cee0bSThierry Reding static const struct tegra_sor_regs tegra210_sor_regs = {
3441880cee0bSThierry Reding 	.head_state0 = 0x05,
3442880cee0bSThierry Reding 	.head_state1 = 0x07,
3443880cee0bSThierry Reding 	.head_state2 = 0x09,
3444880cee0bSThierry Reding 	.head_state3 = 0x0b,
3445880cee0bSThierry Reding 	.head_state4 = 0x0d,
3446880cee0bSThierry Reding 	.head_state5 = 0x0f,
3447880cee0bSThierry Reding 	.pll0 = 0x17,
3448880cee0bSThierry Reding 	.pll1 = 0x18,
3449880cee0bSThierry Reding 	.pll2 = 0x19,
3450880cee0bSThierry Reding 	.pll3 = 0x1a,
3451880cee0bSThierry Reding 	.dp_padctl0 = 0x5c,
3452880cee0bSThierry Reding 	.dp_padctl2 = 0x73,
3453880cee0bSThierry Reding };
3454880cee0bSThierry Reding 
3455c1763937SThierry Reding static const u8 tegra210_sor_xbar_cfg[5] = {
3456c1763937SThierry Reding 	2, 1, 0, 3, 4
3457c1763937SThierry Reding };
3458c1763937SThierry Reding 
34590472c21bSThierry Reding static const u8 tegra210_sor_lane_map[4] = {
34600472c21bSThierry Reding 	0, 1, 2, 3,
34610472c21bSThierry Reding };
34620472c21bSThierry Reding 
3463459cc2c6SThierry Reding static const struct tegra_sor_soc tegra210_sor = {
3464459cc2c6SThierry Reding 	.supports_lvds = false,
3465459cc2c6SThierry Reding 	.supports_hdmi = false,
3466d278e4a9SThierry Reding 	.supports_dp = true,
3467d278e4a9SThierry Reding 	.supports_audio = false,
3468d278e4a9SThierry Reding 	.supports_hdcp = false,
3469c1763937SThierry Reding 
3470880cee0bSThierry Reding 	.regs = &tegra210_sor_regs,
3471c57997bcSThierry Reding 	.has_nvdisplay = false,
347230b49435SThierry Reding 
3473c1763937SThierry Reding 	.xbar_cfg = tegra210_sor_xbar_cfg,
34740472c21bSThierry Reding 	.lane_map = tegra210_sor_lane_map,
34750472c21bSThierry Reding 	.voltage_swing = tegra124_sor_voltage_swing,
34760472c21bSThierry Reding 	.pre_emphasis = tegra124_sor_pre_emphasis,
34770472c21bSThierry Reding 	.post_cursor = tegra124_sor_post_cursor,
34780472c21bSThierry Reding 	.tx_pu = tegra124_sor_tx_pu,
3479459cc2c6SThierry Reding };
3480459cc2c6SThierry Reding 
3481459cc2c6SThierry Reding static const struct tegra_sor_soc tegra210_sor1 = {
3482459cc2c6SThierry Reding 	.supports_lvds = false,
3483459cc2c6SThierry Reding 	.supports_hdmi = true,
3484459cc2c6SThierry Reding 	.supports_dp = true,
3485d278e4a9SThierry Reding 	.supports_audio = true,
3486d278e4a9SThierry Reding 	.supports_hdcp = true,
3487459cc2c6SThierry Reding 
3488880cee0bSThierry Reding 	.regs = &tegra210_sor_regs,
3489c57997bcSThierry Reding 	.has_nvdisplay = false,
3490880cee0bSThierry Reding 
3491459cc2c6SThierry Reding 	.num_settings = ARRAY_SIZE(tegra210_sor_hdmi_defaults),
3492459cc2c6SThierry Reding 	.settings = tegra210_sor_hdmi_defaults,
349330b49435SThierry Reding 	.xbar_cfg = tegra210_sor_xbar_cfg,
34940472c21bSThierry Reding 	.lane_map = tegra210_sor_lane_map,
34950472c21bSThierry Reding 	.voltage_swing = tegra124_sor_voltage_swing,
34960472c21bSThierry Reding 	.pre_emphasis = tegra124_sor_pre_emphasis,
34970472c21bSThierry Reding 	.post_cursor = tegra124_sor_post_cursor,
34980472c21bSThierry Reding 	.tx_pu = tegra124_sor_tx_pu,
3499459cc2c6SThierry Reding };
3500459cc2c6SThierry Reding 
3501c57997bcSThierry Reding static const struct tegra_sor_regs tegra186_sor_regs = {
3502c57997bcSThierry Reding 	.head_state0 = 0x151,
3503c57997bcSThierry Reding 	.head_state1 = 0x154,
3504c57997bcSThierry Reding 	.head_state2 = 0x157,
3505c57997bcSThierry Reding 	.head_state3 = 0x15a,
3506c57997bcSThierry Reding 	.head_state4 = 0x15d,
3507c57997bcSThierry Reding 	.head_state5 = 0x160,
3508c57997bcSThierry Reding 	.pll0 = 0x163,
3509c57997bcSThierry Reding 	.pll1 = 0x164,
3510c57997bcSThierry Reding 	.pll2 = 0x165,
3511c57997bcSThierry Reding 	.pll3 = 0x166,
3512c57997bcSThierry Reding 	.dp_padctl0 = 0x168,
3513c57997bcSThierry Reding 	.dp_padctl2 = 0x16a,
3514c57997bcSThierry Reding };
3515c57997bcSThierry Reding 
35160472c21bSThierry Reding static const u8 tegra186_sor_voltage_swing[4][4][4] = {
35170472c21bSThierry Reding 	{
35180472c21bSThierry Reding 		{ 0x13, 0x19, 0x1e, 0x28 },
35190472c21bSThierry Reding 		{ 0x1e, 0x25, 0x2d, },
35200472c21bSThierry Reding 		{ 0x28, 0x32, },
35210472c21bSThierry Reding 		{ 0x39, },
35220472c21bSThierry Reding 	}, {
35230472c21bSThierry Reding 		{ 0x12, 0x16, 0x1b, 0x25 },
35240472c21bSThierry Reding 		{ 0x1c, 0x23, 0x2a, },
35250472c21bSThierry Reding 		{ 0x25, 0x2f, },
35260472c21bSThierry Reding 		{ 0x37, }
35270472c21bSThierry Reding 	}, {
35280472c21bSThierry Reding 		{ 0x12, 0x16, 0x1a, 0x22 },
35290472c21bSThierry Reding 		{ 0x1b, 0x20, 0x27, },
35300472c21bSThierry Reding 		{ 0x24, 0x2d, },
35310472c21bSThierry Reding 		{ 0x35, },
35320472c21bSThierry Reding 	}, {
35330472c21bSThierry Reding 		{ 0x11, 0x14, 0x17, 0x1f },
35340472c21bSThierry Reding 		{ 0x19, 0x1e, 0x24, },
35350472c21bSThierry Reding 		{ 0x22, 0x2a, },
35360472c21bSThierry Reding 		{ 0x32, },
35370472c21bSThierry Reding 	},
35380472c21bSThierry Reding };
35390472c21bSThierry Reding 
35400472c21bSThierry Reding static const u8 tegra186_sor_pre_emphasis[4][4][4] = {
35410472c21bSThierry Reding 	{
35420472c21bSThierry Reding 		{ 0x00, 0x08, 0x12, 0x24 },
35430472c21bSThierry Reding 		{ 0x01, 0x0e, 0x1d, },
35440472c21bSThierry Reding 		{ 0x01, 0x13, },
35450472c21bSThierry Reding 		{ 0x00, },
35460472c21bSThierry Reding 	}, {
35470472c21bSThierry Reding 		{ 0x00, 0x08, 0x12, 0x24 },
35480472c21bSThierry Reding 		{ 0x00, 0x0e, 0x1d, },
35490472c21bSThierry Reding 		{ 0x00, 0x13, },
35500472c21bSThierry Reding 		{ 0x00 },
35510472c21bSThierry Reding 	}, {
35520472c21bSThierry Reding 		{ 0x00, 0x08, 0x14, 0x24 },
35530472c21bSThierry Reding 		{ 0x00, 0x0e, 0x1d, },
35540472c21bSThierry Reding 		{ 0x00, 0x13, },
35550472c21bSThierry Reding 		{ 0x00, },
35560472c21bSThierry Reding 	}, {
35570472c21bSThierry Reding 		{ 0x00, 0x08, 0x12, 0x24 },
35580472c21bSThierry Reding 		{ 0x00, 0x0e, 0x1d, },
35590472c21bSThierry Reding 		{ 0x00, 0x13, },
35600472c21bSThierry Reding 		{ 0x00, },
35610472c21bSThierry Reding 	},
35620472c21bSThierry Reding };
35630472c21bSThierry Reding 
3564c57997bcSThierry Reding static const struct tegra_sor_soc tegra186_sor = {
3565c57997bcSThierry Reding 	.supports_lvds = false,
3566c57997bcSThierry Reding 	.supports_hdmi = true,
3567c57997bcSThierry Reding 	.supports_dp = true,
3568d278e4a9SThierry Reding 	.supports_audio = true,
3569d278e4a9SThierry Reding 	.supports_hdcp = true,
3570c57997bcSThierry Reding 
3571c57997bcSThierry Reding 	.regs = &tegra186_sor_regs,
3572c57997bcSThierry Reding 	.has_nvdisplay = true,
3573c57997bcSThierry Reding 
3574c57997bcSThierry Reding 	.num_settings = ARRAY_SIZE(tegra186_sor_hdmi_defaults),
3575c57997bcSThierry Reding 	.settings = tegra186_sor_hdmi_defaults,
3576c57997bcSThierry Reding 	.xbar_cfg = tegra124_sor_xbar_cfg,
35770472c21bSThierry Reding 	.lane_map = tegra124_sor_lane_map,
35780472c21bSThierry Reding 	.voltage_swing = tegra186_sor_voltage_swing,
35790472c21bSThierry Reding 	.pre_emphasis = tegra186_sor_pre_emphasis,
35800472c21bSThierry Reding 	.post_cursor = tegra124_sor_post_cursor,
35810472c21bSThierry Reding 	.tx_pu = tegra124_sor_tx_pu,
3582c57997bcSThierry Reding };
3583c57997bcSThierry Reding 
35849b6c14b8SThierry Reding static const struct tegra_sor_regs tegra194_sor_regs = {
35859b6c14b8SThierry Reding 	.head_state0 = 0x151,
35869b6c14b8SThierry Reding 	.head_state1 = 0x155,
35879b6c14b8SThierry Reding 	.head_state2 = 0x159,
35889b6c14b8SThierry Reding 	.head_state3 = 0x15d,
35899b6c14b8SThierry Reding 	.head_state4 = 0x161,
35909b6c14b8SThierry Reding 	.head_state5 = 0x165,
35919b6c14b8SThierry Reding 	.pll0 = 0x169,
35929b6c14b8SThierry Reding 	.pll1 = 0x16a,
35939b6c14b8SThierry Reding 	.pll2 = 0x16b,
35949b6c14b8SThierry Reding 	.pll3 = 0x16c,
35959b6c14b8SThierry Reding 	.dp_padctl0 = 0x16e,
35969b6c14b8SThierry Reding 	.dp_padctl2 = 0x16f,
35979b6c14b8SThierry Reding };
35989b6c14b8SThierry Reding 
35999b6c14b8SThierry Reding static const struct tegra_sor_soc tegra194_sor = {
36009b6c14b8SThierry Reding 	.supports_lvds = false,
36019b6c14b8SThierry Reding 	.supports_hdmi = true,
36029b6c14b8SThierry Reding 	.supports_dp = true,
3603d278e4a9SThierry Reding 	.supports_audio = true,
3604d278e4a9SThierry Reding 	.supports_hdcp = true,
36059b6c14b8SThierry Reding 
36069b6c14b8SThierry Reding 	.regs = &tegra194_sor_regs,
36079b6c14b8SThierry Reding 	.has_nvdisplay = true,
36089b6c14b8SThierry Reding 
36099b6c14b8SThierry Reding 	.num_settings = ARRAY_SIZE(tegra194_sor_hdmi_defaults),
36109b6c14b8SThierry Reding 	.settings = tegra194_sor_hdmi_defaults,
36119b6c14b8SThierry Reding 
36129b6c14b8SThierry Reding 	.xbar_cfg = tegra210_sor_xbar_cfg,
3613bae88815SThierry Reding 	.lane_map = tegra124_sor_lane_map,
3614bae88815SThierry Reding 	.voltage_swing = tegra186_sor_voltage_swing,
3615bae88815SThierry Reding 	.pre_emphasis = tegra186_sor_pre_emphasis,
3616bae88815SThierry Reding 	.post_cursor = tegra124_sor_post_cursor,
3617bae88815SThierry Reding 	.tx_pu = tegra124_sor_tx_pu,
36189b6c14b8SThierry Reding };
36199b6c14b8SThierry Reding 
3620459cc2c6SThierry Reding static const struct of_device_id tegra_sor_of_match[] = {
36219b6c14b8SThierry Reding 	{ .compatible = "nvidia,tegra194-sor", .data = &tegra194_sor },
3622c57997bcSThierry Reding 	{ .compatible = "nvidia,tegra186-sor", .data = &tegra186_sor },
3623459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra210-sor1", .data = &tegra210_sor1 },
3624459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra210-sor", .data = &tegra210_sor },
3625c1763937SThierry Reding 	{ .compatible = "nvidia,tegra132-sor", .data = &tegra132_sor },
3626459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra124-sor", .data = &tegra124_sor },
3627459cc2c6SThierry Reding 	{ },
3628459cc2c6SThierry Reding };
3629459cc2c6SThierry Reding MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
3630459cc2c6SThierry Reding 
3631c57997bcSThierry Reding static int tegra_sor_parse_dt(struct tegra_sor *sor)
3632c57997bcSThierry Reding {
3633c57997bcSThierry Reding 	struct device_node *np = sor->dev->of_node;
36346d6c815dSThierry Reding 	u32 xbar_cfg[5];
36356d6c815dSThierry Reding 	unsigned int i;
3636c57997bcSThierry Reding 	u32 value;
3637c57997bcSThierry Reding 	int err;
3638c57997bcSThierry Reding 
3639c57997bcSThierry Reding 	if (sor->soc->has_nvdisplay) {
3640c57997bcSThierry Reding 		err = of_property_read_u32(np, "nvidia,interface", &value);
3641c57997bcSThierry Reding 		if (err < 0)
3642c57997bcSThierry Reding 			return err;
3643c57997bcSThierry Reding 
3644c57997bcSThierry Reding 		sor->index = value;
3645c57997bcSThierry Reding 
3646c57997bcSThierry Reding 		/*
3647c57997bcSThierry Reding 		 * override the default that we already set for Tegra210 and
3648c57997bcSThierry Reding 		 * earlier
3649c57997bcSThierry Reding 		 */
3650c57997bcSThierry Reding 		sor->pad = TEGRA_IO_PAD_HDMI_DP0 + sor->index;
365124e64f86SThierry Reding 	} else {
3652d278e4a9SThierry Reding 		if (!sor->soc->supports_audio)
365324e64f86SThierry Reding 			sor->index = 0;
365424e64f86SThierry Reding 		else
365524e64f86SThierry Reding 			sor->index = 1;
3656c57997bcSThierry Reding 	}
3657c57997bcSThierry Reding 
36586d6c815dSThierry Reding 	err = of_property_read_u32_array(np, "nvidia,xbar-cfg", xbar_cfg, 5);
36596d6c815dSThierry Reding 	if (err < 0) {
36606d6c815dSThierry Reding 		/* fall back to default per-SoC XBAR configuration */
36616d6c815dSThierry Reding 		for (i = 0; i < 5; i++)
36626d6c815dSThierry Reding 			sor->xbar_cfg[i] = sor->soc->xbar_cfg[i];
36636d6c815dSThierry Reding 	} else {
36646d6c815dSThierry Reding 		/* copy cells to SOR XBAR configuration */
36656d6c815dSThierry Reding 		for (i = 0; i < 5; i++)
36666d6c815dSThierry Reding 			sor->xbar_cfg[i] = xbar_cfg[i];
3667c57997bcSThierry Reding 	}
3668c57997bcSThierry Reding 
36696b6b6042SThierry Reding 	return 0;
36708e2988a7SThierry Reding }
36718e2988a7SThierry Reding 
36728e2988a7SThierry Reding static irqreturn_t tegra_sor_irq(int irq, void *data)
36738e2988a7SThierry Reding {
36748e2988a7SThierry Reding 	struct tegra_sor *sor = data;
36758e2988a7SThierry Reding 	u32 value;
36768e2988a7SThierry Reding 
36778e2988a7SThierry Reding 	value = tegra_sor_readl(sor, SOR_INT_STATUS);
36788e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_INT_STATUS);
36798e2988a7SThierry Reding 
36808e2988a7SThierry Reding 	if (value & SOR_INT_CODEC_SCRATCH0) {
36818e2988a7SThierry Reding 		value = tegra_sor_readl(sor, SOR_AUDIO_HDA_CODEC_SCRATCH0);
36828e2988a7SThierry Reding 
36838e2988a7SThierry Reding 		if (value & SOR_AUDIO_HDA_CODEC_SCRATCH0_VALID) {
3684cd54fb96SThierry Reding 			unsigned int format;
36858e2988a7SThierry Reding 
36868e2988a7SThierry Reding 			format = value & SOR_AUDIO_HDA_CODEC_SCRATCH0_FMT_MASK;
36878e2988a7SThierry Reding 
3688fad7b806SThierry Reding 			tegra_hda_parse_format(format, &sor->format);
36898e2988a7SThierry Reding 
369085d0c4b5SThierry Reding 			if (sor->ops->audio_enable)
369185d0c4b5SThierry Reding 				sor->ops->audio_enable(sor);
36928e2988a7SThierry Reding 		} else {
369385d0c4b5SThierry Reding 			if (sor->ops->audio_disable)
369485d0c4b5SThierry Reding 				sor->ops->audio_disable(sor);
36958e2988a7SThierry Reding 		}
36968e2988a7SThierry Reding 	}
36978e2988a7SThierry Reding 
36988e2988a7SThierry Reding 	return IRQ_HANDLED;
36998e2988a7SThierry Reding }
37008e2988a7SThierry Reding 
37016b6b6042SThierry Reding static int tegra_sor_probe(struct platform_device *pdev)
37026b6b6042SThierry Reding {
37036b6b6042SThierry Reding 	struct device_node *np;
37046b6b6042SThierry Reding 	struct tegra_sor *sor;
37056b6b6042SThierry Reding 	struct resource *regs;
37066b6b6042SThierry Reding 	int err;
37076b6b6042SThierry Reding 
37086b6b6042SThierry Reding 	sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
37096b6b6042SThierry Reding 	if (!sor)
37106b6b6042SThierry Reding 		return -ENOMEM;
37116b6b6042SThierry Reding 
37125faea3d0SThierry Reding 	sor->soc = of_device_get_match_data(&pdev->dev);
37136b6b6042SThierry Reding 	sor->output.dev = sor->dev = &pdev->dev;
3714459cc2c6SThierry Reding 
3715459cc2c6SThierry Reding 	sor->settings = devm_kmemdup(&pdev->dev, sor->soc->settings,
3716459cc2c6SThierry Reding 				     sor->soc->num_settings *
3717459cc2c6SThierry Reding 					sizeof(*sor->settings),
3718459cc2c6SThierry Reding 				     GFP_KERNEL);
3719459cc2c6SThierry Reding 	if (!sor->settings)
3720459cc2c6SThierry Reding 		return -ENOMEM;
3721459cc2c6SThierry Reding 
3722459cc2c6SThierry Reding 	sor->num_settings = sor->soc->num_settings;
37236b6b6042SThierry Reding 
37246b6b6042SThierry Reding 	np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
37256b6b6042SThierry Reding 	if (np) {
37269542c237SThierry Reding 		sor->aux = drm_dp_aux_find_by_of_node(np);
37276b6b6042SThierry Reding 		of_node_put(np);
37286b6b6042SThierry Reding 
37299542c237SThierry Reding 		if (!sor->aux)
37306b6b6042SThierry Reding 			return -EPROBE_DEFER;
37316f684de5SThierry Reding 
37326f684de5SThierry Reding 		sor->output.ddc = &sor->aux->ddc;
37336b6b6042SThierry Reding 	}
37346b6b6042SThierry Reding 
37359542c237SThierry Reding 	if (!sor->aux) {
3736459cc2c6SThierry Reding 		if (sor->soc->supports_hdmi) {
3737459cc2c6SThierry Reding 			sor->ops = &tegra_sor_hdmi_ops;
3738c57997bcSThierry Reding 			sor->pad = TEGRA_IO_PAD_HDMI;
3739459cc2c6SThierry Reding 		} else if (sor->soc->supports_lvds) {
3740459cc2c6SThierry Reding 			dev_err(&pdev->dev, "LVDS not supported yet\n");
3741459cc2c6SThierry Reding 			return -ENODEV;
3742459cc2c6SThierry Reding 		} else {
3743459cc2c6SThierry Reding 			dev_err(&pdev->dev, "unknown (non-DP) support\n");
3744459cc2c6SThierry Reding 			return -ENODEV;
3745459cc2c6SThierry Reding 		}
3746459cc2c6SThierry Reding 	} else {
3747d278e4a9SThierry Reding 		np = of_parse_phandle(pdev->dev.of_node, "nvidia,panel", 0);
3748d278e4a9SThierry Reding 		/*
3749d278e4a9SThierry Reding 		 * No need to keep this around since we only use it as a check
3750d278e4a9SThierry Reding 		 * to see if a panel is connected (eDP) or not (DP).
3751d278e4a9SThierry Reding 		 */
3752d278e4a9SThierry Reding 		of_node_put(np);
3753d278e4a9SThierry Reding 
37540472c21bSThierry Reding 		sor->ops = &tegra_sor_dp_ops;
3755d23691f6SThierry Reding 		sor->pad = TEGRA_IO_PAD_LVDS;
3756459cc2c6SThierry Reding 	}
3757459cc2c6SThierry Reding 
3758c57997bcSThierry Reding 	err = tegra_sor_parse_dt(sor);
3759c57997bcSThierry Reding 	if (err < 0)
3760c57997bcSThierry Reding 		return err;
3761c57997bcSThierry Reding 
37626b6b6042SThierry Reding 	err = tegra_output_probe(&sor->output);
37634dbdc740SThierry Reding 	if (err < 0) {
37644dbdc740SThierry Reding 		dev_err(&pdev->dev, "failed to probe output: %d\n", err);
37656b6b6042SThierry Reding 		return err;
37664dbdc740SThierry Reding 	}
37676b6b6042SThierry Reding 
3768459cc2c6SThierry Reding 	if (sor->ops && sor->ops->probe) {
3769459cc2c6SThierry Reding 		err = sor->ops->probe(sor);
3770459cc2c6SThierry Reding 		if (err < 0) {
3771459cc2c6SThierry Reding 			dev_err(&pdev->dev, "failed to probe %s: %d\n",
3772459cc2c6SThierry Reding 				sor->ops->name, err);
3773459cc2c6SThierry Reding 			goto output;
3774459cc2c6SThierry Reding 		}
3775459cc2c6SThierry Reding 	}
3776459cc2c6SThierry Reding 
37776b6b6042SThierry Reding 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
37786b6b6042SThierry Reding 	sor->regs = devm_ioremap_resource(&pdev->dev, regs);
3779459cc2c6SThierry Reding 	if (IS_ERR(sor->regs)) {
3780459cc2c6SThierry Reding 		err = PTR_ERR(sor->regs);
3781459cc2c6SThierry Reding 		goto remove;
3782459cc2c6SThierry Reding 	}
37836b6b6042SThierry Reding 
37848e2988a7SThierry Reding 	err = platform_get_irq(pdev, 0);
37858e2988a7SThierry Reding 	if (err < 0) {
37868e2988a7SThierry Reding 		dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
37878e2988a7SThierry Reding 		goto remove;
37888e2988a7SThierry Reding 	}
37898e2988a7SThierry Reding 
37908e2988a7SThierry Reding 	sor->irq = err;
37918e2988a7SThierry Reding 
37928e2988a7SThierry Reding 	err = devm_request_irq(sor->dev, sor->irq, tegra_sor_irq, 0,
37938e2988a7SThierry Reding 			       dev_name(sor->dev), sor);
37948e2988a7SThierry Reding 	if (err < 0) {
37958e2988a7SThierry Reding 		dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
37968e2988a7SThierry Reding 		goto remove;
37978e2988a7SThierry Reding 	}
37988e2988a7SThierry Reding 
379911c632e1SThierry Reding 	sor->rst = devm_reset_control_get_exclusive_released(&pdev->dev, "sor");
38004dbdc740SThierry Reding 	if (IS_ERR(sor->rst)) {
3801459cc2c6SThierry Reding 		err = PTR_ERR(sor->rst);
3802180b46ecSThierry Reding 
3803180b46ecSThierry Reding 		if (err != -EBUSY || WARN_ON(!pdev->dev.pm_domain)) {
3804f8c79120SJon Hunter 			dev_err(&pdev->dev, "failed to get reset control: %d\n",
3805f8c79120SJon Hunter 				err);
3806459cc2c6SThierry Reding 			goto remove;
38074dbdc740SThierry Reding 		}
3808180b46ecSThierry Reding 
3809180b46ecSThierry Reding 		/*
3810180b46ecSThierry Reding 		 * At this point, the reset control is most likely being used
3811180b46ecSThierry Reding 		 * by the generic power domain implementation. With any luck
3812180b46ecSThierry Reding 		 * the power domain will have taken care of resetting the SOR
3813180b46ecSThierry Reding 		 * and we don't have to do anything.
3814180b46ecSThierry Reding 		 */
3815180b46ecSThierry Reding 		sor->rst = NULL;
3816f8c79120SJon Hunter 	}
38176b6b6042SThierry Reding 
38186b6b6042SThierry Reding 	sor->clk = devm_clk_get(&pdev->dev, NULL);
38194dbdc740SThierry Reding 	if (IS_ERR(sor->clk)) {
3820459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk);
3821459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get module clock: %d\n", err);
3822459cc2c6SThierry Reding 		goto remove;
38234dbdc740SThierry Reding 	}
38246b6b6042SThierry Reding 
3825618dee39SThierry Reding 	if (sor->soc->supports_hdmi || sor->soc->supports_dp) {
3826e1335e2fSThierry Reding 		struct device_node *np = pdev->dev.of_node;
3827e1335e2fSThierry Reding 		const char *name;
3828e1335e2fSThierry Reding 
3829e1335e2fSThierry Reding 		/*
3830e1335e2fSThierry Reding 		 * For backwards compatibility with Tegra210 device trees,
3831e1335e2fSThierry Reding 		 * fall back to the old clock name "source" if the new "out"
3832e1335e2fSThierry Reding 		 * clock is not available.
3833e1335e2fSThierry Reding 		 */
3834e1335e2fSThierry Reding 		if (of_property_match_string(np, "clock-names", "out") < 0)
3835e1335e2fSThierry Reding 			name = "source";
3836e1335e2fSThierry Reding 		else
3837e1335e2fSThierry Reding 			name = "out";
3838e1335e2fSThierry Reding 
3839e1335e2fSThierry Reding 		sor->clk_out = devm_clk_get(&pdev->dev, name);
3840e1335e2fSThierry Reding 		if (IS_ERR(sor->clk_out)) {
3841e1335e2fSThierry Reding 			err = PTR_ERR(sor->clk_out);
3842e1335e2fSThierry Reding 			dev_err(sor->dev, "failed to get %s clock: %d\n",
3843e1335e2fSThierry Reding 				name, err);
3844618dee39SThierry Reding 			goto remove;
3845618dee39SThierry Reding 		}
38461087fac1SThierry Reding 	} else {
3847d780537fSThierry Reding 		/* fall back to the module clock on SOR0 (eDP/LVDS only) */
38481087fac1SThierry Reding 		sor->clk_out = sor->clk;
3849618dee39SThierry Reding 	}
3850618dee39SThierry Reding 
38516b6b6042SThierry Reding 	sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
38524dbdc740SThierry Reding 	if (IS_ERR(sor->clk_parent)) {
3853459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_parent);
3854459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get parent clock: %d\n", err);
3855459cc2c6SThierry Reding 		goto remove;
38564dbdc740SThierry Reding 	}
38576b6b6042SThierry Reding 
38586b6b6042SThierry Reding 	sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
38594dbdc740SThierry Reding 	if (IS_ERR(sor->clk_safe)) {
3860459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_safe);
3861459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get safe clock: %d\n", err);
3862459cc2c6SThierry Reding 		goto remove;
38634dbdc740SThierry Reding 	}
38646b6b6042SThierry Reding 
38656b6b6042SThierry Reding 	sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
38664dbdc740SThierry Reding 	if (IS_ERR(sor->clk_dp)) {
3867459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_dp);
3868459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get DP clock: %d\n", err);
3869459cc2c6SThierry Reding 		goto remove;
38704dbdc740SThierry Reding 	}
38716b6b6042SThierry Reding 
3872e1335e2fSThierry Reding 	/*
3873e1335e2fSThierry Reding 	 * Starting with Tegra186, the BPMP provides an implementation for
3874e1335e2fSThierry Reding 	 * the pad output clock, so we have to look it up from device tree.
3875e1335e2fSThierry Reding 	 */
3876e1335e2fSThierry Reding 	sor->clk_pad = devm_clk_get(&pdev->dev, "pad");
3877e1335e2fSThierry Reding 	if (IS_ERR(sor->clk_pad)) {
3878e1335e2fSThierry Reding 		if (sor->clk_pad != ERR_PTR(-ENOENT)) {
3879e1335e2fSThierry Reding 			err = PTR_ERR(sor->clk_pad);
3880e1335e2fSThierry Reding 			goto remove;
3881e1335e2fSThierry Reding 		}
3882e1335e2fSThierry Reding 
3883e1335e2fSThierry Reding 		/*
3884e1335e2fSThierry Reding 		 * If the pad output clock is not available, then we assume
3885e1335e2fSThierry Reding 		 * we're on Tegra210 or earlier and have to provide our own
3886e1335e2fSThierry Reding 		 * implementation.
3887e1335e2fSThierry Reding 		 */
3888e1335e2fSThierry Reding 		sor->clk_pad = NULL;
3889e1335e2fSThierry Reding 	}
3890e1335e2fSThierry Reding 
3891e1335e2fSThierry Reding 	/*
3892e1335e2fSThierry Reding 	 * The bootloader may have set up the SOR such that it's module clock
3893e1335e2fSThierry Reding 	 * is sourced by one of the display PLLs. However, that doesn't work
3894e1335e2fSThierry Reding 	 * without properly having set up other bits of the SOR.
3895e1335e2fSThierry Reding 	 */
3896e1335e2fSThierry Reding 	err = clk_set_parent(sor->clk_out, sor->clk_safe);
3897e1335e2fSThierry Reding 	if (err < 0) {
3898e1335e2fSThierry Reding 		dev_err(&pdev->dev, "failed to use safe clock: %d\n", err);
3899e1335e2fSThierry Reding 		goto remove;
3900e1335e2fSThierry Reding 	}
3901e1335e2fSThierry Reding 
3902aaff8bd2SThierry Reding 	platform_set_drvdata(pdev, sor);
3903aaff8bd2SThierry Reding 	pm_runtime_enable(&pdev->dev);
3904aaff8bd2SThierry Reding 
39056b6b6042SThierry Reding 	INIT_LIST_HEAD(&sor->client.list);
39066b6b6042SThierry Reding 	sor->client.ops = &sor_client_ops;
39076b6b6042SThierry Reding 	sor->client.dev = &pdev->dev;
39086b6b6042SThierry Reding 
39096b6b6042SThierry Reding 	err = host1x_client_register(&sor->client);
39106b6b6042SThierry Reding 	if (err < 0) {
39116b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
39126b6b6042SThierry Reding 			err);
3913ad2139cbSThierry Reding 		goto rpm_disable;
39146b6b6042SThierry Reding 	}
39156b6b6042SThierry Reding 
3916c472a0b0SThierry Reding 	/*
3917c472a0b0SThierry Reding 	 * On Tegra210 and earlier, provide our own implementation for the
3918c472a0b0SThierry Reding 	 * pad output clock.
3919c472a0b0SThierry Reding 	 */
3920c472a0b0SThierry Reding 	if (!sor->clk_pad) {
3921c472a0b0SThierry Reding 		char *name;
3922c472a0b0SThierry Reding 
3923c472a0b0SThierry Reding 		name = devm_kasprintf(sor->dev, GFP_KERNEL, "sor%u_pad_clkout",
3924c472a0b0SThierry Reding 				      sor->index);
3925c472a0b0SThierry Reding 		if (!name) {
3926c472a0b0SThierry Reding 			err = -ENOMEM;
3927c472a0b0SThierry Reding 			goto unregister;
3928c472a0b0SThierry Reding 		}
3929c472a0b0SThierry Reding 
3930c472a0b0SThierry Reding 		err = host1x_client_resume(&sor->client);
3931c472a0b0SThierry Reding 		if (err < 0) {
3932c472a0b0SThierry Reding 			dev_err(sor->dev, "failed to resume: %d\n", err);
3933c472a0b0SThierry Reding 			goto unregister;
3934c472a0b0SThierry Reding 		}
3935c472a0b0SThierry Reding 
3936c472a0b0SThierry Reding 		sor->clk_pad = tegra_clk_sor_pad_register(sor, name);
3937c472a0b0SThierry Reding 		host1x_client_suspend(&sor->client);
3938c472a0b0SThierry Reding 	}
3939c472a0b0SThierry Reding 
3940c472a0b0SThierry Reding 	if (IS_ERR(sor->clk_pad)) {
3941c472a0b0SThierry Reding 		err = PTR_ERR(sor->clk_pad);
3942c472a0b0SThierry Reding 		dev_err(sor->dev, "failed to register SOR pad clock: %d\n",
3943c472a0b0SThierry Reding 			err);
3944c472a0b0SThierry Reding 		goto unregister;
39456b6b6042SThierry Reding 	}
39466b6b6042SThierry Reding 
39476b6b6042SThierry Reding 	return 0;
3948459cc2c6SThierry Reding 
3949c472a0b0SThierry Reding unregister:
3950c472a0b0SThierry Reding 	host1x_client_unregister(&sor->client);
3951ad2139cbSThierry Reding rpm_disable:
3952ad2139cbSThierry Reding 	pm_runtime_disable(&pdev->dev);
3953459cc2c6SThierry Reding remove:
3954459cc2c6SThierry Reding 	if (sor->ops && sor->ops->remove)
3955459cc2c6SThierry Reding 		sor->ops->remove(sor);
3956459cc2c6SThierry Reding output:
3957459cc2c6SThierry Reding 	tegra_output_remove(&sor->output);
3958459cc2c6SThierry Reding 	return err;
39596b6b6042SThierry Reding }
39606b6b6042SThierry Reding 
39616b6b6042SThierry Reding static int tegra_sor_remove(struct platform_device *pdev)
39626b6b6042SThierry Reding {
39636b6b6042SThierry Reding 	struct tegra_sor *sor = platform_get_drvdata(pdev);
39646b6b6042SThierry Reding 	int err;
39656b6b6042SThierry Reding 
39666b6b6042SThierry Reding 	err = host1x_client_unregister(&sor->client);
39676b6b6042SThierry Reding 	if (err < 0) {
39686b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
39696b6b6042SThierry Reding 			err);
39706b6b6042SThierry Reding 		return err;
39716b6b6042SThierry Reding 	}
39726b6b6042SThierry Reding 
3973ad2139cbSThierry Reding 	pm_runtime_disable(&pdev->dev);
3974ad2139cbSThierry Reding 
3975459cc2c6SThierry Reding 	if (sor->ops && sor->ops->remove) {
3976459cc2c6SThierry Reding 		err = sor->ops->remove(sor);
3977459cc2c6SThierry Reding 		if (err < 0)
3978459cc2c6SThierry Reding 			dev_err(&pdev->dev, "failed to remove SOR: %d\n", err);
3979459cc2c6SThierry Reding 	}
3980459cc2c6SThierry Reding 
3981328ec69eSThierry Reding 	tegra_output_remove(&sor->output);
39826b6b6042SThierry Reding 
39836b6b6042SThierry Reding 	return 0;
39846b6b6042SThierry Reding }
39856b6b6042SThierry Reding 
3986f90965abSArnd Bergmann static int __maybe_unused tegra_sor_suspend(struct device *dev)
3987aaff8bd2SThierry Reding {
3988aaff8bd2SThierry Reding 	struct tegra_sor *sor = dev_get_drvdata(dev);
3989aaff8bd2SThierry Reding 	int err;
3990aaff8bd2SThierry Reding 
3991271502efSThierry Reding 	err = tegra_output_suspend(&sor->output);
3992aaff8bd2SThierry Reding 	if (err < 0) {
3993271502efSThierry Reding 		dev_err(dev, "failed to suspend output: %d\n", err);
3994aaff8bd2SThierry Reding 		return err;
3995aaff8bd2SThierry Reding 	}
399611c632e1SThierry Reding 
3997b06e145fSThierry Reding 	if (sor->hdmi_supply) {
3998b06e145fSThierry Reding 		err = regulator_disable(sor->hdmi_supply);
3999fd67e9c6SThierry Reding 		if (err < 0) {
4000271502efSThierry Reding 			tegra_output_resume(&sor->output);
4001b06e145fSThierry Reding 			return err;
4002b06e145fSThierry Reding 		}
4003fd67e9c6SThierry Reding 	}
4004b06e145fSThierry Reding 
4005b06e145fSThierry Reding 	return 0;
4006be0b23f2SThierry Reding }
4007be0b23f2SThierry Reding 
4008f90965abSArnd Bergmann static int __maybe_unused tegra_sor_resume(struct device *dev)
4009be0b23f2SThierry Reding {
4010be0b23f2SThierry Reding 	struct tegra_sor *sor = dev_get_drvdata(dev);
4011b06e145fSThierry Reding 	int err;
4012be0b23f2SThierry Reding 
4013b06e145fSThierry Reding 	if (sor->hdmi_supply) {
4014b06e145fSThierry Reding 		err = regulator_enable(sor->hdmi_supply);
4015b06e145fSThierry Reding 		if (err < 0)
4016b06e145fSThierry Reding 			return err;
4017b06e145fSThierry Reding 	}
4018b06e145fSThierry Reding 
4019271502efSThierry Reding 	err = tegra_output_resume(&sor->output);
4020271502efSThierry Reding 	if (err < 0) {
4021271502efSThierry Reding 		dev_err(dev, "failed to resume output: %d\n", err);
4022271502efSThierry Reding 
4023271502efSThierry Reding 		if (sor->hdmi_supply)
4024271502efSThierry Reding 			regulator_disable(sor->hdmi_supply);
4025271502efSThierry Reding 
4026271502efSThierry Reding 		return err;
4027271502efSThierry Reding 	}
4028271502efSThierry Reding 
4029b06e145fSThierry Reding 	return 0;
4030be0b23f2SThierry Reding }
4031aaff8bd2SThierry Reding 
4032aaff8bd2SThierry Reding static const struct dev_pm_ops tegra_sor_pm_ops = {
4033be0b23f2SThierry Reding 	SET_SYSTEM_SLEEP_PM_OPS(tegra_sor_suspend, tegra_sor_resume)
4034aaff8bd2SThierry Reding };
4035aaff8bd2SThierry Reding 
40366b6b6042SThierry Reding struct platform_driver tegra_sor_driver = {
40376b6b6042SThierry Reding 	.driver = {
40386b6b6042SThierry Reding 		.name = "tegra-sor",
40396b6b6042SThierry Reding 		.of_match_table = tegra_sor_of_match,
4040aaff8bd2SThierry Reding 		.pm = &tegra_sor_pm_ops,
40416b6b6042SThierry Reding 	},
40426b6b6042SThierry Reding 	.probe = tegra_sor_probe,
40436b6b6042SThierry Reding 	.remove = tegra_sor_remove,
40446b6b6042SThierry Reding };
4045