xref: /openbmc/linux/drivers/gpu/drm/tegra/sor.c (revision 4bdf4710e00a502347e33dc2454821fa9af2968b)
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>
266b6b6042SThierry Reding 
276b6b6042SThierry Reding #include "dc.h"
289a42c7c6SThierry Reding #include "dp.h"
296b6b6042SThierry Reding #include "drm.h"
30fad7b806SThierry Reding #include "hda.h"
316b6b6042SThierry Reding #include "sor.h"
32932f6529SThierry Reding #include "trace.h"
336b6b6042SThierry Reding 
34459cc2c6SThierry Reding #define SOR_REKEY 0x38
35459cc2c6SThierry Reding 
36459cc2c6SThierry Reding struct tegra_sor_hdmi_settings {
37459cc2c6SThierry Reding 	unsigned long frequency;
38459cc2c6SThierry Reding 
39459cc2c6SThierry Reding 	u8 vcocap;
40c57997bcSThierry Reding 	u8 filter;
41459cc2c6SThierry Reding 	u8 ichpmp;
42459cc2c6SThierry Reding 	u8 loadadj;
43c57997bcSThierry Reding 	u8 tmds_termadj;
44c57997bcSThierry Reding 	u8 tx_pu_value;
45c57997bcSThierry Reding 	u8 bg_temp_coef;
46c57997bcSThierry Reding 	u8 bg_vref_level;
47c57997bcSThierry Reding 	u8 avdd10_level;
48c57997bcSThierry Reding 	u8 avdd14_level;
49c57997bcSThierry Reding 	u8 sparepll;
50459cc2c6SThierry Reding 
51459cc2c6SThierry Reding 	u8 drive_current[4];
52459cc2c6SThierry Reding 	u8 preemphasis[4];
53459cc2c6SThierry Reding };
54459cc2c6SThierry Reding 
55459cc2c6SThierry Reding #if 1
56459cc2c6SThierry Reding static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
57459cc2c6SThierry Reding 	{
58459cc2c6SThierry Reding 		.frequency = 54000000,
59459cc2c6SThierry Reding 		.vcocap = 0x0,
60c57997bcSThierry Reding 		.filter = 0x0,
61459cc2c6SThierry Reding 		.ichpmp = 0x1,
62459cc2c6SThierry Reding 		.loadadj = 0x3,
63c57997bcSThierry Reding 		.tmds_termadj = 0x9,
64c57997bcSThierry Reding 		.tx_pu_value = 0x10,
65c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
66c57997bcSThierry Reding 		.bg_vref_level = 0x8,
67c57997bcSThierry Reding 		.avdd10_level = 0x4,
68c57997bcSThierry Reding 		.avdd14_level = 0x4,
69c57997bcSThierry Reding 		.sparepll = 0x0,
70459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
71459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
72459cc2c6SThierry Reding 	}, {
73459cc2c6SThierry Reding 		.frequency = 75000000,
74459cc2c6SThierry Reding 		.vcocap = 0x3,
75c57997bcSThierry Reding 		.filter = 0x0,
76459cc2c6SThierry Reding 		.ichpmp = 0x1,
77459cc2c6SThierry Reding 		.loadadj = 0x3,
78c57997bcSThierry Reding 		.tmds_termadj = 0x9,
79c57997bcSThierry Reding 		.tx_pu_value = 0x40,
80c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
81c57997bcSThierry Reding 		.bg_vref_level = 0x8,
82c57997bcSThierry Reding 		.avdd10_level = 0x4,
83c57997bcSThierry Reding 		.avdd14_level = 0x4,
84c57997bcSThierry Reding 		.sparepll = 0x0,
85459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
86459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
87459cc2c6SThierry Reding 	}, {
88459cc2c6SThierry Reding 		.frequency = 150000000,
89459cc2c6SThierry Reding 		.vcocap = 0x3,
90c57997bcSThierry Reding 		.filter = 0x0,
91459cc2c6SThierry Reding 		.ichpmp = 0x1,
92459cc2c6SThierry Reding 		.loadadj = 0x3,
93c57997bcSThierry Reding 		.tmds_termadj = 0x9,
94c57997bcSThierry Reding 		.tx_pu_value = 0x66,
95c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
96c57997bcSThierry Reding 		.bg_vref_level = 0x8,
97c57997bcSThierry Reding 		.avdd10_level = 0x4,
98c57997bcSThierry Reding 		.avdd14_level = 0x4,
99c57997bcSThierry Reding 		.sparepll = 0x0,
100459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
101459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
102459cc2c6SThierry Reding 	}, {
103459cc2c6SThierry Reding 		.frequency = 300000000,
104459cc2c6SThierry Reding 		.vcocap = 0x3,
105c57997bcSThierry Reding 		.filter = 0x0,
106459cc2c6SThierry Reding 		.ichpmp = 0x1,
107459cc2c6SThierry Reding 		.loadadj = 0x3,
108c57997bcSThierry Reding 		.tmds_termadj = 0x9,
109c57997bcSThierry Reding 		.tx_pu_value = 0x66,
110c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
111c57997bcSThierry Reding 		.bg_vref_level = 0xa,
112c57997bcSThierry Reding 		.avdd10_level = 0x4,
113c57997bcSThierry Reding 		.avdd14_level = 0x4,
114c57997bcSThierry Reding 		.sparepll = 0x0,
115459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
116459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x17, 0x17, 0x17 },
117459cc2c6SThierry Reding 	}, {
118459cc2c6SThierry Reding 		.frequency = 600000000,
119459cc2c6SThierry Reding 		.vcocap = 0x3,
120c57997bcSThierry Reding 		.filter = 0x0,
121459cc2c6SThierry Reding 		.ichpmp = 0x1,
122459cc2c6SThierry Reding 		.loadadj = 0x3,
123c57997bcSThierry Reding 		.tmds_termadj = 0x9,
124c57997bcSThierry Reding 		.tx_pu_value = 0x66,
125c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
126c57997bcSThierry Reding 		.bg_vref_level = 0x8,
127c57997bcSThierry Reding 		.avdd10_level = 0x4,
128c57997bcSThierry Reding 		.avdd14_level = 0x4,
129c57997bcSThierry Reding 		.sparepll = 0x0,
130459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
131459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
132459cc2c6SThierry Reding 	},
133459cc2c6SThierry Reding };
134459cc2c6SThierry Reding #else
135459cc2c6SThierry Reding static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
136459cc2c6SThierry Reding 	{
137459cc2c6SThierry Reding 		.frequency = 75000000,
138459cc2c6SThierry Reding 		.vcocap = 0x3,
139c57997bcSThierry Reding 		.filter = 0x0,
140459cc2c6SThierry Reding 		.ichpmp = 0x1,
141459cc2c6SThierry Reding 		.loadadj = 0x3,
142c57997bcSThierry Reding 		.tmds_termadj = 0x9,
143c57997bcSThierry Reding 		.tx_pu_value = 0x40,
144c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
145c57997bcSThierry Reding 		.bg_vref_level = 0x8,
146c57997bcSThierry Reding 		.avdd10_level = 0x4,
147c57997bcSThierry Reding 		.avdd14_level = 0x4,
148c57997bcSThierry Reding 		.sparepll = 0x0,
149459cc2c6SThierry Reding 		.drive_current = { 0x29, 0x29, 0x29, 0x29 },
150459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
151459cc2c6SThierry Reding 	}, {
152459cc2c6SThierry Reding 		.frequency = 150000000,
153459cc2c6SThierry Reding 		.vcocap = 0x3,
154c57997bcSThierry Reding 		.filter = 0x0,
155459cc2c6SThierry Reding 		.ichpmp = 0x1,
156459cc2c6SThierry Reding 		.loadadj = 0x3,
157c57997bcSThierry Reding 		.tmds_termadj = 0x9,
158c57997bcSThierry Reding 		.tx_pu_value = 0x66,
159c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
160c57997bcSThierry Reding 		.bg_vref_level = 0x8,
161c57997bcSThierry Reding 		.avdd10_level = 0x4,
162c57997bcSThierry Reding 		.avdd14_level = 0x4,
163c57997bcSThierry Reding 		.sparepll = 0x0,
164459cc2c6SThierry Reding 		.drive_current = { 0x30, 0x37, 0x37, 0x37 },
165459cc2c6SThierry Reding 		.preemphasis = { 0x01, 0x02, 0x02, 0x02 },
166459cc2c6SThierry Reding 	}, {
167459cc2c6SThierry Reding 		.frequency = 300000000,
168459cc2c6SThierry Reding 		.vcocap = 0x3,
169c57997bcSThierry Reding 		.filter = 0x0,
170459cc2c6SThierry Reding 		.ichpmp = 0x6,
171459cc2c6SThierry Reding 		.loadadj = 0x3,
172c57997bcSThierry Reding 		.tmds_termadj = 0x9,
173c57997bcSThierry Reding 		.tx_pu_value = 0x66,
174c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
175c57997bcSThierry Reding 		.bg_vref_level = 0xf,
176c57997bcSThierry Reding 		.avdd10_level = 0x4,
177c57997bcSThierry Reding 		.avdd14_level = 0x4,
178c57997bcSThierry Reding 		.sparepll = 0x0,
179459cc2c6SThierry Reding 		.drive_current = { 0x30, 0x37, 0x37, 0x37 },
180459cc2c6SThierry Reding 		.preemphasis = { 0x10, 0x3e, 0x3e, 0x3e },
181459cc2c6SThierry Reding 	}, {
182459cc2c6SThierry Reding 		.frequency = 600000000,
183459cc2c6SThierry Reding 		.vcocap = 0x3,
184c57997bcSThierry Reding 		.filter = 0x0,
185459cc2c6SThierry Reding 		.ichpmp = 0xa,
186459cc2c6SThierry Reding 		.loadadj = 0x3,
187c57997bcSThierry Reding 		.tmds_termadj = 0xb,
188c57997bcSThierry Reding 		.tx_pu_value = 0x66,
189c57997bcSThierry Reding 		.bg_temp_coef = 0x3,
190c57997bcSThierry Reding 		.bg_vref_level = 0xe,
191c57997bcSThierry Reding 		.avdd10_level = 0x4,
192c57997bcSThierry Reding 		.avdd14_level = 0x4,
193c57997bcSThierry Reding 		.sparepll = 0x0,
194459cc2c6SThierry Reding 		.drive_current = { 0x35, 0x3e, 0x3e, 0x3e },
195459cc2c6SThierry Reding 		.preemphasis = { 0x02, 0x3f, 0x3f, 0x3f },
196459cc2c6SThierry Reding 	},
197459cc2c6SThierry Reding };
198459cc2c6SThierry Reding #endif
199459cc2c6SThierry Reding 
200c57997bcSThierry Reding static const struct tegra_sor_hdmi_settings tegra186_sor_hdmi_defaults[] = {
201c57997bcSThierry Reding 	{
202c57997bcSThierry Reding 		.frequency = 54000000,
203c57997bcSThierry Reding 		.vcocap = 0,
204c57997bcSThierry Reding 		.filter = 5,
205c57997bcSThierry Reding 		.ichpmp = 5,
206c57997bcSThierry Reding 		.loadadj = 3,
207c57997bcSThierry Reding 		.tmds_termadj = 0xf,
208c57997bcSThierry Reding 		.tx_pu_value = 0,
209c57997bcSThierry Reding 		.bg_temp_coef = 3,
210c57997bcSThierry Reding 		.bg_vref_level = 8,
211c57997bcSThierry Reding 		.avdd10_level = 4,
212c57997bcSThierry Reding 		.avdd14_level = 4,
213c57997bcSThierry Reding 		.sparepll = 0x54,
214c57997bcSThierry Reding 		.drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
215c57997bcSThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
216c57997bcSThierry Reding 	}, {
217c57997bcSThierry Reding 		.frequency = 75000000,
218c57997bcSThierry Reding 		.vcocap = 1,
219c57997bcSThierry Reding 		.filter = 5,
220c57997bcSThierry Reding 		.ichpmp = 5,
221c57997bcSThierry Reding 		.loadadj = 3,
222c57997bcSThierry Reding 		.tmds_termadj = 0xf,
223c57997bcSThierry Reding 		.tx_pu_value = 0,
224c57997bcSThierry Reding 		.bg_temp_coef = 3,
225c57997bcSThierry Reding 		.bg_vref_level = 8,
226c57997bcSThierry Reding 		.avdd10_level = 4,
227c57997bcSThierry Reding 		.avdd14_level = 4,
228c57997bcSThierry Reding 		.sparepll = 0x44,
229c57997bcSThierry Reding 		.drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
230c57997bcSThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
231c57997bcSThierry Reding 	}, {
232c57997bcSThierry Reding 		.frequency = 150000000,
233c57997bcSThierry Reding 		.vcocap = 3,
234c57997bcSThierry Reding 		.filter = 5,
235c57997bcSThierry Reding 		.ichpmp = 5,
236c57997bcSThierry Reding 		.loadadj = 3,
237c57997bcSThierry Reding 		.tmds_termadj = 15,
238c57997bcSThierry Reding 		.tx_pu_value = 0x66 /* 0 */,
239c57997bcSThierry Reding 		.bg_temp_coef = 3,
240c57997bcSThierry Reding 		.bg_vref_level = 8,
241c57997bcSThierry Reding 		.avdd10_level = 4,
242c57997bcSThierry Reding 		.avdd14_level = 4,
243c57997bcSThierry Reding 		.sparepll = 0x00, /* 0x34 */
244c57997bcSThierry Reding 		.drive_current = { 0x3a, 0x3a, 0x3a, 0x37 },
245c57997bcSThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
246c57997bcSThierry Reding 	}, {
247c57997bcSThierry Reding 		.frequency = 300000000,
248c57997bcSThierry Reding 		.vcocap = 3,
249c57997bcSThierry Reding 		.filter = 5,
250c57997bcSThierry Reding 		.ichpmp = 5,
251c57997bcSThierry Reding 		.loadadj = 3,
252c57997bcSThierry Reding 		.tmds_termadj = 15,
253c57997bcSThierry Reding 		.tx_pu_value = 64,
254c57997bcSThierry Reding 		.bg_temp_coef = 3,
255c57997bcSThierry Reding 		.bg_vref_level = 8,
256c57997bcSThierry Reding 		.avdd10_level = 4,
257c57997bcSThierry Reding 		.avdd14_level = 4,
258c57997bcSThierry Reding 		.sparepll = 0x34,
259c57997bcSThierry Reding 		.drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
260c57997bcSThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
261c57997bcSThierry Reding 	}, {
262c57997bcSThierry Reding 		.frequency = 600000000,
263c57997bcSThierry Reding 		.vcocap = 3,
264c57997bcSThierry Reding 		.filter = 5,
265c57997bcSThierry Reding 		.ichpmp = 5,
266c57997bcSThierry Reding 		.loadadj = 3,
267c57997bcSThierry Reding 		.tmds_termadj = 12,
268c57997bcSThierry Reding 		.tx_pu_value = 96,
269c57997bcSThierry Reding 		.bg_temp_coef = 3,
270c57997bcSThierry Reding 		.bg_vref_level = 8,
271c57997bcSThierry Reding 		.avdd10_level = 4,
272c57997bcSThierry Reding 		.avdd14_level = 4,
273c57997bcSThierry Reding 		.sparepll = 0x34,
274c57997bcSThierry Reding 		.drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
275c57997bcSThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
276c57997bcSThierry Reding 	}
277c57997bcSThierry Reding };
278c57997bcSThierry Reding 
2799b6c14b8SThierry Reding static const struct tegra_sor_hdmi_settings tegra194_sor_hdmi_defaults[] = {
2809b6c14b8SThierry Reding 	{
2819b6c14b8SThierry Reding 		.frequency = 54000000,
2829b6c14b8SThierry Reding 		.vcocap = 0,
2839b6c14b8SThierry Reding 		.filter = 5,
2849b6c14b8SThierry Reding 		.ichpmp = 5,
2859b6c14b8SThierry Reding 		.loadadj = 3,
2869b6c14b8SThierry Reding 		.tmds_termadj = 0xf,
2879b6c14b8SThierry Reding 		.tx_pu_value = 0,
2889b6c14b8SThierry Reding 		.bg_temp_coef = 3,
2899b6c14b8SThierry Reding 		.bg_vref_level = 8,
2909b6c14b8SThierry Reding 		.avdd10_level = 4,
2919b6c14b8SThierry Reding 		.avdd14_level = 4,
2929b6c14b8SThierry Reding 		.sparepll = 0x54,
2939b6c14b8SThierry Reding 		.drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
2949b6c14b8SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
2959b6c14b8SThierry Reding 	}, {
2969b6c14b8SThierry Reding 		.frequency = 75000000,
2979b6c14b8SThierry Reding 		.vcocap = 1,
2989b6c14b8SThierry Reding 		.filter = 5,
2999b6c14b8SThierry Reding 		.ichpmp = 5,
3009b6c14b8SThierry Reding 		.loadadj = 3,
3019b6c14b8SThierry Reding 		.tmds_termadj = 0xf,
3029b6c14b8SThierry Reding 		.tx_pu_value = 0,
3039b6c14b8SThierry Reding 		.bg_temp_coef = 3,
3049b6c14b8SThierry Reding 		.bg_vref_level = 8,
3059b6c14b8SThierry Reding 		.avdd10_level = 4,
3069b6c14b8SThierry Reding 		.avdd14_level = 4,
3079b6c14b8SThierry Reding 		.sparepll = 0x44,
3089b6c14b8SThierry Reding 		.drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
3099b6c14b8SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
3109b6c14b8SThierry Reding 	}, {
3119b6c14b8SThierry Reding 		.frequency = 150000000,
3129b6c14b8SThierry Reding 		.vcocap = 3,
3139b6c14b8SThierry Reding 		.filter = 5,
3149b6c14b8SThierry Reding 		.ichpmp = 5,
3159b6c14b8SThierry Reding 		.loadadj = 3,
3169b6c14b8SThierry Reding 		.tmds_termadj = 15,
3179b6c14b8SThierry Reding 		.tx_pu_value = 0x66 /* 0 */,
3189b6c14b8SThierry Reding 		.bg_temp_coef = 3,
3199b6c14b8SThierry Reding 		.bg_vref_level = 8,
3209b6c14b8SThierry Reding 		.avdd10_level = 4,
3219b6c14b8SThierry Reding 		.avdd14_level = 4,
3229b6c14b8SThierry Reding 		.sparepll = 0x00, /* 0x34 */
3239b6c14b8SThierry Reding 		.drive_current = { 0x3a, 0x3a, 0x3a, 0x37 },
3249b6c14b8SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
3259b6c14b8SThierry Reding 	}, {
3269b6c14b8SThierry Reding 		.frequency = 300000000,
3279b6c14b8SThierry Reding 		.vcocap = 3,
3289b6c14b8SThierry Reding 		.filter = 5,
3299b6c14b8SThierry Reding 		.ichpmp = 5,
3309b6c14b8SThierry Reding 		.loadadj = 3,
3319b6c14b8SThierry Reding 		.tmds_termadj = 15,
3329b6c14b8SThierry Reding 		.tx_pu_value = 64,
3339b6c14b8SThierry Reding 		.bg_temp_coef = 3,
3349b6c14b8SThierry Reding 		.bg_vref_level = 8,
3359b6c14b8SThierry Reding 		.avdd10_level = 4,
3369b6c14b8SThierry Reding 		.avdd14_level = 4,
3379b6c14b8SThierry Reding 		.sparepll = 0x34,
3389b6c14b8SThierry Reding 		.drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
3399b6c14b8SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
3409b6c14b8SThierry Reding 	}, {
3419b6c14b8SThierry Reding 		.frequency = 600000000,
3429b6c14b8SThierry Reding 		.vcocap = 3,
3439b6c14b8SThierry Reding 		.filter = 5,
3449b6c14b8SThierry Reding 		.ichpmp = 5,
3459b6c14b8SThierry Reding 		.loadadj = 3,
3469b6c14b8SThierry Reding 		.tmds_termadj = 12,
3479b6c14b8SThierry Reding 		.tx_pu_value = 96,
3489b6c14b8SThierry Reding 		.bg_temp_coef = 3,
3499b6c14b8SThierry Reding 		.bg_vref_level = 8,
3509b6c14b8SThierry Reding 		.avdd10_level = 4,
3519b6c14b8SThierry Reding 		.avdd14_level = 4,
3529b6c14b8SThierry Reding 		.sparepll = 0x34,
3539b6c14b8SThierry Reding 		.drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
3549b6c14b8SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
3559b6c14b8SThierry Reding 	}
3569b6c14b8SThierry Reding };
3579b6c14b8SThierry Reding 
358880cee0bSThierry Reding struct tegra_sor_regs {
359880cee0bSThierry Reding 	unsigned int head_state0;
360880cee0bSThierry Reding 	unsigned int head_state1;
361880cee0bSThierry Reding 	unsigned int head_state2;
362880cee0bSThierry Reding 	unsigned int head_state3;
363880cee0bSThierry Reding 	unsigned int head_state4;
364880cee0bSThierry Reding 	unsigned int head_state5;
365880cee0bSThierry Reding 	unsigned int pll0;
366880cee0bSThierry Reding 	unsigned int pll1;
367880cee0bSThierry Reding 	unsigned int pll2;
368880cee0bSThierry Reding 	unsigned int pll3;
369880cee0bSThierry Reding 	unsigned int dp_padctl0;
370880cee0bSThierry Reding 	unsigned int dp_padctl2;
371880cee0bSThierry Reding };
372880cee0bSThierry Reding 
373459cc2c6SThierry Reding struct tegra_sor_soc {
374459cc2c6SThierry Reding 	bool supports_edp;
375459cc2c6SThierry Reding 	bool supports_lvds;
376459cc2c6SThierry Reding 	bool supports_hdmi;
377459cc2c6SThierry Reding 	bool supports_dp;
378459cc2c6SThierry Reding 
379880cee0bSThierry Reding 	const struct tegra_sor_regs *regs;
380c57997bcSThierry Reding 	bool has_nvdisplay;
381880cee0bSThierry Reding 
382459cc2c6SThierry Reding 	const struct tegra_sor_hdmi_settings *settings;
383459cc2c6SThierry Reding 	unsigned int num_settings;
38430b49435SThierry Reding 
38530b49435SThierry Reding 	const u8 *xbar_cfg;
386c1763937SThierry Reding 	const u8 *lane_map;
387c1763937SThierry Reding 
388c1763937SThierry Reding 	const u8 (*voltage_swing)[4][4];
389c1763937SThierry Reding 	const u8 (*pre_emphasis)[4][4];
390c1763937SThierry Reding 	const u8 (*post_cursor)[4][4];
391c1763937SThierry Reding 	const u8 (*tx_pu)[4][4];
392459cc2c6SThierry Reding };
393459cc2c6SThierry Reding 
394459cc2c6SThierry Reding struct tegra_sor;
395459cc2c6SThierry Reding 
396459cc2c6SThierry Reding struct tegra_sor_ops {
397459cc2c6SThierry Reding 	const char *name;
398459cc2c6SThierry Reding 	int (*probe)(struct tegra_sor *sor);
399459cc2c6SThierry Reding 	int (*remove)(struct tegra_sor *sor);
400459cc2c6SThierry Reding };
401459cc2c6SThierry Reding 
4026b6b6042SThierry Reding struct tegra_sor {
4036b6b6042SThierry Reding 	struct host1x_client client;
4046b6b6042SThierry Reding 	struct tegra_output output;
4056b6b6042SThierry Reding 	struct device *dev;
4066b6b6042SThierry Reding 
407459cc2c6SThierry Reding 	const struct tegra_sor_soc *soc;
4086b6b6042SThierry Reding 	void __iomem *regs;
409c57997bcSThierry Reding 	unsigned int index;
4108e2988a7SThierry Reding 	unsigned int irq;
4116b6b6042SThierry Reding 
4126b6b6042SThierry Reding 	struct reset_control *rst;
4136b6b6042SThierry Reding 	struct clk *clk_parent;
4146b6b6042SThierry Reding 	struct clk *clk_safe;
415e1335e2fSThierry Reding 	struct clk *clk_out;
416e1335e2fSThierry Reding 	struct clk *clk_pad;
4176b6b6042SThierry Reding 	struct clk *clk_dp;
4186b6b6042SThierry Reding 	struct clk *clk;
4196b6b6042SThierry Reding 
4206d6c815dSThierry Reding 	u8 xbar_cfg[5];
4216d6c815dSThierry Reding 
422c1763937SThierry Reding 	struct drm_dp_link link;
4239542c237SThierry Reding 	struct drm_dp_aux *aux;
4246b6b6042SThierry Reding 
425dab16336SThierry Reding 	struct drm_info_list *debugfs_files;
426459cc2c6SThierry Reding 
427459cc2c6SThierry Reding 	const struct tegra_sor_ops *ops;
428c57997bcSThierry Reding 	enum tegra_io_pad pad;
429459cc2c6SThierry Reding 
430459cc2c6SThierry Reding 	/* for HDMI 2.0 */
431459cc2c6SThierry Reding 	struct tegra_sor_hdmi_settings *settings;
432459cc2c6SThierry Reding 	unsigned int num_settings;
433459cc2c6SThierry Reding 
434459cc2c6SThierry Reding 	struct regulator *avdd_io_supply;
435459cc2c6SThierry Reding 	struct regulator *vdd_pll_supply;
436459cc2c6SThierry Reding 	struct regulator *hdmi_supply;
43736e90221SThierry Reding 
43836e90221SThierry Reding 	struct delayed_work scdc;
43936e90221SThierry Reding 	bool scdc_enabled;
4408e2988a7SThierry Reding 
441fad7b806SThierry Reding 	struct tegra_hda_format format;
4426b6b6042SThierry Reding };
4436b6b6042SThierry Reding 
444c31efa7aSThierry Reding struct tegra_sor_state {
445c31efa7aSThierry Reding 	struct drm_connector_state base;
446c31efa7aSThierry Reding 
44736e90221SThierry Reding 	unsigned int link_speed;
44836e90221SThierry Reding 	unsigned long pclk;
449c31efa7aSThierry Reding 	unsigned int bpc;
450c31efa7aSThierry Reding };
451c31efa7aSThierry Reding 
452c31efa7aSThierry Reding static inline struct tegra_sor_state *
453c31efa7aSThierry Reding to_sor_state(struct drm_connector_state *state)
454c31efa7aSThierry Reding {
455c31efa7aSThierry Reding 	return container_of(state, struct tegra_sor_state, base);
456c31efa7aSThierry Reding }
457c31efa7aSThierry Reding 
45834fa183bSThierry Reding struct tegra_sor_config {
45934fa183bSThierry Reding 	u32 bits_per_pixel;
46034fa183bSThierry Reding 
46134fa183bSThierry Reding 	u32 active_polarity;
46234fa183bSThierry Reding 	u32 active_count;
46334fa183bSThierry Reding 	u32 tu_size;
46434fa183bSThierry Reding 	u32 active_frac;
46534fa183bSThierry Reding 	u32 watermark;
4667890b576SThierry Reding 
4677890b576SThierry Reding 	u32 hblank_symbols;
4687890b576SThierry Reding 	u32 vblank_symbols;
46934fa183bSThierry Reding };
47034fa183bSThierry Reding 
4716b6b6042SThierry Reding static inline struct tegra_sor *
4726b6b6042SThierry Reding host1x_client_to_sor(struct host1x_client *client)
4736b6b6042SThierry Reding {
4746b6b6042SThierry Reding 	return container_of(client, struct tegra_sor, client);
4756b6b6042SThierry Reding }
4766b6b6042SThierry Reding 
4776b6b6042SThierry Reding static inline struct tegra_sor *to_sor(struct tegra_output *output)
4786b6b6042SThierry Reding {
4796b6b6042SThierry Reding 	return container_of(output, struct tegra_sor, output);
4806b6b6042SThierry Reding }
4816b6b6042SThierry Reding 
4825c5f1301SThierry Reding static inline u32 tegra_sor_readl(struct tegra_sor *sor, unsigned int offset)
4836b6b6042SThierry Reding {
484932f6529SThierry Reding 	u32 value = readl(sor->regs + (offset << 2));
485932f6529SThierry Reding 
486932f6529SThierry Reding 	trace_sor_readl(sor->dev, offset, value);
487932f6529SThierry Reding 
488932f6529SThierry Reding 	return value;
4896b6b6042SThierry Reding }
4906b6b6042SThierry Reding 
49128fe2076SThierry Reding static inline void tegra_sor_writel(struct tegra_sor *sor, u32 value,
4925c5f1301SThierry Reding 				    unsigned int offset)
4936b6b6042SThierry Reding {
494932f6529SThierry Reding 	trace_sor_writel(sor->dev, offset, value);
4956b6b6042SThierry Reding 	writel(value, sor->regs + (offset << 2));
4966b6b6042SThierry Reding }
4976b6b6042SThierry Reding 
49825bb2cecSThierry Reding static int tegra_sor_set_parent_clock(struct tegra_sor *sor, struct clk *parent)
49925bb2cecSThierry Reding {
50025bb2cecSThierry Reding 	int err;
50125bb2cecSThierry Reding 
50225bb2cecSThierry Reding 	clk_disable_unprepare(sor->clk);
50325bb2cecSThierry Reding 
504e1335e2fSThierry Reding 	err = clk_set_parent(sor->clk_out, parent);
50525bb2cecSThierry Reding 	if (err < 0)
50625bb2cecSThierry Reding 		return err;
50725bb2cecSThierry Reding 
50825bb2cecSThierry Reding 	err = clk_prepare_enable(sor->clk);
50925bb2cecSThierry Reding 	if (err < 0)
51025bb2cecSThierry Reding 		return err;
51125bb2cecSThierry Reding 
51225bb2cecSThierry Reding 	return 0;
51325bb2cecSThierry Reding }
51425bb2cecSThierry Reding 
515e1335e2fSThierry Reding struct tegra_clk_sor_pad {
516b299221cSThierry Reding 	struct clk_hw hw;
517b299221cSThierry Reding 	struct tegra_sor *sor;
518b299221cSThierry Reding };
519b299221cSThierry Reding 
520e1335e2fSThierry Reding static inline struct tegra_clk_sor_pad *to_pad(struct clk_hw *hw)
521b299221cSThierry Reding {
522e1335e2fSThierry Reding 	return container_of(hw, struct tegra_clk_sor_pad, hw);
523b299221cSThierry Reding }
524b299221cSThierry Reding 
525*4bdf4710SThierry Reding static const char * const tegra_clk_sor_pad_parents[2][2] = {
526*4bdf4710SThierry Reding 	{ "pll_d_out0", "pll_dp" },
527*4bdf4710SThierry Reding 	{ "pll_d2_out0", "pll_dp" },
528b299221cSThierry Reding };
529b299221cSThierry Reding 
530e1335e2fSThierry Reding static int tegra_clk_sor_pad_set_parent(struct clk_hw *hw, u8 index)
531b299221cSThierry Reding {
532e1335e2fSThierry Reding 	struct tegra_clk_sor_pad *pad = to_pad(hw);
533e1335e2fSThierry Reding 	struct tegra_sor *sor = pad->sor;
534b299221cSThierry Reding 	u32 value;
535b299221cSThierry Reding 
536b299221cSThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
537b299221cSThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
538b299221cSThierry Reding 
539b299221cSThierry Reding 	switch (index) {
540b299221cSThierry Reding 	case 0:
541b299221cSThierry Reding 		value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
542b299221cSThierry Reding 		break;
543b299221cSThierry Reding 
544b299221cSThierry Reding 	case 1:
545b299221cSThierry Reding 		value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
546b299221cSThierry Reding 		break;
547b299221cSThierry Reding 	}
548b299221cSThierry Reding 
549b299221cSThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
550b299221cSThierry Reding 
551b299221cSThierry Reding 	return 0;
552b299221cSThierry Reding }
553b299221cSThierry Reding 
554e1335e2fSThierry Reding static u8 tegra_clk_sor_pad_get_parent(struct clk_hw *hw)
555b299221cSThierry Reding {
556e1335e2fSThierry Reding 	struct tegra_clk_sor_pad *pad = to_pad(hw);
557e1335e2fSThierry Reding 	struct tegra_sor *sor = pad->sor;
558b299221cSThierry Reding 	u8 parent = U8_MAX;
559b299221cSThierry Reding 	u32 value;
560b299221cSThierry Reding 
561b299221cSThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
562b299221cSThierry Reding 
563b299221cSThierry Reding 	switch (value & SOR_CLK_CNTRL_DP_CLK_SEL_MASK) {
564b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK:
565b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_PCLK:
566b299221cSThierry Reding 		parent = 0;
567b299221cSThierry Reding 		break;
568b299221cSThierry Reding 
569b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK:
570b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_DPCLK:
571b299221cSThierry Reding 		parent = 1;
572b299221cSThierry Reding 		break;
573b299221cSThierry Reding 	}
574b299221cSThierry Reding 
575b299221cSThierry Reding 	return parent;
576b299221cSThierry Reding }
577b299221cSThierry Reding 
578e1335e2fSThierry Reding static const struct clk_ops tegra_clk_sor_pad_ops = {
579e1335e2fSThierry Reding 	.set_parent = tegra_clk_sor_pad_set_parent,
580e1335e2fSThierry Reding 	.get_parent = tegra_clk_sor_pad_get_parent,
581b299221cSThierry Reding };
582b299221cSThierry Reding 
583e1335e2fSThierry Reding static struct clk *tegra_clk_sor_pad_register(struct tegra_sor *sor,
584b299221cSThierry Reding 					      const char *name)
585b299221cSThierry Reding {
586e1335e2fSThierry Reding 	struct tegra_clk_sor_pad *pad;
587b299221cSThierry Reding 	struct clk_init_data init;
588b299221cSThierry Reding 	struct clk *clk;
589b299221cSThierry Reding 
590e1335e2fSThierry Reding 	pad = devm_kzalloc(sor->dev, sizeof(*pad), GFP_KERNEL);
591e1335e2fSThierry Reding 	if (!pad)
592b299221cSThierry Reding 		return ERR_PTR(-ENOMEM);
593b299221cSThierry Reding 
594e1335e2fSThierry Reding 	pad->sor = sor;
595b299221cSThierry Reding 
596b299221cSThierry Reding 	init.name = name;
597b299221cSThierry Reding 	init.flags = 0;
598*4bdf4710SThierry Reding 	init.parent_names = tegra_clk_sor_pad_parents[sor->index];
599*4bdf4710SThierry Reding 	init.num_parents = ARRAY_SIZE(tegra_clk_sor_pad_parents[sor->index]);
600e1335e2fSThierry Reding 	init.ops = &tegra_clk_sor_pad_ops;
601b299221cSThierry Reding 
602e1335e2fSThierry Reding 	pad->hw.init = &init;
603b299221cSThierry Reding 
604e1335e2fSThierry Reding 	clk = devm_clk_register(sor->dev, &pad->hw);
605b299221cSThierry Reding 
606b299221cSThierry Reding 	return clk;
607b299221cSThierry Reding }
608b299221cSThierry Reding 
609c9533131SThierry Reding static void tegra_sor_filter_rates(struct tegra_sor *sor)
610c9533131SThierry Reding {
611c9533131SThierry Reding 	struct drm_dp_link *link = &sor->link;
612c9533131SThierry Reding 	unsigned int i;
613c9533131SThierry Reding 
614c9533131SThierry Reding 	/* Tegra only supports RBR, HBR and HBR2 */
615c9533131SThierry Reding 	for (i = 0; i < link->num_rates; i++) {
616c9533131SThierry Reding 		switch (link->rates[i]) {
617c9533131SThierry Reding 		case 1620000:
618c9533131SThierry Reding 		case 2700000:
619c9533131SThierry Reding 		case 5400000:
620c9533131SThierry Reding 			break;
621c9533131SThierry Reding 
622c9533131SThierry Reding 		default:
623c9533131SThierry Reding 			DRM_DEBUG_KMS("link rate %lu kHz not supported\n",
624c9533131SThierry Reding 				      link->rates[i]);
625c9533131SThierry Reding 			link->rates[i] = 0;
626c9533131SThierry Reding 			break;
627c9533131SThierry Reding 		}
628c9533131SThierry Reding 	}
629c9533131SThierry Reding 
630c9533131SThierry Reding 	drm_dp_link_update_rates(link);
631c9533131SThierry Reding }
632c9533131SThierry Reding 
633c1763937SThierry Reding static int tegra_sor_power_up_lanes(struct tegra_sor *sor, unsigned int lanes)
6346b6b6042SThierry Reding {
635c1763937SThierry Reding 	unsigned long timeout;
63628fe2076SThierry Reding 	u32 value;
6376b6b6042SThierry Reding 
638c1763937SThierry Reding 	/*
639c1763937SThierry Reding 	 * Clear or set the PD_TXD bit corresponding to each lane, depending
640c1763937SThierry Reding 	 * on whether it is used or not.
641c1763937SThierry Reding 	 */
642880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
643c1763937SThierry Reding 
644c1763937SThierry Reding 	if (lanes <= 2)
645c1763937SThierry Reding 		value &= ~(SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[3]) |
646c1763937SThierry Reding 			   SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[2]));
647c1763937SThierry Reding 	else
648c1763937SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[3]) |
649c1763937SThierry Reding 			 SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[2]);
650c1763937SThierry Reding 
651c1763937SThierry Reding 	if (lanes <= 1)
652c1763937SThierry Reding 		value &= ~SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[1]);
653c1763937SThierry Reding 	else
654c1763937SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[1]);
655c1763937SThierry Reding 
656c1763937SThierry Reding 	if (lanes == 0)
657c1763937SThierry Reding 		value &= ~SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[0]);
658c1763937SThierry Reding 	else
659c1763937SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[0]);
660c1763937SThierry Reding 
661880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
6626b6b6042SThierry Reding 
663c1763937SThierry Reding 	/* start lane sequencer */
664c1763937SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
665c1763937SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_UP;
666c1763937SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
667c1763937SThierry Reding 
668c1763937SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
669c1763937SThierry Reding 
670c1763937SThierry Reding 	while (time_before(jiffies, timeout)) {
671c1763937SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
672c1763937SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
673c1763937SThierry Reding 			break;
674c1763937SThierry Reding 
675c1763937SThierry Reding 		usleep_range(250, 1000);
676c1763937SThierry Reding 	}
677c1763937SThierry Reding 
678c1763937SThierry Reding 	if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
679c1763937SThierry Reding 		return -ETIMEDOUT;
680c1763937SThierry Reding 
681c1763937SThierry Reding 	return 0;
682c1763937SThierry Reding }
683c1763937SThierry Reding 
684c1763937SThierry Reding static int tegra_sor_power_down_lanes(struct tegra_sor *sor)
685c1763937SThierry Reding {
686c1763937SThierry Reding 	unsigned long timeout;
687c1763937SThierry Reding 	u32 value;
688c1763937SThierry Reding 
689c1763937SThierry Reding 	/* power down all lanes */
690880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
691c1763937SThierry Reding 	value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
692c1763937SThierry Reding 		   SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
693880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
6946b6b6042SThierry Reding 
695c1763937SThierry Reding 	/* start lane sequencer */
696c1763937SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
697c1763937SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
698c1763937SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
699c1763937SThierry Reding 
700c1763937SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
701c1763937SThierry Reding 
702c1763937SThierry Reding 	while (time_before(jiffies, timeout)) {
703c1763937SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
704c1763937SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
705c1763937SThierry Reding 			break;
706c1763937SThierry Reding 
707c1763937SThierry Reding 		usleep_range(25, 100);
708c1763937SThierry Reding 	}
709c1763937SThierry Reding 
710c1763937SThierry Reding 	if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
711c1763937SThierry Reding 		return -ETIMEDOUT;
712c1763937SThierry Reding 
713c1763937SThierry Reding 	return 0;
714c1763937SThierry Reding }
715c1763937SThierry Reding 
716c1763937SThierry Reding static void tegra_sor_dp_precharge(struct tegra_sor *sor, unsigned int lanes)
717c1763937SThierry Reding {
718c1763937SThierry Reding 	u32 value;
719c1763937SThierry Reding 
720c1763937SThierry Reding 	/* pre-charge all used lanes */
721c1763937SThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
722c1763937SThierry Reding 
723c1763937SThierry Reding 	if (lanes <= 2)
724c1763937SThierry Reding 		value &= ~(SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[3]) |
725c1763937SThierry Reding 			   SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[2]));
726c1763937SThierry Reding 	else
727c1763937SThierry Reding 		value |= SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[3]) |
728c1763937SThierry Reding 			 SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[2]);
729c1763937SThierry Reding 
730c1763937SThierry Reding 	if (lanes <= 1)
731c1763937SThierry Reding 		value &= ~SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[1]);
732c1763937SThierry Reding 	else
733c1763937SThierry Reding 		value |= SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[1]);
734c1763937SThierry Reding 
735c1763937SThierry Reding 	if (lanes == 0)
736c1763937SThierry Reding 		value &= ~SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[0]);
737c1763937SThierry Reding 	else
738c1763937SThierry Reding 		value |= SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[0]);
739c1763937SThierry Reding 
740c1763937SThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
741c1763937SThierry Reding 
742c1763937SThierry Reding 	usleep_range(15, 100);
7436b6b6042SThierry Reding 
744880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
7456b6b6042SThierry Reding 	value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
7466b6b6042SThierry Reding 		   SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
747880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
7486b6b6042SThierry Reding }
7496b6b6042SThierry Reding 
750c1763937SThierry Reding static void tegra_sor_dp_term_calibrate(struct tegra_sor *sor)
751c1763937SThierry Reding {
752c1763937SThierry Reding 	u32 mask = 0x08, adj = 0, value;
7536b6b6042SThierry Reding 
754c1763937SThierry Reding 	/* enable pad calibration logic */
755c1763937SThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
756c1763937SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
757c1763937SThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
7586b6b6042SThierry Reding 
759c1763937SThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll1);
760c1763937SThierry Reding 	value |= SOR_PLL1_TMDS_TERM;
761c1763937SThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll1);
7626b6b6042SThierry Reding 
763c1763937SThierry Reding 	while (mask) {
764c1763937SThierry Reding 		adj |= mask;
7656b6b6042SThierry Reding 
766c1763937SThierry Reding 		value = tegra_sor_readl(sor, sor->soc->regs->pll1);
767c1763937SThierry Reding 		value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
768c1763937SThierry Reding 		value |= SOR_PLL1_TMDS_TERMADJ(adj);
769c1763937SThierry Reding 		tegra_sor_writel(sor, value, sor->soc->regs->pll1);
770c1763937SThierry Reding 
771c1763937SThierry Reding 		usleep_range(100, 200);
772c1763937SThierry Reding 
773c1763937SThierry Reding 		value = tegra_sor_readl(sor, sor->soc->regs->pll1);
774c1763937SThierry Reding 		if (value & SOR_PLL1_TERM_COMPOUT)
775c1763937SThierry Reding 			adj &= ~mask;
776c1763937SThierry Reding 
777c1763937SThierry Reding 		mask >>= 1;
7786b6b6042SThierry Reding 	}
7796b6b6042SThierry Reding 
780c1763937SThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll1);
781c1763937SThierry Reding 	value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
782c1763937SThierry Reding 	value |= SOR_PLL1_TMDS_TERMADJ(adj);
783c1763937SThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll1);
7846b6b6042SThierry Reding 
785c1763937SThierry Reding 	/* disable pad calibration logic */
786c1763937SThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
787c1763937SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
788c1763937SThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
789c1763937SThierry Reding }
7906b6b6042SThierry Reding 
791c1763937SThierry Reding static int tegra_sor_dp_link_apply_training(struct drm_dp_link *link)
792c1763937SThierry Reding {
793c1763937SThierry Reding 	struct tegra_sor *sor = container_of(link, struct tegra_sor, link);
794c1763937SThierry Reding 	u32 voltage_swing = 0, pre_emphasis = 0, post_cursor = 0;
795c1763937SThierry Reding 	const struct tegra_sor_soc *soc = sor->soc;
796c1763937SThierry Reding 	u32 pattern = 0, tx_pu = 0, value;
797c1763937SThierry Reding 	unsigned int i;
7986b6b6042SThierry Reding 
799c1763937SThierry Reding 	for (value = 0, i = 0; i < link->lanes; i++) {
800c1763937SThierry Reding 		u8 vs = link->train.request.voltage_swing[i];
801c1763937SThierry Reding 		u8 pe = link->train.request.pre_emphasis[i];
802c1763937SThierry Reding 		u8 pc = link->train.request.post_cursor[i];
803c1763937SThierry Reding 		u8 shift = sor->soc->lane_map[i] << 3;
804c1763937SThierry Reding 
805c1763937SThierry Reding 		voltage_swing |= soc->voltage_swing[pc][vs][pe] << shift;
806c1763937SThierry Reding 		pre_emphasis |= soc->pre_emphasis[pc][vs][pe] << shift;
807c1763937SThierry Reding 		post_cursor |= soc->post_cursor[pc][vs][pe] << shift;
808c1763937SThierry Reding 
809c1763937SThierry Reding 		if (sor->soc->tx_pu[pc][vs][pe] > tx_pu)
810c1763937SThierry Reding 			tx_pu = sor->soc->tx_pu[pc][vs][pe];
811c1763937SThierry Reding 
812c1763937SThierry Reding 		switch (link->train.pattern) {
813c1763937SThierry Reding 		case DP_TRAINING_PATTERN_DISABLE:
814c1763937SThierry Reding 			value = SOR_DP_TPG_SCRAMBLER_GALIOS |
8156b6b6042SThierry Reding 				SOR_DP_TPG_PATTERN_NONE;
816c1763937SThierry Reding 			break;
817c1763937SThierry Reding 
818c1763937SThierry Reding 		case DP_TRAINING_PATTERN_1:
819c1763937SThierry Reding 			value = SOR_DP_TPG_SCRAMBLER_NONE |
820c1763937SThierry Reding 				SOR_DP_TPG_PATTERN_TRAIN1;
821c1763937SThierry Reding 			break;
822c1763937SThierry Reding 
823c1763937SThierry Reding 		case DP_TRAINING_PATTERN_2:
824c1763937SThierry Reding 			value = SOR_DP_TPG_SCRAMBLER_NONE |
825c1763937SThierry Reding 				SOR_DP_TPG_PATTERN_TRAIN2;
826c1763937SThierry Reding 			break;
827c1763937SThierry Reding 
828c1763937SThierry Reding 		case DP_TRAINING_PATTERN_3:
829c1763937SThierry Reding 			value = SOR_DP_TPG_SCRAMBLER_NONE |
830c1763937SThierry Reding 				SOR_DP_TPG_PATTERN_TRAIN3;
831c1763937SThierry Reding 			break;
832c1763937SThierry Reding 
833c1763937SThierry Reding 		default:
834c1763937SThierry Reding 			return -EINVAL;
8356b6b6042SThierry Reding 		}
8366b6b6042SThierry Reding 
837c1763937SThierry Reding 		if (link->caps.channel_coding)
838c1763937SThierry Reding 			value |= SOR_DP_TPG_CHANNEL_CODING;
8396b6b6042SThierry Reding 
840c1763937SThierry Reding 		pattern = pattern << 8 | value;
841c1763937SThierry Reding 	}
8426b6b6042SThierry Reding 
843c1763937SThierry Reding 	tegra_sor_writel(sor, voltage_swing, SOR_LANE_DRIVE_CURRENT0);
844c1763937SThierry Reding 	tegra_sor_writel(sor, pre_emphasis, SOR_LANE_PREEMPHASIS0);
845c1763937SThierry Reding 
846c1763937SThierry Reding 	if (link->caps.tps3_supported)
847c1763937SThierry Reding 		tegra_sor_writel(sor, post_cursor, SOR_LANE_POSTCURSOR0);
848c1763937SThierry Reding 
849c1763937SThierry Reding 	tegra_sor_writel(sor, pattern, SOR_DP_TPG);
850c1763937SThierry Reding 
851c1763937SThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
852c1763937SThierry Reding 	value &= ~SOR_DP_PADCTL_TX_PU_MASK;
853c1763937SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU_ENABLE;
854c1763937SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU(tx_pu);
855c1763937SThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
856c1763937SThierry Reding 
857c1763937SThierry Reding 	usleep_range(20, 100);
8586b6b6042SThierry Reding 
8596b6b6042SThierry Reding 	return 0;
8606b6b6042SThierry Reding }
8616b6b6042SThierry Reding 
862c1763937SThierry Reding static int tegra_sor_dp_link_configure(struct drm_dp_link *link)
863c1763937SThierry Reding {
864c1763937SThierry Reding 	struct tegra_sor *sor = container_of(link, struct tegra_sor, link);
865c1763937SThierry Reding 	unsigned int rate, lanes;
866c1763937SThierry Reding 	u32 value;
867c1763937SThierry Reding 	int err;
868c1763937SThierry Reding 
869c1763937SThierry Reding 	rate = drm_dp_link_rate_to_bw_code(link->rate);
870c1763937SThierry Reding 	lanes = link->lanes;
871c1763937SThierry Reding 
872c1763937SThierry Reding 	/* configure link speed and lane count */
873c1763937SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
874c1763937SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
875c1763937SThierry Reding 	value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
876c1763937SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
877c1763937SThierry Reding 
878c1763937SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
879c1763937SThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
880c1763937SThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
881c1763937SThierry Reding 
882c1763937SThierry Reding 	if (link->caps.enhanced_framing)
883c1763937SThierry Reding 		value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
884c1763937SThierry Reding 
885c1763937SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
886c1763937SThierry Reding 
887c1763937SThierry Reding 	usleep_range(400, 1000);
888c1763937SThierry Reding 
889c1763937SThierry Reding 	/* configure load pulse position adjustment */
890c1763937SThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll1);
891c1763937SThierry Reding 	value &= ~SOR_PLL1_LOADADJ_MASK;
892c1763937SThierry Reding 
893c1763937SThierry Reding 	switch (rate) {
894c1763937SThierry Reding 	case DP_LINK_BW_1_62:
895c1763937SThierry Reding 		value |= SOR_PLL1_LOADADJ(0x3);
896c1763937SThierry Reding 		break;
897c1763937SThierry Reding 
898c1763937SThierry Reding 	case DP_LINK_BW_2_7:
899c1763937SThierry Reding 		value |= SOR_PLL1_LOADADJ(0x4);
900c1763937SThierry Reding 		break;
901c1763937SThierry Reding 
902c1763937SThierry Reding 	case DP_LINK_BW_5_4:
903c1763937SThierry Reding 		value |= SOR_PLL1_LOADADJ(0x6);
904c1763937SThierry Reding 		break;
905c1763937SThierry Reding 	}
906c1763937SThierry Reding 
907c1763937SThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll1);
908c1763937SThierry Reding 
909c1763937SThierry Reding 	/* use alternate scrambler reset for eDP */
910c1763937SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
911c1763937SThierry Reding 
912c1763937SThierry Reding 	if (link->edp == 0)
913c1763937SThierry Reding 		value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
914c1763937SThierry Reding 	else
915c1763937SThierry Reding 		value |= SOR_DP_SPARE_PANEL_INTERNAL;
916c1763937SThierry Reding 
917c1763937SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
918c1763937SThierry Reding 
919c1763937SThierry Reding 	err = tegra_sor_power_down_lanes(sor);
920c1763937SThierry Reding 	if (err < 0) {
921c1763937SThierry Reding 		dev_err(sor->dev, "failed to power down lanes: %d\n", err);
922c1763937SThierry Reding 		return err;
923c1763937SThierry Reding 	}
924c1763937SThierry Reding 
925c1763937SThierry Reding 	/* power up and pre-charge lanes */
926c1763937SThierry Reding 	err = tegra_sor_power_up_lanes(sor, lanes);
927c1763937SThierry Reding 	if (err < 0) {
928c1763937SThierry Reding 		dev_err(sor->dev, "failed to power up %u lane%s: %d\n",
929c1763937SThierry Reding 			lanes, (lanes != 1) ? "s" : "", err);
930c1763937SThierry Reding 		return err;
931c1763937SThierry Reding 	}
932c1763937SThierry Reding 
933c1763937SThierry Reding 	tegra_sor_dp_precharge(sor, lanes);
934c1763937SThierry Reding 
935c1763937SThierry Reding 	return 0;
936c1763937SThierry Reding }
937c1763937SThierry Reding 
938c1763937SThierry Reding static const struct drm_dp_link_ops tegra_sor_dp_link_ops = {
939c1763937SThierry Reding 	.apply_training = tegra_sor_dp_link_apply_training,
940c1763937SThierry Reding 	.configure = tegra_sor_dp_link_configure,
941c1763937SThierry Reding };
942c1763937SThierry Reding 
9436b6b6042SThierry Reding static void tegra_sor_super_update(struct tegra_sor *sor)
9446b6b6042SThierry Reding {
945a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
946a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 1, SOR_SUPER_STATE0);
947a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
9486b6b6042SThierry Reding }
9496b6b6042SThierry Reding 
9506b6b6042SThierry Reding static void tegra_sor_update(struct tegra_sor *sor)
9516b6b6042SThierry Reding {
952a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE0);
953a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 1, SOR_STATE0);
954a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE0);
9556b6b6042SThierry Reding }
9566b6b6042SThierry Reding 
9576b6b6042SThierry Reding static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
9586b6b6042SThierry Reding {
95928fe2076SThierry Reding 	u32 value;
9606b6b6042SThierry Reding 
9616b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWM_DIV);
9626b6b6042SThierry Reding 	value &= ~SOR_PWM_DIV_MASK;
9636b6b6042SThierry Reding 	value |= 0x400; /* period */
9646b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWM_DIV);
9656b6b6042SThierry Reding 
9666b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWM_CTL);
9676b6b6042SThierry Reding 	value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
9686b6b6042SThierry Reding 	value |= 0x400; /* duty cycle */
9696b6b6042SThierry Reding 	value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
9706b6b6042SThierry Reding 	value |= SOR_PWM_CTL_TRIGGER;
9716b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWM_CTL);
9726b6b6042SThierry Reding 
9736b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
9746b6b6042SThierry Reding 
9756b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
9766b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWM_CTL);
9776b6b6042SThierry Reding 		if ((value & SOR_PWM_CTL_TRIGGER) == 0)
9786b6b6042SThierry Reding 			return 0;
9796b6b6042SThierry Reding 
9806b6b6042SThierry Reding 		usleep_range(25, 100);
9816b6b6042SThierry Reding 	}
9826b6b6042SThierry Reding 
9836b6b6042SThierry Reding 	return -ETIMEDOUT;
9846b6b6042SThierry Reding }
9856b6b6042SThierry Reding 
9866b6b6042SThierry Reding static int tegra_sor_attach(struct tegra_sor *sor)
9876b6b6042SThierry Reding {
9886b6b6042SThierry Reding 	unsigned long value, timeout;
9896b6b6042SThierry Reding 
9906b6b6042SThierry Reding 	/* wake up in normal mode */
991a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
9926b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
9936b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_MODE_NORMAL;
994a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
9956b6b6042SThierry Reding 	tegra_sor_super_update(sor);
9966b6b6042SThierry Reding 
9976b6b6042SThierry Reding 	/* attach */
998a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
9996b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_ATTACHED;
1000a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
10016b6b6042SThierry Reding 	tegra_sor_super_update(sor);
10026b6b6042SThierry Reding 
10036b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
10046b6b6042SThierry Reding 
10056b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
10066b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
10076b6b6042SThierry Reding 		if ((value & SOR_TEST_ATTACHED) != 0)
10086b6b6042SThierry Reding 			return 0;
10096b6b6042SThierry Reding 
10106b6b6042SThierry Reding 		usleep_range(25, 100);
10116b6b6042SThierry Reding 	}
10126b6b6042SThierry Reding 
10136b6b6042SThierry Reding 	return -ETIMEDOUT;
10146b6b6042SThierry Reding }
10156b6b6042SThierry Reding 
10166b6b6042SThierry Reding static int tegra_sor_wakeup(struct tegra_sor *sor)
10176b6b6042SThierry Reding {
10186b6b6042SThierry Reding 	unsigned long value, timeout;
10196b6b6042SThierry Reding 
10206b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
10216b6b6042SThierry Reding 
10226b6b6042SThierry Reding 	/* wait for head to wake up */
10236b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
10246b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
10256b6b6042SThierry Reding 		value &= SOR_TEST_HEAD_MODE_MASK;
10266b6b6042SThierry Reding 
10276b6b6042SThierry Reding 		if (value == SOR_TEST_HEAD_MODE_AWAKE)
10286b6b6042SThierry Reding 			return 0;
10296b6b6042SThierry Reding 
10306b6b6042SThierry Reding 		usleep_range(25, 100);
10316b6b6042SThierry Reding 	}
10326b6b6042SThierry Reding 
10336b6b6042SThierry Reding 	return -ETIMEDOUT;
10346b6b6042SThierry Reding }
10356b6b6042SThierry Reding 
10366b6b6042SThierry Reding static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
10376b6b6042SThierry Reding {
103828fe2076SThierry Reding 	u32 value;
10396b6b6042SThierry Reding 
10406b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWR);
10416b6b6042SThierry Reding 	value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
10426b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWR);
10436b6b6042SThierry Reding 
10446b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
10456b6b6042SThierry Reding 
10466b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
10476b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
10486b6b6042SThierry Reding 		if ((value & SOR_PWR_TRIGGER) == 0)
10496b6b6042SThierry Reding 			return 0;
10506b6b6042SThierry Reding 
10516b6b6042SThierry Reding 		usleep_range(25, 100);
10526b6b6042SThierry Reding 	}
10536b6b6042SThierry Reding 
10546b6b6042SThierry Reding 	return -ETIMEDOUT;
10556b6b6042SThierry Reding }
10566b6b6042SThierry Reding 
105734fa183bSThierry Reding struct tegra_sor_params {
105834fa183bSThierry Reding 	/* number of link clocks per line */
105934fa183bSThierry Reding 	unsigned int num_clocks;
106034fa183bSThierry Reding 	/* ratio between input and output */
106134fa183bSThierry Reding 	u64 ratio;
106234fa183bSThierry Reding 	/* precision factor */
106334fa183bSThierry Reding 	u64 precision;
106434fa183bSThierry Reding 
106534fa183bSThierry Reding 	unsigned int active_polarity;
106634fa183bSThierry Reding 	unsigned int active_count;
106734fa183bSThierry Reding 	unsigned int active_frac;
106834fa183bSThierry Reding 	unsigned int tu_size;
106934fa183bSThierry Reding 	unsigned int error;
107034fa183bSThierry Reding };
107134fa183bSThierry Reding 
107234fa183bSThierry Reding static int tegra_sor_compute_params(struct tegra_sor *sor,
107334fa183bSThierry Reding 				    struct tegra_sor_params *params,
107434fa183bSThierry Reding 				    unsigned int tu_size)
107534fa183bSThierry Reding {
107634fa183bSThierry Reding 	u64 active_sym, active_count, frac, approx;
107734fa183bSThierry Reding 	u32 active_polarity, active_frac = 0;
107834fa183bSThierry Reding 	const u64 f = params->precision;
107934fa183bSThierry Reding 	s64 error;
108034fa183bSThierry Reding 
108134fa183bSThierry Reding 	active_sym = params->ratio * tu_size;
108234fa183bSThierry Reding 	active_count = div_u64(active_sym, f) * f;
108334fa183bSThierry Reding 	frac = active_sym - active_count;
108434fa183bSThierry Reding 
108534fa183bSThierry Reding 	/* fraction < 0.5 */
108634fa183bSThierry Reding 	if (frac >= (f / 2)) {
108734fa183bSThierry Reding 		active_polarity = 1;
108834fa183bSThierry Reding 		frac = f - frac;
108934fa183bSThierry Reding 	} else {
109034fa183bSThierry Reding 		active_polarity = 0;
109134fa183bSThierry Reding 	}
109234fa183bSThierry Reding 
109334fa183bSThierry Reding 	if (frac != 0) {
109434fa183bSThierry Reding 		frac = div_u64(f * f,  frac); /* 1/fraction */
109534fa183bSThierry Reding 		if (frac <= (15 * f)) {
109634fa183bSThierry Reding 			active_frac = div_u64(frac, f);
109734fa183bSThierry Reding 
109834fa183bSThierry Reding 			/* round up */
109934fa183bSThierry Reding 			if (active_polarity)
110034fa183bSThierry Reding 				active_frac++;
110134fa183bSThierry Reding 		} else {
110234fa183bSThierry Reding 			active_frac = active_polarity ? 1 : 15;
110334fa183bSThierry Reding 		}
110434fa183bSThierry Reding 	}
110534fa183bSThierry Reding 
110634fa183bSThierry Reding 	if (active_frac == 1)
110734fa183bSThierry Reding 		active_polarity = 0;
110834fa183bSThierry Reding 
110934fa183bSThierry Reding 	if (active_polarity == 1) {
111034fa183bSThierry Reding 		if (active_frac) {
111134fa183bSThierry Reding 			approx = active_count + (active_frac * (f - 1)) * f;
111234fa183bSThierry Reding 			approx = div_u64(approx, active_frac * f);
111334fa183bSThierry Reding 		} else {
111434fa183bSThierry Reding 			approx = active_count + f;
111534fa183bSThierry Reding 		}
111634fa183bSThierry Reding 	} else {
111734fa183bSThierry Reding 		if (active_frac)
111834fa183bSThierry Reding 			approx = active_count + div_u64(f, active_frac);
111934fa183bSThierry Reding 		else
112034fa183bSThierry Reding 			approx = active_count;
112134fa183bSThierry Reding 	}
112234fa183bSThierry Reding 
112334fa183bSThierry Reding 	error = div_s64(active_sym - approx, tu_size);
112434fa183bSThierry Reding 	error *= params->num_clocks;
112534fa183bSThierry Reding 
112679211c8eSAndrew Morton 	if (error <= 0 && abs(error) < params->error) {
112734fa183bSThierry Reding 		params->active_count = div_u64(active_count, f);
112834fa183bSThierry Reding 		params->active_polarity = active_polarity;
112934fa183bSThierry Reding 		params->active_frac = active_frac;
113079211c8eSAndrew Morton 		params->error = abs(error);
113134fa183bSThierry Reding 		params->tu_size = tu_size;
113234fa183bSThierry Reding 
113334fa183bSThierry Reding 		if (error == 0)
113434fa183bSThierry Reding 			return true;
113534fa183bSThierry Reding 	}
113634fa183bSThierry Reding 
113734fa183bSThierry Reding 	return false;
113834fa183bSThierry Reding }
113934fa183bSThierry Reding 
1140a198359eSThierry Reding static int tegra_sor_compute_config(struct tegra_sor *sor,
114180444495SThierry Reding 				    const struct drm_display_mode *mode,
114234fa183bSThierry Reding 				    struct tegra_sor_config *config,
114334fa183bSThierry Reding 				    struct drm_dp_link *link)
114434fa183bSThierry Reding {
114534fa183bSThierry Reding 	const u64 f = 100000, link_rate = link->rate * 1000;
114634fa183bSThierry Reding 	const u64 pclk = mode->clock * 1000;
11477890b576SThierry Reding 	u64 input, output, watermark, num;
114834fa183bSThierry Reding 	struct tegra_sor_params params;
114934fa183bSThierry Reding 	u32 num_syms_per_line;
115034fa183bSThierry Reding 	unsigned int i;
115134fa183bSThierry Reding 
1152c728e2d4SThierry Reding 	if (!link_rate || !link->lanes || !pclk || !config->bits_per_pixel)
115334fa183bSThierry Reding 		return -EINVAL;
115434fa183bSThierry Reding 
115534fa183bSThierry Reding 	input = pclk * config->bits_per_pixel;
1156c728e2d4SThierry Reding 	output = link_rate * 8 * link->lanes;
115734fa183bSThierry Reding 
115834fa183bSThierry Reding 	if (input >= output)
115934fa183bSThierry Reding 		return -ERANGE;
116034fa183bSThierry Reding 
116134fa183bSThierry Reding 	memset(&params, 0, sizeof(params));
116234fa183bSThierry Reding 	params.ratio = div64_u64(input * f, output);
116334fa183bSThierry Reding 	params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
116434fa183bSThierry Reding 	params.precision = f;
116534fa183bSThierry Reding 	params.error = 64 * f;
116634fa183bSThierry Reding 	params.tu_size = 64;
116734fa183bSThierry Reding 
116834fa183bSThierry Reding 	for (i = params.tu_size; i >= 32; i--)
116934fa183bSThierry Reding 		if (tegra_sor_compute_params(sor, &params, i))
117034fa183bSThierry Reding 			break;
117134fa183bSThierry Reding 
117234fa183bSThierry Reding 	if (params.active_frac == 0) {
117334fa183bSThierry Reding 		config->active_polarity = 0;
117434fa183bSThierry Reding 		config->active_count = params.active_count;
117534fa183bSThierry Reding 
117634fa183bSThierry Reding 		if (!params.active_polarity)
117734fa183bSThierry Reding 			config->active_count--;
117834fa183bSThierry Reding 
117934fa183bSThierry Reding 		config->tu_size = params.tu_size;
118034fa183bSThierry Reding 		config->active_frac = 1;
118134fa183bSThierry Reding 	} else {
118234fa183bSThierry Reding 		config->active_polarity = params.active_polarity;
118334fa183bSThierry Reding 		config->active_count = params.active_count;
118434fa183bSThierry Reding 		config->active_frac = params.active_frac;
118534fa183bSThierry Reding 		config->tu_size = params.tu_size;
118634fa183bSThierry Reding 	}
118734fa183bSThierry Reding 
118834fa183bSThierry Reding 	dev_dbg(sor->dev,
118934fa183bSThierry Reding 		"polarity: %d active count: %d tu size: %d active frac: %d\n",
119034fa183bSThierry Reding 		config->active_polarity, config->active_count,
119134fa183bSThierry Reding 		config->tu_size, config->active_frac);
119234fa183bSThierry Reding 
119334fa183bSThierry Reding 	watermark = params.ratio * config->tu_size * (f - params.ratio);
119434fa183bSThierry Reding 	watermark = div_u64(watermark, f);
119534fa183bSThierry Reding 
119634fa183bSThierry Reding 	watermark = div_u64(watermark + params.error, f);
119734fa183bSThierry Reding 	config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
119834fa183bSThierry Reding 	num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
1199c728e2d4SThierry Reding 			    (link->lanes * 8);
120034fa183bSThierry Reding 
120134fa183bSThierry Reding 	if (config->watermark > 30) {
120234fa183bSThierry Reding 		config->watermark = 30;
120334fa183bSThierry Reding 		dev_err(sor->dev,
120434fa183bSThierry Reding 			"unable to compute TU size, forcing watermark to %u\n",
120534fa183bSThierry Reding 			config->watermark);
120634fa183bSThierry Reding 	} else if (config->watermark > num_syms_per_line) {
120734fa183bSThierry Reding 		config->watermark = num_syms_per_line;
120834fa183bSThierry Reding 		dev_err(sor->dev, "watermark too high, forcing to %u\n",
120934fa183bSThierry Reding 			config->watermark);
121034fa183bSThierry Reding 	}
121134fa183bSThierry Reding 
12127890b576SThierry Reding 	/* compute the number of symbols per horizontal blanking interval */
12137890b576SThierry Reding 	num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
12147890b576SThierry Reding 	config->hblank_symbols = div_u64(num, pclk);
12157890b576SThierry Reding 
121627ba465cSThierry Reding 	if (link->caps.enhanced_framing)
12177890b576SThierry Reding 		config->hblank_symbols -= 3;
12187890b576SThierry Reding 
1219c728e2d4SThierry Reding 	config->hblank_symbols -= 12 / link->lanes;
12207890b576SThierry Reding 
12217890b576SThierry Reding 	/* compute the number of symbols per vertical blanking interval */
12227890b576SThierry Reding 	num = (mode->hdisplay - 25) * link_rate;
12237890b576SThierry Reding 	config->vblank_symbols = div_u64(num, pclk);
1224c728e2d4SThierry Reding 	config->vblank_symbols -= 36 / link->lanes + 4;
12257890b576SThierry Reding 
12267890b576SThierry Reding 	dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
12277890b576SThierry Reding 		config->vblank_symbols);
12287890b576SThierry Reding 
122934fa183bSThierry Reding 	return 0;
123034fa183bSThierry Reding }
123134fa183bSThierry Reding 
1232402f6bcdSThierry Reding static void tegra_sor_apply_config(struct tegra_sor *sor,
1233402f6bcdSThierry Reding 				   const struct tegra_sor_config *config)
1234402f6bcdSThierry Reding {
1235402f6bcdSThierry Reding 	u32 value;
1236402f6bcdSThierry Reding 
1237402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
1238402f6bcdSThierry Reding 	value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
1239402f6bcdSThierry Reding 	value |= SOR_DP_LINKCTL_TU_SIZE(config->tu_size);
1240402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
1241402f6bcdSThierry Reding 
1242402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_CONFIG0);
1243402f6bcdSThierry Reding 	value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
1244402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_WATERMARK(config->watermark);
1245402f6bcdSThierry Reding 
1246402f6bcdSThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
1247402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config->active_count);
1248402f6bcdSThierry Reding 
1249402f6bcdSThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
1250402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config->active_frac);
1251402f6bcdSThierry Reding 
1252402f6bcdSThierry Reding 	if (config->active_polarity)
1253402f6bcdSThierry Reding 		value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
1254402f6bcdSThierry Reding 	else
1255402f6bcdSThierry Reding 		value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
1256402f6bcdSThierry Reding 
1257402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
1258402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
1259402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_CONFIG0);
1260402f6bcdSThierry Reding 
1261402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
1262402f6bcdSThierry Reding 	value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
1263402f6bcdSThierry Reding 	value |= config->hblank_symbols & 0xffff;
1264402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
1265402f6bcdSThierry Reding 
1266402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
1267402f6bcdSThierry Reding 	value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
1268402f6bcdSThierry Reding 	value |= config->vblank_symbols & 0xffff;
1269402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
1270402f6bcdSThierry Reding }
1271402f6bcdSThierry Reding 
12722bd1dd39SThierry Reding static void tegra_sor_mode_set(struct tegra_sor *sor,
12732bd1dd39SThierry Reding 			       const struct drm_display_mode *mode,
1274c31efa7aSThierry Reding 			       struct tegra_sor_state *state)
12752bd1dd39SThierry Reding {
12762bd1dd39SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(sor->output.encoder.crtc);
12772bd1dd39SThierry Reding 	unsigned int vbe, vse, hbe, hse, vbs, hbs;
12782bd1dd39SThierry Reding 	u32 value;
12792bd1dd39SThierry Reding 
12802bd1dd39SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
12812bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_PIXELDEPTH_MASK;
12822bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
12832bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_OWNER_MASK;
12842bd1dd39SThierry Reding 
12852bd1dd39SThierry Reding 	value |= SOR_STATE_ASY_CRC_MODE_COMPLETE |
12862bd1dd39SThierry Reding 		 SOR_STATE_ASY_OWNER(dc->pipe + 1);
12872bd1dd39SThierry Reding 
12882bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PHSYNC)
12892bd1dd39SThierry Reding 		value &= ~SOR_STATE_ASY_HSYNCPOL;
12902bd1dd39SThierry Reding 
12912bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
12922bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_HSYNCPOL;
12932bd1dd39SThierry Reding 
12942bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PVSYNC)
12952bd1dd39SThierry Reding 		value &= ~SOR_STATE_ASY_VSYNCPOL;
12962bd1dd39SThierry Reding 
12972bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
12982bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_VSYNCPOL;
12992bd1dd39SThierry Reding 
1300c31efa7aSThierry Reding 	switch (state->bpc) {
1301c31efa7aSThierry Reding 	case 16:
1302c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_48_444;
1303c31efa7aSThierry Reding 		break;
1304c31efa7aSThierry Reding 
1305c31efa7aSThierry Reding 	case 12:
1306c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_36_444;
1307c31efa7aSThierry Reding 		break;
1308c31efa7aSThierry Reding 
1309c31efa7aSThierry Reding 	case 10:
1310c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_30_444;
1311c31efa7aSThierry Reding 		break;
1312c31efa7aSThierry Reding 
13132bd1dd39SThierry Reding 	case 8:
13142bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
13152bd1dd39SThierry Reding 		break;
13162bd1dd39SThierry Reding 
13172bd1dd39SThierry Reding 	case 6:
13182bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
13192bd1dd39SThierry Reding 		break;
13202bd1dd39SThierry Reding 
13212bd1dd39SThierry Reding 	default:
1322c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
13232bd1dd39SThierry Reding 		break;
13242bd1dd39SThierry Reding 	}
13252bd1dd39SThierry Reding 
13262bd1dd39SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
13272bd1dd39SThierry Reding 
13282bd1dd39SThierry Reding 	/*
13292bd1dd39SThierry Reding 	 * TODO: The video timing programming below doesn't seem to match the
13302bd1dd39SThierry Reding 	 * register definitions.
13312bd1dd39SThierry Reding 	 */
13322bd1dd39SThierry Reding 
13332bd1dd39SThierry Reding 	value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
1334880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state1 + dc->pipe);
13352bd1dd39SThierry Reding 
13362bd1dd39SThierry Reding 	/* sync end = sync width - 1 */
13372bd1dd39SThierry Reding 	vse = mode->vsync_end - mode->vsync_start - 1;
13382bd1dd39SThierry Reding 	hse = mode->hsync_end - mode->hsync_start - 1;
13392bd1dd39SThierry Reding 
13402bd1dd39SThierry Reding 	value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
1341880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state2 + dc->pipe);
13422bd1dd39SThierry Reding 
13432bd1dd39SThierry Reding 	/* blank end = sync end + back porch */
13442bd1dd39SThierry Reding 	vbe = vse + (mode->vtotal - mode->vsync_end);
13452bd1dd39SThierry Reding 	hbe = hse + (mode->htotal - mode->hsync_end);
13462bd1dd39SThierry Reding 
13472bd1dd39SThierry Reding 	value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
1348880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state3 + dc->pipe);
13492bd1dd39SThierry Reding 
13502bd1dd39SThierry Reding 	/* blank start = blank end + active */
13512bd1dd39SThierry Reding 	vbs = vbe + mode->vdisplay;
13522bd1dd39SThierry Reding 	hbs = hbe + mode->hdisplay;
13532bd1dd39SThierry Reding 
13542bd1dd39SThierry Reding 	value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
1355880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state4 + dc->pipe);
13562bd1dd39SThierry Reding 
13572bd1dd39SThierry Reding 	/* XXX interlacing support */
1358880cee0bSThierry Reding 	tegra_sor_writel(sor, 0x001, sor->soc->regs->head_state5 + dc->pipe);
13592bd1dd39SThierry Reding }
13602bd1dd39SThierry Reding 
13616fad8f66SThierry Reding static int tegra_sor_detach(struct tegra_sor *sor)
13626b6b6042SThierry Reding {
13636fad8f66SThierry Reding 	unsigned long value, timeout;
13646fad8f66SThierry Reding 
13656fad8f66SThierry Reding 	/* switch to safe mode */
1366a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
13676fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_MODE_NORMAL;
1368a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
13696fad8f66SThierry Reding 	tegra_sor_super_update(sor);
13706fad8f66SThierry Reding 
13716fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
13726fad8f66SThierry Reding 
13736fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
13746fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
13756fad8f66SThierry Reding 		if (value & SOR_PWR_MODE_SAFE)
13766fad8f66SThierry Reding 			break;
13776fad8f66SThierry Reding 	}
13786fad8f66SThierry Reding 
13796fad8f66SThierry Reding 	if ((value & SOR_PWR_MODE_SAFE) == 0)
13806fad8f66SThierry Reding 		return -ETIMEDOUT;
13816fad8f66SThierry Reding 
13826fad8f66SThierry Reding 	/* go to sleep */
1383a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
13846fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
1385a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
13866fad8f66SThierry Reding 	tegra_sor_super_update(sor);
13876fad8f66SThierry Reding 
13886fad8f66SThierry Reding 	/* detach */
1389a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
13906fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_ATTACHED;
1391a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
13926fad8f66SThierry Reding 	tegra_sor_super_update(sor);
13936fad8f66SThierry Reding 
13946fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
13956fad8f66SThierry Reding 
13966fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
13976fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
13986fad8f66SThierry Reding 		if ((value & SOR_TEST_ATTACHED) == 0)
13996fad8f66SThierry Reding 			break;
14006fad8f66SThierry Reding 
14016fad8f66SThierry Reding 		usleep_range(25, 100);
14026fad8f66SThierry Reding 	}
14036fad8f66SThierry Reding 
14046fad8f66SThierry Reding 	if ((value & SOR_TEST_ATTACHED) != 0)
14056fad8f66SThierry Reding 		return -ETIMEDOUT;
14066fad8f66SThierry Reding 
14076fad8f66SThierry Reding 	return 0;
14086fad8f66SThierry Reding }
14096fad8f66SThierry Reding 
14106fad8f66SThierry Reding static int tegra_sor_power_down(struct tegra_sor *sor)
14116fad8f66SThierry Reding {
14126fad8f66SThierry Reding 	unsigned long value, timeout;
14136fad8f66SThierry Reding 	int err;
14146fad8f66SThierry Reding 
14156fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWR);
14166fad8f66SThierry Reding 	value &= ~SOR_PWR_NORMAL_STATE_PU;
14176fad8f66SThierry Reding 	value |= SOR_PWR_TRIGGER;
14186fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWR);
14196fad8f66SThierry Reding 
14206fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
14216fad8f66SThierry Reding 
14226fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
14236fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
14246fad8f66SThierry Reding 		if ((value & SOR_PWR_TRIGGER) == 0)
14256fad8f66SThierry Reding 			return 0;
14266fad8f66SThierry Reding 
14276fad8f66SThierry Reding 		usleep_range(25, 100);
14286fad8f66SThierry Reding 	}
14296fad8f66SThierry Reding 
14306fad8f66SThierry Reding 	if ((value & SOR_PWR_TRIGGER) != 0)
14316fad8f66SThierry Reding 		return -ETIMEDOUT;
14326fad8f66SThierry Reding 
143325bb2cecSThierry Reding 	/* switch to safe parent clock */
143425bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
1435e1335e2fSThierry Reding 	if (err < 0) {
14366fad8f66SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
1437e1335e2fSThierry Reding 		return err;
1438e1335e2fSThierry Reding 	}
14396fad8f66SThierry Reding 
1440880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1441a9a9e4fdSThierry Reding 	value |= SOR_PLL2_PORT_POWERDOWN;
1442880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
14436fad8f66SThierry Reding 
14446fad8f66SThierry Reding 	usleep_range(20, 100);
14456fad8f66SThierry Reding 
1446880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
1447a9a9e4fdSThierry Reding 	value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
1448880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
14496fad8f66SThierry Reding 
1450880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1451a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD;
1452a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1453880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
14546fad8f66SThierry Reding 
14556fad8f66SThierry Reding 	usleep_range(20, 100);
14566fad8f66SThierry Reding 
14576fad8f66SThierry Reding 	return 0;
14586fad8f66SThierry Reding }
14596fad8f66SThierry Reding 
14606fad8f66SThierry Reding static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
14616fad8f66SThierry Reding {
14626fad8f66SThierry Reding 	u32 value;
14636fad8f66SThierry Reding 
14646fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
14656fad8f66SThierry Reding 
14666fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
1467a9a9e4fdSThierry Reding 		value = tegra_sor_readl(sor, SOR_CRCA);
1468a9a9e4fdSThierry Reding 		if (value & SOR_CRCA_VALID)
14696fad8f66SThierry Reding 			return 0;
14706fad8f66SThierry Reding 
14716fad8f66SThierry Reding 		usleep_range(100, 200);
14726fad8f66SThierry Reding 	}
14736fad8f66SThierry Reding 
14746fad8f66SThierry Reding 	return -ETIMEDOUT;
14756fad8f66SThierry Reding }
14766fad8f66SThierry Reding 
1477530239a8SThierry Reding static int tegra_sor_show_crc(struct seq_file *s, void *data)
14786fad8f66SThierry Reding {
1479530239a8SThierry Reding 	struct drm_info_node *node = s->private;
1480530239a8SThierry Reding 	struct tegra_sor *sor = node->info_ent->data;
1481850bab44SThierry Reding 	struct drm_crtc *crtc = sor->output.encoder.crtc;
1482850bab44SThierry Reding 	struct drm_device *drm = node->minor->dev;
1483530239a8SThierry Reding 	int err = 0;
14846fad8f66SThierry Reding 	u32 value;
14856fad8f66SThierry Reding 
1486850bab44SThierry Reding 	drm_modeset_lock_all(drm);
14876fad8f66SThierry Reding 
1488850bab44SThierry Reding 	if (!crtc || !crtc->state->active) {
1489850bab44SThierry Reding 		err = -EBUSY;
14906fad8f66SThierry Reding 		goto unlock;
14916fad8f66SThierry Reding 	}
14926fad8f66SThierry Reding 
1493a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
14946fad8f66SThierry Reding 	value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
1495a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
14966fad8f66SThierry Reding 
14976fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
14986fad8f66SThierry Reding 	value |= SOR_CRC_CNTRL_ENABLE;
14996fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
15006fad8f66SThierry Reding 
15016fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_TEST);
15026fad8f66SThierry Reding 	value &= ~SOR_TEST_CRC_POST_SERIALIZE;
15036fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_TEST);
15046fad8f66SThierry Reding 
15056fad8f66SThierry Reding 	err = tegra_sor_crc_wait(sor, 100);
15066fad8f66SThierry Reding 	if (err < 0)
15076fad8f66SThierry Reding 		goto unlock;
15086fad8f66SThierry Reding 
1509a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, SOR_CRCA_RESET, SOR_CRCA);
1510a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_CRCB);
15116fad8f66SThierry Reding 
1512530239a8SThierry Reding 	seq_printf(s, "%08x\n", value);
15136fad8f66SThierry Reding 
15146fad8f66SThierry Reding unlock:
1515850bab44SThierry Reding 	drm_modeset_unlock_all(drm);
15166fad8f66SThierry Reding 	return err;
15176fad8f66SThierry Reding }
15186fad8f66SThierry Reding 
1519062f5b2cSThierry Reding #define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
1520062f5b2cSThierry Reding 
1521062f5b2cSThierry Reding static const struct debugfs_reg32 tegra_sor_regs[] = {
1522062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CTXSW),
1523062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SUPER_STATE0),
1524062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SUPER_STATE1),
1525062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_STATE0),
1526062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_STATE1),
1527062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE0(0)),
1528062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE0(1)),
1529062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE1(0)),
1530062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE1(1)),
1531062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE2(0)),
1532062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE2(1)),
1533062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE3(0)),
1534062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE3(1)),
1535062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE4(0)),
1536062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE4(1)),
1537062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE5(0)),
1538062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE5(1)),
1539062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CRC_CNTRL),
1540062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_DEBUG_MVID),
1541062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CLK_CNTRL),
1542062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CAP),
1543062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PWR),
1544062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_TEST),
1545062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL0),
1546062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL1),
1547062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL2),
1548062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL3),
1549062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CSTM),
1550062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LVDS),
1551062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CRCA),
1552062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CRCB),
1553062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_BLANK),
1554062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_CTL),
1555062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_SEQ_CTL),
1556062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(0)),
1557062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(1)),
1558062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(2)),
1559062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(3)),
1560062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(4)),
1561062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(5)),
1562062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(6)),
1563062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(7)),
1564062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(8)),
1565062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(9)),
1566062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(10)),
1567062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(11)),
1568062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(12)),
1569062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(13)),
1570062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(14)),
1571062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(15)),
1572062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PWM_DIV),
1573062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PWM_CTL),
1574062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_A0),
1575062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_A1),
1576062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_B0),
1577062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_B1),
1578062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_A0),
1579062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_A1),
1580062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_B0),
1581062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_B1),
1582062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_A0),
1583062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_A1),
1584062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_B0),
1585062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_B1),
1586062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_A0),
1587062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_A1),
1588062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_B0),
1589062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_B1),
1590062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_A0),
1591062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_A1),
1592062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_B0),
1593062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_B1),
1594062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_TRIG),
1595062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_MSCHECK),
1596062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_XBAR_CTRL),
1597062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_XBAR_POL),
1598062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LINKCTL0),
1599062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LINKCTL1),
1600062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT0),
1601062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT1),
1602062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT0),
1603062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT1),
1604062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_PREEMPHASIS0),
1605062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_PREEMPHASIS1),
1606062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS0),
1607062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS1),
1608062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_POSTCURSOR0),
1609062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_POSTCURSOR1),
1610062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_CONFIG0),
1611062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_CONFIG1),
1612062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_MN0),
1613062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_MN1),
1614062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_PADCTL0),
1615062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_PADCTL1),
1616c57997bcSThierry Reding 	DEBUGFS_REG32(SOR_DP_PADCTL2),
1617062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_DEBUG0),
1618062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_DEBUG1),
1619062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_SPARE0),
1620062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_SPARE1),
1621062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_AUDIO_CTRL),
1622062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_AUDIO_HBLANK_SYMBOLS),
1623062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_AUDIO_VBLANK_SYMBOLS),
1624062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_HEADER),
1625062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK0),
1626062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK1),
1627062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK2),
1628062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK3),
1629062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK4),
1630062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK5),
1631062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK6),
1632062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_TPG),
1633062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_TPG_CONFIG),
1634062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LQ_CSTM0),
1635062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LQ_CSTM1),
1636062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LQ_CSTM2),
1637062f5b2cSThierry Reding };
1638062f5b2cSThierry Reding 
1639dab16336SThierry Reding static int tegra_sor_show_regs(struct seq_file *s, void *data)
1640dab16336SThierry Reding {
1641dab16336SThierry Reding 	struct drm_info_node *node = s->private;
1642dab16336SThierry Reding 	struct tegra_sor *sor = node->info_ent->data;
1643850bab44SThierry Reding 	struct drm_crtc *crtc = sor->output.encoder.crtc;
1644850bab44SThierry Reding 	struct drm_device *drm = node->minor->dev;
1645062f5b2cSThierry Reding 	unsigned int i;
1646850bab44SThierry Reding 	int err = 0;
1647850bab44SThierry Reding 
1648850bab44SThierry Reding 	drm_modeset_lock_all(drm);
1649850bab44SThierry Reding 
1650850bab44SThierry Reding 	if (!crtc || !crtc->state->active) {
1651850bab44SThierry Reding 		err = -EBUSY;
1652850bab44SThierry Reding 		goto unlock;
1653850bab44SThierry Reding 	}
1654dab16336SThierry Reding 
1655062f5b2cSThierry Reding 	for (i = 0; i < ARRAY_SIZE(tegra_sor_regs); i++) {
1656062f5b2cSThierry Reding 		unsigned int offset = tegra_sor_regs[i].offset;
1657dab16336SThierry Reding 
1658062f5b2cSThierry Reding 		seq_printf(s, "%-38s %#05x %08x\n", tegra_sor_regs[i].name,
1659062f5b2cSThierry Reding 			   offset, tegra_sor_readl(sor, offset));
1660062f5b2cSThierry Reding 	}
1661dab16336SThierry Reding 
1662850bab44SThierry Reding unlock:
1663850bab44SThierry Reding 	drm_modeset_unlock_all(drm);
1664850bab44SThierry Reding 	return err;
1665dab16336SThierry Reding }
1666dab16336SThierry Reding 
1667dab16336SThierry Reding static const struct drm_info_list debugfs_files[] = {
1668530239a8SThierry Reding 	{ "crc", tegra_sor_show_crc, 0, NULL },
1669dab16336SThierry Reding 	{ "regs", tegra_sor_show_regs, 0, NULL },
1670dab16336SThierry Reding };
1671dab16336SThierry Reding 
16725b8e043bSThierry Reding static int tegra_sor_late_register(struct drm_connector *connector)
16736fad8f66SThierry Reding {
16745b8e043bSThierry Reding 	struct tegra_output *output = connector_to_output(connector);
16755b8e043bSThierry Reding 	unsigned int i, count = ARRAY_SIZE(debugfs_files);
16765b8e043bSThierry Reding 	struct drm_minor *minor = connector->dev->primary;
16775b8e043bSThierry Reding 	struct dentry *root = connector->debugfs_entry;
16785b8e043bSThierry Reding 	struct tegra_sor *sor = to_sor(output);
1679530239a8SThierry Reding 	int err;
16806fad8f66SThierry Reding 
1681dab16336SThierry Reding 	sor->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1682dab16336SThierry Reding 				     GFP_KERNEL);
16835b8e043bSThierry Reding 	if (!sor->debugfs_files)
16845b8e043bSThierry Reding 		return -ENOMEM;
16856fad8f66SThierry Reding 
16865b8e043bSThierry Reding 	for (i = 0; i < count; i++)
1687dab16336SThierry Reding 		sor->debugfs_files[i].data = sor;
1688dab16336SThierry Reding 
16895b8e043bSThierry Reding 	err = drm_debugfs_create_files(sor->debugfs_files, count, root, minor);
1690dab16336SThierry Reding 	if (err < 0)
1691dab16336SThierry Reding 		goto free;
1692dab16336SThierry Reding 
1693530239a8SThierry Reding 	return 0;
16946fad8f66SThierry Reding 
1695dab16336SThierry Reding free:
1696dab16336SThierry Reding 	kfree(sor->debugfs_files);
1697dab16336SThierry Reding 	sor->debugfs_files = NULL;
16985b8e043bSThierry Reding 
16996fad8f66SThierry Reding 	return err;
17006fad8f66SThierry Reding }
17016fad8f66SThierry Reding 
17025b8e043bSThierry Reding static void tegra_sor_early_unregister(struct drm_connector *connector)
17036fad8f66SThierry Reding {
17045b8e043bSThierry Reding 	struct tegra_output *output = connector_to_output(connector);
17055b8e043bSThierry Reding 	unsigned int count = ARRAY_SIZE(debugfs_files);
17065b8e043bSThierry Reding 	struct tegra_sor *sor = to_sor(output);
1707d92e6009SThierry Reding 
17085b8e043bSThierry Reding 	drm_debugfs_remove_files(sor->debugfs_files, count,
17095b8e043bSThierry Reding 				 connector->dev->primary);
1710dab16336SThierry Reding 	kfree(sor->debugfs_files);
1711066d30f8SThierry Reding 	sor->debugfs_files = NULL;
17126fad8f66SThierry Reding }
17136fad8f66SThierry Reding 
1714c31efa7aSThierry Reding static void tegra_sor_connector_reset(struct drm_connector *connector)
1715c31efa7aSThierry Reding {
1716c31efa7aSThierry Reding 	struct tegra_sor_state *state;
1717c31efa7aSThierry Reding 
1718c31efa7aSThierry Reding 	state = kzalloc(sizeof(*state), GFP_KERNEL);
1719c31efa7aSThierry Reding 	if (!state)
1720c31efa7aSThierry Reding 		return;
1721c31efa7aSThierry Reding 
1722c31efa7aSThierry Reding 	if (connector->state) {
1723c31efa7aSThierry Reding 		__drm_atomic_helper_connector_destroy_state(connector->state);
1724c31efa7aSThierry Reding 		kfree(connector->state);
1725c31efa7aSThierry Reding 	}
1726c31efa7aSThierry Reding 
1727c31efa7aSThierry Reding 	__drm_atomic_helper_connector_reset(connector, &state->base);
1728c31efa7aSThierry Reding }
1729c31efa7aSThierry Reding 
17306fad8f66SThierry Reding static enum drm_connector_status
17316fad8f66SThierry Reding tegra_sor_connector_detect(struct drm_connector *connector, bool force)
17326fad8f66SThierry Reding {
17336fad8f66SThierry Reding 	struct tegra_output *output = connector_to_output(connector);
17346fad8f66SThierry Reding 	struct tegra_sor *sor = to_sor(output);
17356fad8f66SThierry Reding 
17369542c237SThierry Reding 	if (sor->aux)
17379542c237SThierry Reding 		return drm_dp_aux_detect(sor->aux);
17386fad8f66SThierry Reding 
1739459cc2c6SThierry Reding 	return tegra_output_connector_detect(connector, force);
17406fad8f66SThierry Reding }
17416fad8f66SThierry Reding 
1742c31efa7aSThierry Reding static struct drm_connector_state *
1743c31efa7aSThierry Reding tegra_sor_connector_duplicate_state(struct drm_connector *connector)
1744c31efa7aSThierry Reding {
1745c31efa7aSThierry Reding 	struct tegra_sor_state *state = to_sor_state(connector->state);
1746c31efa7aSThierry Reding 	struct tegra_sor_state *copy;
1747c31efa7aSThierry Reding 
1748c31efa7aSThierry Reding 	copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
1749c31efa7aSThierry Reding 	if (!copy)
1750c31efa7aSThierry Reding 		return NULL;
1751c31efa7aSThierry Reding 
1752c31efa7aSThierry Reding 	__drm_atomic_helper_connector_duplicate_state(connector, &copy->base);
1753c31efa7aSThierry Reding 
1754c31efa7aSThierry Reding 	return &copy->base;
1755c31efa7aSThierry Reding }
1756c31efa7aSThierry Reding 
17576fad8f66SThierry Reding static const struct drm_connector_funcs tegra_sor_connector_funcs = {
1758c31efa7aSThierry Reding 	.reset = tegra_sor_connector_reset,
17596fad8f66SThierry Reding 	.detect = tegra_sor_connector_detect,
17606fad8f66SThierry Reding 	.fill_modes = drm_helper_probe_single_connector_modes,
17616fad8f66SThierry Reding 	.destroy = tegra_output_connector_destroy,
1762c31efa7aSThierry Reding 	.atomic_duplicate_state = tegra_sor_connector_duplicate_state,
17634aa3df71SThierry Reding 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
17645b8e043bSThierry Reding 	.late_register = tegra_sor_late_register,
17655b8e043bSThierry Reding 	.early_unregister = tegra_sor_early_unregister,
17666fad8f66SThierry Reding };
17676fad8f66SThierry Reding 
17686fad8f66SThierry Reding static int tegra_sor_connector_get_modes(struct drm_connector *connector)
17696fad8f66SThierry Reding {
17706fad8f66SThierry Reding 	struct tegra_output *output = connector_to_output(connector);
17716fad8f66SThierry Reding 	struct tegra_sor *sor = to_sor(output);
17726fad8f66SThierry Reding 	int err;
17736fad8f66SThierry Reding 
17749542c237SThierry Reding 	if (sor->aux)
17759542c237SThierry Reding 		drm_dp_aux_enable(sor->aux);
17766fad8f66SThierry Reding 
17776fad8f66SThierry Reding 	err = tegra_output_connector_get_modes(connector);
17786fad8f66SThierry Reding 
17799542c237SThierry Reding 	if (sor->aux)
17809542c237SThierry Reding 		drm_dp_aux_disable(sor->aux);
17816fad8f66SThierry Reding 
17826fad8f66SThierry Reding 	return err;
17836fad8f66SThierry Reding }
17846fad8f66SThierry Reding 
17856fad8f66SThierry Reding static enum drm_mode_status
17866fad8f66SThierry Reding tegra_sor_connector_mode_valid(struct drm_connector *connector,
17876fad8f66SThierry Reding 			       struct drm_display_mode *mode)
17886fad8f66SThierry Reding {
17896fad8f66SThierry Reding 	return MODE_OK;
17906fad8f66SThierry Reding }
17916fad8f66SThierry Reding 
17926fad8f66SThierry Reding static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
17936fad8f66SThierry Reding 	.get_modes = tegra_sor_connector_get_modes,
17946fad8f66SThierry Reding 	.mode_valid = tegra_sor_connector_mode_valid,
17956fad8f66SThierry Reding };
17966fad8f66SThierry Reding 
17976fad8f66SThierry Reding static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
17986fad8f66SThierry Reding 	.destroy = tegra_output_encoder_destroy,
17996fad8f66SThierry Reding };
18006fad8f66SThierry Reding 
1801850bab44SThierry Reding static void tegra_sor_edp_disable(struct drm_encoder *encoder)
18026fad8f66SThierry Reding {
1803850bab44SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
1804850bab44SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1805850bab44SThierry Reding 	struct tegra_sor *sor = to_sor(output);
1806850bab44SThierry Reding 	u32 value;
1807850bab44SThierry Reding 	int err;
1808850bab44SThierry Reding 
1809850bab44SThierry Reding 	if (output->panel)
1810850bab44SThierry Reding 		drm_panel_disable(output->panel);
1811850bab44SThierry Reding 
1812850bab44SThierry Reding 	err = tegra_sor_detach(sor);
1813850bab44SThierry Reding 	if (err < 0)
1814850bab44SThierry Reding 		dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1815850bab44SThierry Reding 
1816850bab44SThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE1);
1817850bab44SThierry Reding 	tegra_sor_update(sor);
1818850bab44SThierry Reding 
1819850bab44SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1820c57997bcSThierry Reding 	value &= ~SOR_ENABLE(0);
1821850bab44SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1822850bab44SThierry Reding 
1823850bab44SThierry Reding 	tegra_dc_commit(dc);
18246fad8f66SThierry Reding 
1825850bab44SThierry Reding 	err = tegra_sor_power_down(sor);
1826850bab44SThierry Reding 	if (err < 0)
1827850bab44SThierry Reding 		dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1828850bab44SThierry Reding 
18299542c237SThierry Reding 	if (sor->aux) {
18309542c237SThierry Reding 		err = drm_dp_aux_disable(sor->aux);
1831850bab44SThierry Reding 		if (err < 0)
1832850bab44SThierry Reding 			dev_err(sor->dev, "failed to disable DP: %d\n", err);
18336fad8f66SThierry Reding 	}
18346fad8f66SThierry Reding 
1835c57997bcSThierry Reding 	err = tegra_io_pad_power_disable(sor->pad);
1836850bab44SThierry Reding 	if (err < 0)
1837c57997bcSThierry Reding 		dev_err(sor->dev, "failed to power off I/O pad: %d\n", err);
1838850bab44SThierry Reding 
1839850bab44SThierry Reding 	if (output->panel)
1840850bab44SThierry Reding 		drm_panel_unprepare(output->panel);
1841850bab44SThierry Reding 
1842aaff8bd2SThierry Reding 	pm_runtime_put(sor->dev);
18436fad8f66SThierry Reding }
18446fad8f66SThierry Reding 
1845459cc2c6SThierry Reding #if 0
1846459cc2c6SThierry Reding static int calc_h_ref_to_sync(const struct drm_display_mode *mode,
1847459cc2c6SThierry Reding 			      unsigned int *value)
1848459cc2c6SThierry Reding {
1849459cc2c6SThierry Reding 	unsigned int hfp, hsw, hbp, a = 0, b;
1850459cc2c6SThierry Reding 
1851459cc2c6SThierry Reding 	hfp = mode->hsync_start - mode->hdisplay;
1852459cc2c6SThierry Reding 	hsw = mode->hsync_end - mode->hsync_start;
1853459cc2c6SThierry Reding 	hbp = mode->htotal - mode->hsync_end;
1854459cc2c6SThierry Reding 
1855459cc2c6SThierry Reding 	pr_info("hfp: %u, hsw: %u, hbp: %u\n", hfp, hsw, hbp);
1856459cc2c6SThierry Reding 
1857459cc2c6SThierry Reding 	b = hfp - 1;
1858459cc2c6SThierry Reding 
1859459cc2c6SThierry Reding 	pr_info("a: %u, b: %u\n", a, b);
1860459cc2c6SThierry Reding 	pr_info("a + hsw + hbp = %u\n", a + hsw + hbp);
1861459cc2c6SThierry Reding 
1862459cc2c6SThierry Reding 	if (a + hsw + hbp <= 11) {
1863459cc2c6SThierry Reding 		a = 1 + 11 - hsw - hbp;
1864459cc2c6SThierry Reding 		pr_info("a: %u\n", a);
1865459cc2c6SThierry Reding 	}
1866459cc2c6SThierry Reding 
1867459cc2c6SThierry Reding 	if (a > b)
1868459cc2c6SThierry Reding 		return -EINVAL;
1869459cc2c6SThierry Reding 
1870459cc2c6SThierry Reding 	if (hsw < 1)
1871459cc2c6SThierry Reding 		return -EINVAL;
1872459cc2c6SThierry Reding 
1873459cc2c6SThierry Reding 	if (mode->hdisplay < 16)
1874459cc2c6SThierry Reding 		return -EINVAL;
1875459cc2c6SThierry Reding 
1876459cc2c6SThierry Reding 	if (value) {
1877459cc2c6SThierry Reding 		if (b > a && a % 2)
1878459cc2c6SThierry Reding 			*value = a + 1;
1879459cc2c6SThierry Reding 		else
1880459cc2c6SThierry Reding 			*value = a;
1881459cc2c6SThierry Reding 	}
1882459cc2c6SThierry Reding 
1883459cc2c6SThierry Reding 	return 0;
1884459cc2c6SThierry Reding }
1885459cc2c6SThierry Reding #endif
1886459cc2c6SThierry Reding 
1887850bab44SThierry Reding static void tegra_sor_edp_enable(struct drm_encoder *encoder)
18886fad8f66SThierry Reding {
18896fad8f66SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
18906fad8f66SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
18916b6b6042SThierry Reding 	struct tegra_sor *sor = to_sor(output);
189234fa183bSThierry Reding 	struct tegra_sor_config config;
1893c31efa7aSThierry Reding 	struct tegra_sor_state *state;
1894c1763937SThierry Reding 	struct drm_display_mode *mode;
1895c1763937SThierry Reding 	struct drm_display_info *info;
18962bd1dd39SThierry Reding 	unsigned int i;
189728fe2076SThierry Reding 	u32 value;
1898c1763937SThierry Reding 	int err;
189986f5c52dSThierry Reding 
1900c31efa7aSThierry Reding 	state = to_sor_state(output->connector.state);
1901c1763937SThierry Reding 	mode = &encoder->crtc->state->adjusted_mode;
1902c1763937SThierry Reding 	info = &output->connector.display_info;
19036b6b6042SThierry Reding 
1904aaff8bd2SThierry Reding 	pm_runtime_get_sync(sor->dev);
19056b6b6042SThierry Reding 
190625bb2cecSThierry Reding 	/* switch to safe parent clock */
190725bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
19086b6b6042SThierry Reding 	if (err < 0)
19096b6b6042SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
19106b6b6042SThierry Reding 
191138b445bcSThierry Reding 	err = tegra_io_rail_power_on(TEGRA_IO_RAIL_LVDS);
191238b445bcSThierry Reding 	if (err < 0)
191338b445bcSThierry Reding 		dev_err(sor->dev, "failed to power on LVDS rail: %d\n", err);
191438b445bcSThierry Reding 
191538b445bcSThierry Reding 	usleep_range(20, 100);
191638b445bcSThierry Reding 
191738b445bcSThierry Reding 	err = drm_dp_aux_enable(sor->aux);
191838b445bcSThierry Reding 	if (err < 0)
191938b445bcSThierry Reding 		dev_err(sor->dev, "failed to enable DPAUX: %d\n", err);
192038b445bcSThierry Reding 
192138b445bcSThierry Reding 	err = drm_dp_link_probe(sor->aux, &sor->link);
192238b445bcSThierry Reding 	if (err < 0)
192338b445bcSThierry Reding 		dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
192438b445bcSThierry Reding 
1925c9533131SThierry Reding 	tegra_sor_filter_rates(sor);
1926c9533131SThierry Reding 
192738b445bcSThierry Reding 	err = drm_dp_link_choose(&sor->link, mode, info);
192838b445bcSThierry Reding 	if (err < 0)
192938b445bcSThierry Reding 		dev_err(sor->dev, "failed to choose link: %d\n", err);
193038b445bcSThierry Reding 
193138b445bcSThierry Reding 	if (output->panel)
193238b445bcSThierry Reding 		drm_panel_prepare(output->panel);
19336b6b6042SThierry Reding 
1934880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1935a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1936880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
193738b445bcSThierry Reding 
193838b445bcSThierry Reding 	usleep_range(20, 40);
19396b6b6042SThierry Reding 
1940880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll3);
1941a9a9e4fdSThierry Reding 	value |= SOR_PLL3_PLL_VDD_MODE_3V3;
1942880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll3);
19436b6b6042SThierry Reding 
194438b445bcSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
194538b445bcSThierry Reding 	value &= ~(SOR_PLL0_VCOPD | SOR_PLL0_PWR);
1946880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
19476b6b6042SThierry Reding 
1948880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1949a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
195038b445bcSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD;
1951880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
19526b6b6042SThierry Reding 
195338b445bcSThierry Reding 	usleep_range(200, 400);
19546b6b6042SThierry Reding 
1955880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1956a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
1957a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
1958880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
19596b6b6042SThierry Reding 
19606b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
196138b445bcSThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
196238b445bcSThierry Reding 	value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
19636b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
19646b6b6042SThierry Reding 
196538b445bcSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
196638b445bcSThierry Reding 	/* XXX not in TRM */
196738b445bcSThierry Reding 	value |= SOR_DP_SPARE_PANEL_INTERNAL;
196838b445bcSThierry Reding 	value |= SOR_DP_SPARE_SEQ_ENABLE;
196938b445bcSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
197038b445bcSThierry Reding 
197138b445bcSThierry Reding 	/* XXX not in TRM */
197238b445bcSThierry Reding 	tegra_sor_writel(sor, 0, SOR_LVDS);
19736b6b6042SThierry Reding 
1974880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
197538b445bcSThierry Reding 	value &= ~SOR_PLL0_ICHPMP_MASK;
197638b445bcSThierry Reding 	value &= ~SOR_PLL0_VCOCAP_MASK;
197738b445bcSThierry Reding 	value |= SOR_PLL0_ICHPMP(0x1);
197838b445bcSThierry Reding 	value |= SOR_PLL0_VCOCAP(0x3);
197938b445bcSThierry Reding 	value |= SOR_PLL0_RESISTOR_EXT;
1980880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
19816b6b6042SThierry Reding 
198230b49435SThierry Reding 	/* XXX not in TRM */
198330b49435SThierry Reding 	for (value = 0, i = 0; i < 5; i++)
19846d6c815dSThierry Reding 		value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->xbar_cfg[i]) |
198530b49435SThierry Reding 			 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
198630b49435SThierry Reding 
198730b49435SThierry Reding 	tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
198830b49435SThierry Reding 	tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
198930b49435SThierry Reding 
199025bb2cecSThierry Reding 	/* switch to DP parent clock */
199125bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_dp);
19926b6b6042SThierry Reding 	if (err < 0)
199325bb2cecSThierry Reding 		dev_err(sor->dev, "failed to set parent clock: %d\n", err);
19946b6b6042SThierry Reding 
1995c1763937SThierry Reding 	/* use DP-A protocol */
1996c1763937SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
1997c1763937SThierry Reding 	value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
1998c1763937SThierry Reding 	value |= SOR_STATE_ASY_PROTOCOL_DP_A;
1999c1763937SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
2000899451b7SThierry Reding 
2001c1763937SThierry Reding 	/* enable port */
2002a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
20036b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_ENABLE;
2004a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
20056b6b6042SThierry Reding 
2006c1763937SThierry Reding 	tegra_sor_dp_term_calibrate(sor);
2007c1763937SThierry Reding 
2008c1763937SThierry Reding 	err = drm_dp_link_train(&sor->link);
2009c1763937SThierry Reding 	if (err < 0)
2010c1763937SThierry Reding 		dev_err(sor->dev, "link training failed: %d\n", err);
2011c1763937SThierry Reding 	else
2012c1763937SThierry Reding 		dev_dbg(sor->dev, "link training succeeded\n");
2013c1763937SThierry Reding 
2014c1763937SThierry Reding 	err = drm_dp_link_power_up(sor->aux, &sor->link);
201538b445bcSThierry Reding 	if (err < 0)
201638b445bcSThierry Reding 		dev_err(sor->dev, "failed to power up eDP link: %d\n", err);
20176b6b6042SThierry Reding 
2018c1763937SThierry Reding 	/* compute configuration */
2019c1763937SThierry Reding 	memset(&config, 0, sizeof(config));
2020c1763937SThierry Reding 	config.bits_per_pixel = state->bpc * 3;
20216b6b6042SThierry Reding 
2022c1763937SThierry Reding 	err = tegra_sor_compute_config(sor, mode, &config, &sor->link);
202338b445bcSThierry Reding 	if (err < 0)
2024c1763937SThierry Reding 		dev_err(sor->dev, "failed to compute configuration: %d\n", err);
20256b6b6042SThierry Reding 
2026c1763937SThierry Reding 	tegra_sor_apply_config(sor, &config);
20276b6b6042SThierry Reding 
20286b6b6042SThierry Reding 	err = tegra_sor_power_up(sor, 250);
2029850bab44SThierry Reding 	if (err < 0)
20306b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
20316b6b6042SThierry Reding 
20326b6b6042SThierry Reding 	/* CSTM (LVDS, link A/B, upper) */
2033143b1df2SStéphane Marchesin 	value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
20346b6b6042SThierry Reding 		SOR_CSTM_UPPER;
20356b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CSTM);
20366b6b6042SThierry Reding 
20372bd1dd39SThierry Reding 	/* use DP-A protocol */
20382bd1dd39SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
20392bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
20402bd1dd39SThierry Reding 	value |= SOR_STATE_ASY_PROTOCOL_DP_A;
20412bd1dd39SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
20422bd1dd39SThierry Reding 
2043c31efa7aSThierry Reding 	tegra_sor_mode_set(sor, mode, state);
20442bd1dd39SThierry Reding 
20456b6b6042SThierry Reding 	/* PWM setup */
20466b6b6042SThierry Reding 	err = tegra_sor_setup_pwm(sor, 250);
2047850bab44SThierry Reding 	if (err < 0)
20486b6b6042SThierry Reding 		dev_err(sor->dev, "failed to setup PWM: %d\n", err);
20496b6b6042SThierry Reding 
2050666cb873SThierry Reding 	tegra_sor_update(sor);
2051666cb873SThierry Reding 
205238b445bcSThierry Reding 	err = tegra_sor_power_up(sor, 250);
205338b445bcSThierry Reding 	if (err < 0)
205438b445bcSThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
205538b445bcSThierry Reding 
205638b445bcSThierry Reding 	/* attach and wake up */
205738b445bcSThierry Reding 	err = tegra_sor_attach(sor);
205838b445bcSThierry Reding 	if (err < 0)
205938b445bcSThierry Reding 		dev_err(sor->dev, "failed to attach SOR: %d\n", err);
206038b445bcSThierry Reding 
20616b6b6042SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
2062c57997bcSThierry Reding 	value |= SOR_ENABLE(0);
20636b6b6042SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
20646b6b6042SThierry Reding 
2065666cb873SThierry Reding 	tegra_dc_commit(dc);
20666b6b6042SThierry Reding 
20676b6b6042SThierry Reding 	err = tegra_sor_wakeup(sor);
2068850bab44SThierry Reding 	if (err < 0)
206938b445bcSThierry Reding 		dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
20706b6b6042SThierry Reding 
20716fad8f66SThierry Reding 	if (output->panel)
20726fad8f66SThierry Reding 		drm_panel_enable(output->panel);
20736b6b6042SThierry Reding }
20746b6b6042SThierry Reding 
207582f1511cSThierry Reding static int
207682f1511cSThierry Reding tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
207782f1511cSThierry Reding 			       struct drm_crtc_state *crtc_state,
207882f1511cSThierry Reding 			       struct drm_connector_state *conn_state)
207982f1511cSThierry Reding {
208082f1511cSThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
2081c31efa7aSThierry Reding 	struct tegra_sor_state *state = to_sor_state(conn_state);
208282f1511cSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
208382f1511cSThierry Reding 	unsigned long pclk = crtc_state->mode.clock * 1000;
208482f1511cSThierry Reding 	struct tegra_sor *sor = to_sor(output);
2085c31efa7aSThierry Reding 	struct drm_display_info *info;
208682f1511cSThierry Reding 	int err;
208782f1511cSThierry Reding 
2088c31efa7aSThierry Reding 	info = &output->connector.display_info;
2089c31efa7aSThierry Reding 
209036e90221SThierry Reding 	/*
209136e90221SThierry Reding 	 * For HBR2 modes, the SOR brick needs to use the x20 multiplier, so
209236e90221SThierry Reding 	 * the pixel clock must be corrected accordingly.
209336e90221SThierry Reding 	 */
209436e90221SThierry Reding 	if (pclk >= 340000000) {
209536e90221SThierry Reding 		state->link_speed = 20;
209636e90221SThierry Reding 		state->pclk = pclk / 2;
209736e90221SThierry Reding 	} else {
209836e90221SThierry Reding 		state->link_speed = 10;
209936e90221SThierry Reding 		state->pclk = pclk;
210036e90221SThierry Reding 	}
210136e90221SThierry Reding 
210282f1511cSThierry Reding 	err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
210382f1511cSThierry Reding 					 pclk, 0);
210482f1511cSThierry Reding 	if (err < 0) {
210582f1511cSThierry Reding 		dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
210682f1511cSThierry Reding 		return err;
210782f1511cSThierry Reding 	}
210882f1511cSThierry Reding 
2109c31efa7aSThierry Reding 	switch (info->bpc) {
2110c31efa7aSThierry Reding 	case 8:
2111c31efa7aSThierry Reding 	case 6:
2112c31efa7aSThierry Reding 		state->bpc = info->bpc;
2113c31efa7aSThierry Reding 		break;
2114c31efa7aSThierry Reding 
2115c31efa7aSThierry Reding 	default:
2116c31efa7aSThierry Reding 		DRM_DEBUG_KMS("%u bits-per-color not supported\n", info->bpc);
2117c31efa7aSThierry Reding 		state->bpc = 8;
2118c31efa7aSThierry Reding 		break;
2119c31efa7aSThierry Reding 	}
2120c31efa7aSThierry Reding 
212182f1511cSThierry Reding 	return 0;
212282f1511cSThierry Reding }
212382f1511cSThierry Reding 
2124459cc2c6SThierry Reding static const struct drm_encoder_helper_funcs tegra_sor_edp_helpers = {
2125850bab44SThierry Reding 	.disable = tegra_sor_edp_disable,
2126850bab44SThierry Reding 	.enable = tegra_sor_edp_enable,
212782f1511cSThierry Reding 	.atomic_check = tegra_sor_encoder_atomic_check,
21286b6b6042SThierry Reding };
21296b6b6042SThierry Reding 
2130459cc2c6SThierry Reding static inline u32 tegra_sor_hdmi_subpack(const u8 *ptr, size_t size)
2131459cc2c6SThierry Reding {
2132459cc2c6SThierry Reding 	u32 value = 0;
2133459cc2c6SThierry Reding 	size_t i;
2134459cc2c6SThierry Reding 
2135459cc2c6SThierry Reding 	for (i = size; i > 0; i--)
2136459cc2c6SThierry Reding 		value = (value << 8) | ptr[i - 1];
2137459cc2c6SThierry Reding 
2138459cc2c6SThierry Reding 	return value;
2139459cc2c6SThierry Reding }
2140459cc2c6SThierry Reding 
2141459cc2c6SThierry Reding static void tegra_sor_hdmi_write_infopack(struct tegra_sor *sor,
2142459cc2c6SThierry Reding 					  const void *data, size_t size)
2143459cc2c6SThierry Reding {
2144459cc2c6SThierry Reding 	const u8 *ptr = data;
2145459cc2c6SThierry Reding 	unsigned long offset;
2146459cc2c6SThierry Reding 	size_t i, j;
2147459cc2c6SThierry Reding 	u32 value;
2148459cc2c6SThierry Reding 
2149459cc2c6SThierry Reding 	switch (ptr[0]) {
2150459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_AVI:
2151459cc2c6SThierry Reding 		offset = SOR_HDMI_AVI_INFOFRAME_HEADER;
2152459cc2c6SThierry Reding 		break;
2153459cc2c6SThierry Reding 
2154459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_AUDIO:
2155459cc2c6SThierry Reding 		offset = SOR_HDMI_AUDIO_INFOFRAME_HEADER;
2156459cc2c6SThierry Reding 		break;
2157459cc2c6SThierry Reding 
2158459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_VENDOR:
2159459cc2c6SThierry Reding 		offset = SOR_HDMI_VSI_INFOFRAME_HEADER;
2160459cc2c6SThierry Reding 		break;
2161459cc2c6SThierry Reding 
2162459cc2c6SThierry Reding 	default:
2163459cc2c6SThierry Reding 		dev_err(sor->dev, "unsupported infoframe type: %02x\n",
2164459cc2c6SThierry Reding 			ptr[0]);
2165459cc2c6SThierry Reding 		return;
2166459cc2c6SThierry Reding 	}
2167459cc2c6SThierry Reding 
2168459cc2c6SThierry Reding 	value = INFOFRAME_HEADER_TYPE(ptr[0]) |
2169459cc2c6SThierry Reding 		INFOFRAME_HEADER_VERSION(ptr[1]) |
2170459cc2c6SThierry Reding 		INFOFRAME_HEADER_LEN(ptr[2]);
2171459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, offset);
2172459cc2c6SThierry Reding 	offset++;
2173459cc2c6SThierry Reding 
2174459cc2c6SThierry Reding 	/*
2175459cc2c6SThierry Reding 	 * Each subpack contains 7 bytes, divided into:
2176459cc2c6SThierry Reding 	 * - subpack_low: bytes 0 - 3
2177459cc2c6SThierry Reding 	 * - subpack_high: bytes 4 - 6 (with byte 7 padded to 0x00)
2178459cc2c6SThierry Reding 	 */
2179459cc2c6SThierry Reding 	for (i = 3, j = 0; i < size; i += 7, j += 8) {
2180459cc2c6SThierry Reding 		size_t rem = size - i, num = min_t(size_t, rem, 4);
2181459cc2c6SThierry Reding 
2182459cc2c6SThierry Reding 		value = tegra_sor_hdmi_subpack(&ptr[i], num);
2183459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, offset++);
2184459cc2c6SThierry Reding 
2185459cc2c6SThierry Reding 		num = min_t(size_t, rem - num, 3);
2186459cc2c6SThierry Reding 
2187459cc2c6SThierry Reding 		value = tegra_sor_hdmi_subpack(&ptr[i + 4], num);
2188459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, offset++);
2189459cc2c6SThierry Reding 	}
2190459cc2c6SThierry Reding }
2191459cc2c6SThierry Reding 
2192459cc2c6SThierry Reding static int
2193459cc2c6SThierry Reding tegra_sor_hdmi_setup_avi_infoframe(struct tegra_sor *sor,
2194459cc2c6SThierry Reding 				   const struct drm_display_mode *mode)
2195459cc2c6SThierry Reding {
2196459cc2c6SThierry Reding 	u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
2197459cc2c6SThierry Reding 	struct hdmi_avi_infoframe frame;
2198459cc2c6SThierry Reding 	u32 value;
2199459cc2c6SThierry Reding 	int err;
2200459cc2c6SThierry Reding 
2201459cc2c6SThierry Reding 	/* disable AVI infoframe */
2202459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
2203459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_SINGLE;
2204459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_OTHER;
2205459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_ENABLE;
2206459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
2207459cc2c6SThierry Reding 
220813d0add3SVille Syrjälä 	err = drm_hdmi_avi_infoframe_from_display_mode(&frame,
220913d0add3SVille Syrjälä 						       &sor->output.connector, mode);
2210459cc2c6SThierry Reding 	if (err < 0) {
2211459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
2212459cc2c6SThierry Reding 		return err;
2213459cc2c6SThierry Reding 	}
2214459cc2c6SThierry Reding 
2215459cc2c6SThierry Reding 	err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
2216459cc2c6SThierry Reding 	if (err < 0) {
2217459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to pack AVI infoframe: %d\n", err);
2218459cc2c6SThierry Reding 		return err;
2219459cc2c6SThierry Reding 	}
2220459cc2c6SThierry Reding 
2221459cc2c6SThierry Reding 	tegra_sor_hdmi_write_infopack(sor, buffer, err);
2222459cc2c6SThierry Reding 
2223459cc2c6SThierry Reding 	/* enable AVI infoframe */
2224459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
2225459cc2c6SThierry Reding 	value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
2226459cc2c6SThierry Reding 	value |= INFOFRAME_CTRL_ENABLE;
2227459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
2228459cc2c6SThierry Reding 
2229459cc2c6SThierry Reding 	return 0;
2230459cc2c6SThierry Reding }
2231459cc2c6SThierry Reding 
22328e2988a7SThierry Reding static void tegra_sor_write_eld(struct tegra_sor *sor)
22338e2988a7SThierry Reding {
22348e2988a7SThierry Reding 	size_t length = drm_eld_size(sor->output.connector.eld), i;
22358e2988a7SThierry Reding 
22368e2988a7SThierry Reding 	for (i = 0; i < length; i++)
22378e2988a7SThierry Reding 		tegra_sor_writel(sor, i << 8 | sor->output.connector.eld[i],
22388e2988a7SThierry Reding 				 SOR_AUDIO_HDA_ELD_BUFWR);
22398e2988a7SThierry Reding 
22408e2988a7SThierry Reding 	/*
22418e2988a7SThierry Reding 	 * The HDA codec will always report an ELD buffer size of 96 bytes and
22428e2988a7SThierry Reding 	 * the HDA codec driver will check that each byte read from the buffer
22438e2988a7SThierry Reding 	 * is valid. Therefore every byte must be written, even if no 96 bytes
22448e2988a7SThierry Reding 	 * were parsed from EDID.
22458e2988a7SThierry Reding 	 */
22468e2988a7SThierry Reding 	for (i = length; i < 96; i++)
22478e2988a7SThierry Reding 		tegra_sor_writel(sor, i << 8 | 0, SOR_AUDIO_HDA_ELD_BUFWR);
22488e2988a7SThierry Reding }
22498e2988a7SThierry Reding 
22508e2988a7SThierry Reding static void tegra_sor_audio_prepare(struct tegra_sor *sor)
22518e2988a7SThierry Reding {
22528e2988a7SThierry Reding 	u32 value;
22538e2988a7SThierry Reding 
2254f1f20eb9SThierry Reding 	/*
2255f1f20eb9SThierry Reding 	 * Enable and unmask the HDA codec SCRATCH0 register interrupt. This
2256f1f20eb9SThierry Reding 	 * is used for interoperability between the HDA codec driver and the
2257f1f20eb9SThierry Reding 	 * HDMI/DP driver.
2258f1f20eb9SThierry Reding 	 */
2259f1f20eb9SThierry Reding 	value = SOR_INT_CODEC_SCRATCH1 | SOR_INT_CODEC_SCRATCH0;
2260f1f20eb9SThierry Reding 	tegra_sor_writel(sor, value, SOR_INT_ENABLE);
2261f1f20eb9SThierry Reding 	tegra_sor_writel(sor, value, SOR_INT_MASK);
2262f1f20eb9SThierry Reding 
22638e2988a7SThierry Reding 	tegra_sor_write_eld(sor);
22648e2988a7SThierry Reding 
22658e2988a7SThierry Reding 	value = SOR_AUDIO_HDA_PRESENSE_ELDV | SOR_AUDIO_HDA_PRESENSE_PD;
22668e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_AUDIO_HDA_PRESENSE);
22678e2988a7SThierry Reding }
22688e2988a7SThierry Reding 
22698e2988a7SThierry Reding static void tegra_sor_audio_unprepare(struct tegra_sor *sor)
22708e2988a7SThierry Reding {
22718e2988a7SThierry Reding 	tegra_sor_writel(sor, 0, SOR_AUDIO_HDA_PRESENSE);
2272f1f20eb9SThierry Reding 	tegra_sor_writel(sor, 0, SOR_INT_MASK);
2273f1f20eb9SThierry Reding 	tegra_sor_writel(sor, 0, SOR_INT_ENABLE);
22748e2988a7SThierry Reding }
22758e2988a7SThierry Reding 
22768e2988a7SThierry Reding static int tegra_sor_hdmi_enable_audio_infoframe(struct tegra_sor *sor)
22778e2988a7SThierry Reding {
22788e2988a7SThierry Reding 	u8 buffer[HDMI_INFOFRAME_SIZE(AUDIO)];
22798e2988a7SThierry Reding 	struct hdmi_audio_infoframe frame;
22808e2988a7SThierry Reding 	u32 value;
22818e2988a7SThierry Reding 	int err;
22828e2988a7SThierry Reding 
22838e2988a7SThierry Reding 	err = hdmi_audio_infoframe_init(&frame);
22848e2988a7SThierry Reding 	if (err < 0) {
22858e2988a7SThierry Reding 		dev_err(sor->dev, "failed to setup audio infoframe: %d\n", err);
22868e2988a7SThierry Reding 		return err;
22878e2988a7SThierry Reding 	}
22888e2988a7SThierry Reding 
2289fad7b806SThierry Reding 	frame.channels = sor->format.channels;
22908e2988a7SThierry Reding 
22918e2988a7SThierry Reding 	err = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
22928e2988a7SThierry Reding 	if (err < 0) {
22938e2988a7SThierry Reding 		dev_err(sor->dev, "failed to pack audio infoframe: %d\n", err);
22948e2988a7SThierry Reding 		return err;
22958e2988a7SThierry Reding 	}
22968e2988a7SThierry Reding 
22978e2988a7SThierry Reding 	tegra_sor_hdmi_write_infopack(sor, buffer, err);
22988e2988a7SThierry Reding 
22998e2988a7SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
23008e2988a7SThierry Reding 	value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
23018e2988a7SThierry Reding 	value |= INFOFRAME_CTRL_ENABLE;
23028e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
23038e2988a7SThierry Reding 
23048e2988a7SThierry Reding 	return 0;
23058e2988a7SThierry Reding }
23068e2988a7SThierry Reding 
23078e2988a7SThierry Reding static void tegra_sor_hdmi_audio_enable(struct tegra_sor *sor)
23088e2988a7SThierry Reding {
23098e2988a7SThierry Reding 	u32 value;
23108e2988a7SThierry Reding 
23118e2988a7SThierry Reding 	value = tegra_sor_readl(sor, SOR_AUDIO_CNTRL);
23128e2988a7SThierry Reding 
23138e2988a7SThierry Reding 	/* select HDA audio input */
23148e2988a7SThierry Reding 	value &= ~SOR_AUDIO_CNTRL_SOURCE_SELECT(SOURCE_SELECT_MASK);
23158e2988a7SThierry Reding 	value |= SOR_AUDIO_CNTRL_SOURCE_SELECT(SOURCE_SELECT_HDA);
23168e2988a7SThierry Reding 
23178e2988a7SThierry Reding 	/* inject null samples */
2318fad7b806SThierry Reding 	if (sor->format.channels != 2)
23198e2988a7SThierry Reding 		value &= ~SOR_AUDIO_CNTRL_INJECT_NULLSMPL;
23208e2988a7SThierry Reding 	else
23218e2988a7SThierry Reding 		value |= SOR_AUDIO_CNTRL_INJECT_NULLSMPL;
23228e2988a7SThierry Reding 
23238e2988a7SThierry Reding 	value |= SOR_AUDIO_CNTRL_AFIFO_FLUSH;
23248e2988a7SThierry Reding 
23258e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_AUDIO_CNTRL);
23268e2988a7SThierry Reding 
23278e2988a7SThierry Reding 	/* enable advertising HBR capability */
23288e2988a7SThierry Reding 	tegra_sor_writel(sor, SOR_AUDIO_SPARE_HBR_ENABLE, SOR_AUDIO_SPARE);
23298e2988a7SThierry Reding 
23308e2988a7SThierry Reding 	tegra_sor_writel(sor, 0, SOR_HDMI_ACR_CTRL);
23318e2988a7SThierry Reding 
23328e2988a7SThierry Reding 	value = SOR_HDMI_SPARE_ACR_PRIORITY_HIGH |
23338e2988a7SThierry Reding 		SOR_HDMI_SPARE_CTS_RESET(1) |
23348e2988a7SThierry Reding 		SOR_HDMI_SPARE_HW_CTS_ENABLE;
23358e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_SPARE);
23368e2988a7SThierry Reding 
23378e2988a7SThierry Reding 	/* enable HW CTS */
23388e2988a7SThierry Reding 	value = SOR_HDMI_ACR_SUBPACK_LOW_SB1(0);
23398e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_ACR_0441_SUBPACK_LOW);
23408e2988a7SThierry Reding 
23418e2988a7SThierry Reding 	/* allow packet to be sent */
23428e2988a7SThierry Reding 	value = SOR_HDMI_ACR_SUBPACK_HIGH_ENABLE;
23438e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_ACR_0441_SUBPACK_HIGH);
23448e2988a7SThierry Reding 
23458e2988a7SThierry Reding 	/* reset N counter and enable lookup */
23468e2988a7SThierry Reding 	value = SOR_HDMI_AUDIO_N_RESET | SOR_HDMI_AUDIO_N_LOOKUP;
23478e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_N);
23488e2988a7SThierry Reding 
2349fad7b806SThierry Reding 	value = (24000 * 4096) / (128 * sor->format.sample_rate / 1000);
23508e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_0320);
23518e2988a7SThierry Reding 	tegra_sor_writel(sor, 4096, SOR_AUDIO_NVAL_0320);
23528e2988a7SThierry Reding 
23538e2988a7SThierry Reding 	tegra_sor_writel(sor, 20000, SOR_AUDIO_AVAL_0441);
23548e2988a7SThierry Reding 	tegra_sor_writel(sor, 4704, SOR_AUDIO_NVAL_0441);
23558e2988a7SThierry Reding 
23568e2988a7SThierry Reding 	tegra_sor_writel(sor, 20000, SOR_AUDIO_AVAL_0882);
23578e2988a7SThierry Reding 	tegra_sor_writel(sor, 9408, SOR_AUDIO_NVAL_0882);
23588e2988a7SThierry Reding 
23598e2988a7SThierry Reding 	tegra_sor_writel(sor, 20000, SOR_AUDIO_AVAL_1764);
23608e2988a7SThierry Reding 	tegra_sor_writel(sor, 18816, SOR_AUDIO_NVAL_1764);
23618e2988a7SThierry Reding 
2362fad7b806SThierry Reding 	value = (24000 * 6144) / (128 * sor->format.sample_rate / 1000);
23638e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_0480);
23648e2988a7SThierry Reding 	tegra_sor_writel(sor, 6144, SOR_AUDIO_NVAL_0480);
23658e2988a7SThierry Reding 
2366fad7b806SThierry Reding 	value = (24000 * 12288) / (128 * sor->format.sample_rate / 1000);
23678e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_0960);
23688e2988a7SThierry Reding 	tegra_sor_writel(sor, 12288, SOR_AUDIO_NVAL_0960);
23698e2988a7SThierry Reding 
2370fad7b806SThierry Reding 	value = (24000 * 24576) / (128 * sor->format.sample_rate / 1000);
23718e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_1920);
23728e2988a7SThierry Reding 	tegra_sor_writel(sor, 24576, SOR_AUDIO_NVAL_1920);
23738e2988a7SThierry Reding 
23748e2988a7SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_N);
23758e2988a7SThierry Reding 	value &= ~SOR_HDMI_AUDIO_N_RESET;
23768e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_N);
23778e2988a7SThierry Reding 
23788e2988a7SThierry Reding 	tegra_sor_hdmi_enable_audio_infoframe(sor);
23798e2988a7SThierry Reding }
23808e2988a7SThierry Reding 
2381459cc2c6SThierry Reding static void tegra_sor_hdmi_disable_audio_infoframe(struct tegra_sor *sor)
2382459cc2c6SThierry Reding {
2383459cc2c6SThierry Reding 	u32 value;
2384459cc2c6SThierry Reding 
2385459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2386459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_ENABLE;
2387459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2388459cc2c6SThierry Reding }
2389459cc2c6SThierry Reding 
23908e2988a7SThierry Reding static void tegra_sor_hdmi_audio_disable(struct tegra_sor *sor)
23918e2988a7SThierry Reding {
23928e2988a7SThierry Reding 	tegra_sor_hdmi_disable_audio_infoframe(sor);
23938e2988a7SThierry Reding }
23948e2988a7SThierry Reding 
2395459cc2c6SThierry Reding static struct tegra_sor_hdmi_settings *
2396459cc2c6SThierry Reding tegra_sor_hdmi_find_settings(struct tegra_sor *sor, unsigned long frequency)
2397459cc2c6SThierry Reding {
2398459cc2c6SThierry Reding 	unsigned int i;
2399459cc2c6SThierry Reding 
2400459cc2c6SThierry Reding 	for (i = 0; i < sor->num_settings; i++)
2401459cc2c6SThierry Reding 		if (frequency <= sor->settings[i].frequency)
2402459cc2c6SThierry Reding 			return &sor->settings[i];
2403459cc2c6SThierry Reding 
2404459cc2c6SThierry Reding 	return NULL;
2405459cc2c6SThierry Reding }
2406459cc2c6SThierry Reding 
240736e90221SThierry Reding static void tegra_sor_hdmi_disable_scrambling(struct tegra_sor *sor)
240836e90221SThierry Reding {
240936e90221SThierry Reding 	u32 value;
241036e90221SThierry Reding 
241136e90221SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI2_CTRL);
241236e90221SThierry Reding 	value &= ~SOR_HDMI2_CTRL_CLOCK_MODE_DIV_BY_4;
241336e90221SThierry Reding 	value &= ~SOR_HDMI2_CTRL_SCRAMBLE;
241436e90221SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI2_CTRL);
241536e90221SThierry Reding }
241636e90221SThierry Reding 
241736e90221SThierry Reding static void tegra_sor_hdmi_scdc_disable(struct tegra_sor *sor)
241836e90221SThierry Reding {
241936e90221SThierry Reding 	struct i2c_adapter *ddc = sor->output.ddc;
242036e90221SThierry Reding 
242136e90221SThierry Reding 	drm_scdc_set_high_tmds_clock_ratio(ddc, false);
242236e90221SThierry Reding 	drm_scdc_set_scrambling(ddc, false);
242336e90221SThierry Reding 
242436e90221SThierry Reding 	tegra_sor_hdmi_disable_scrambling(sor);
242536e90221SThierry Reding }
242636e90221SThierry Reding 
242736e90221SThierry Reding static void tegra_sor_hdmi_scdc_stop(struct tegra_sor *sor)
242836e90221SThierry Reding {
242936e90221SThierry Reding 	if (sor->scdc_enabled) {
243036e90221SThierry Reding 		cancel_delayed_work_sync(&sor->scdc);
243136e90221SThierry Reding 		tegra_sor_hdmi_scdc_disable(sor);
243236e90221SThierry Reding 	}
243336e90221SThierry Reding }
243436e90221SThierry Reding 
243536e90221SThierry Reding static void tegra_sor_hdmi_enable_scrambling(struct tegra_sor *sor)
243636e90221SThierry Reding {
243736e90221SThierry Reding 	u32 value;
243836e90221SThierry Reding 
243936e90221SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI2_CTRL);
244036e90221SThierry Reding 	value |= SOR_HDMI2_CTRL_CLOCK_MODE_DIV_BY_4;
244136e90221SThierry Reding 	value |= SOR_HDMI2_CTRL_SCRAMBLE;
244236e90221SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI2_CTRL);
244336e90221SThierry Reding }
244436e90221SThierry Reding 
244536e90221SThierry Reding static void tegra_sor_hdmi_scdc_enable(struct tegra_sor *sor)
244636e90221SThierry Reding {
244736e90221SThierry Reding 	struct i2c_adapter *ddc = sor->output.ddc;
244836e90221SThierry Reding 
244936e90221SThierry Reding 	drm_scdc_set_high_tmds_clock_ratio(ddc, true);
245036e90221SThierry Reding 	drm_scdc_set_scrambling(ddc, true);
245136e90221SThierry Reding 
245236e90221SThierry Reding 	tegra_sor_hdmi_enable_scrambling(sor);
245336e90221SThierry Reding }
245436e90221SThierry Reding 
245536e90221SThierry Reding static void tegra_sor_hdmi_scdc_work(struct work_struct *work)
245636e90221SThierry Reding {
245736e90221SThierry Reding 	struct tegra_sor *sor = container_of(work, struct tegra_sor, scdc.work);
245836e90221SThierry Reding 	struct i2c_adapter *ddc = sor->output.ddc;
245936e90221SThierry Reding 
246036e90221SThierry Reding 	if (!drm_scdc_get_scrambling_status(ddc)) {
246136e90221SThierry Reding 		DRM_DEBUG_KMS("SCDC not scrambled\n");
246236e90221SThierry Reding 		tegra_sor_hdmi_scdc_enable(sor);
246336e90221SThierry Reding 	}
246436e90221SThierry Reding 
246536e90221SThierry Reding 	schedule_delayed_work(&sor->scdc, msecs_to_jiffies(5000));
246636e90221SThierry Reding }
246736e90221SThierry Reding 
246836e90221SThierry Reding static void tegra_sor_hdmi_scdc_start(struct tegra_sor *sor)
246936e90221SThierry Reding {
247036e90221SThierry Reding 	struct drm_scdc *scdc = &sor->output.connector.display_info.hdmi.scdc;
247136e90221SThierry Reding 	struct drm_display_mode *mode;
247236e90221SThierry Reding 
247336e90221SThierry Reding 	mode = &sor->output.encoder.crtc->state->adjusted_mode;
247436e90221SThierry Reding 
247536e90221SThierry Reding 	if (mode->clock >= 340000 && scdc->supported) {
247636e90221SThierry Reding 		schedule_delayed_work(&sor->scdc, msecs_to_jiffies(5000));
247736e90221SThierry Reding 		tegra_sor_hdmi_scdc_enable(sor);
247836e90221SThierry Reding 		sor->scdc_enabled = true;
247936e90221SThierry Reding 	}
248036e90221SThierry Reding }
248136e90221SThierry Reding 
2482459cc2c6SThierry Reding static void tegra_sor_hdmi_disable(struct drm_encoder *encoder)
2483459cc2c6SThierry Reding {
2484459cc2c6SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
2485459cc2c6SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
2486459cc2c6SThierry Reding 	struct tegra_sor *sor = to_sor(output);
2487459cc2c6SThierry Reding 	u32 value;
2488459cc2c6SThierry Reding 	int err;
2489459cc2c6SThierry Reding 
24908e2988a7SThierry Reding 	tegra_sor_audio_unprepare(sor);
249136e90221SThierry Reding 	tegra_sor_hdmi_scdc_stop(sor);
249236e90221SThierry Reding 
2493459cc2c6SThierry Reding 	err = tegra_sor_detach(sor);
2494459cc2c6SThierry Reding 	if (err < 0)
2495459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to detach SOR: %d\n", err);
2496459cc2c6SThierry Reding 
2497459cc2c6SThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE1);
2498459cc2c6SThierry Reding 	tegra_sor_update(sor);
2499459cc2c6SThierry Reding 
2500459cc2c6SThierry Reding 	/* disable display to SOR clock */
2501459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
2502c57997bcSThierry Reding 
2503c57997bcSThierry Reding 	if (!sor->soc->has_nvdisplay)
2504c57997bcSThierry Reding 		value &= ~(SOR1_TIMING_CYA | SOR_ENABLE(1));
2505c57997bcSThierry Reding 	else
2506c57997bcSThierry Reding 		value &= ~SOR_ENABLE(sor->index);
2507c57997bcSThierry Reding 
2508459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2509459cc2c6SThierry Reding 
2510459cc2c6SThierry Reding 	tegra_dc_commit(dc);
2511459cc2c6SThierry Reding 
2512459cc2c6SThierry Reding 	err = tegra_sor_power_down(sor);
2513459cc2c6SThierry Reding 	if (err < 0)
2514459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power down SOR: %d\n", err);
2515459cc2c6SThierry Reding 
2516c57997bcSThierry Reding 	err = tegra_io_pad_power_disable(sor->pad);
2517459cc2c6SThierry Reding 	if (err < 0)
2518c57997bcSThierry Reding 		dev_err(sor->dev, "failed to power off I/O pad: %d\n", err);
2519459cc2c6SThierry Reding 
2520aaff8bd2SThierry Reding 	pm_runtime_put(sor->dev);
2521459cc2c6SThierry Reding }
2522459cc2c6SThierry Reding 
2523459cc2c6SThierry Reding static void tegra_sor_hdmi_enable(struct drm_encoder *encoder)
2524459cc2c6SThierry Reding {
2525459cc2c6SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
2526459cc2c6SThierry Reding 	unsigned int h_ref_to_sync = 1, pulse_start, max_ac;
2527459cc2c6SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
2528459cc2c6SThierry Reding 	struct tegra_sor_hdmi_settings *settings;
2529459cc2c6SThierry Reding 	struct tegra_sor *sor = to_sor(output);
2530c31efa7aSThierry Reding 	struct tegra_sor_state *state;
2531459cc2c6SThierry Reding 	struct drm_display_mode *mode;
253236e90221SThierry Reding 	unsigned long rate, pclk;
253330b49435SThierry Reding 	unsigned int div, i;
2534459cc2c6SThierry Reding 	u32 value;
2535459cc2c6SThierry Reding 	int err;
2536459cc2c6SThierry Reding 
2537c31efa7aSThierry Reding 	state = to_sor_state(output->connector.state);
2538459cc2c6SThierry Reding 	mode = &encoder->crtc->state->adjusted_mode;
253936e90221SThierry Reding 	pclk = mode->clock * 1000;
2540459cc2c6SThierry Reding 
2541aaff8bd2SThierry Reding 	pm_runtime_get_sync(sor->dev);
2542459cc2c6SThierry Reding 
254325bb2cecSThierry Reding 	/* switch to safe parent clock */
254425bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
2545e1335e2fSThierry Reding 	if (err < 0) {
2546459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
2547e1335e2fSThierry Reding 		return;
2548e1335e2fSThierry Reding 	}
2549459cc2c6SThierry Reding 
2550459cc2c6SThierry Reding 	div = clk_get_rate(sor->clk) / 1000000 * 4;
2551459cc2c6SThierry Reding 
2552c57997bcSThierry Reding 	err = tegra_io_pad_power_enable(sor->pad);
2553459cc2c6SThierry Reding 	if (err < 0)
2554c57997bcSThierry Reding 		dev_err(sor->dev, "failed to power on I/O pad: %d\n", err);
2555459cc2c6SThierry Reding 
2556459cc2c6SThierry Reding 	usleep_range(20, 100);
2557459cc2c6SThierry Reding 
2558880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
2559459cc2c6SThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
2560880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
2561459cc2c6SThierry Reding 
2562459cc2c6SThierry Reding 	usleep_range(20, 100);
2563459cc2c6SThierry Reding 
2564880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll3);
2565459cc2c6SThierry Reding 	value &= ~SOR_PLL3_PLL_VDD_MODE_3V3;
2566880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll3);
2567459cc2c6SThierry Reding 
2568880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
2569459cc2c6SThierry Reding 	value &= ~SOR_PLL0_VCOPD;
2570459cc2c6SThierry Reding 	value &= ~SOR_PLL0_PWR;
2571880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
2572459cc2c6SThierry Reding 
2573880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
2574459cc2c6SThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
2575880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
2576459cc2c6SThierry Reding 
2577459cc2c6SThierry Reding 	usleep_range(200, 400);
2578459cc2c6SThierry Reding 
2579880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
2580459cc2c6SThierry Reding 	value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
2581459cc2c6SThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
2582880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
2583459cc2c6SThierry Reding 
2584459cc2c6SThierry Reding 	usleep_range(20, 100);
2585459cc2c6SThierry Reding 
2586880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2587459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
2588459cc2c6SThierry Reding 		 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2;
2589880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2590459cc2c6SThierry Reding 
2591459cc2c6SThierry Reding 	while (true) {
2592459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2593459cc2c6SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_STATE_BUSY) == 0)
2594459cc2c6SThierry Reding 			break;
2595459cc2c6SThierry Reding 
2596459cc2c6SThierry Reding 		usleep_range(250, 1000);
2597459cc2c6SThierry Reding 	}
2598459cc2c6SThierry Reding 
2599459cc2c6SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
2600459cc2c6SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_UP | SOR_LANE_SEQ_CTL_DELAY(5);
2601459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
2602459cc2c6SThierry Reding 
2603459cc2c6SThierry Reding 	while (true) {
2604459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2605459cc2c6SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
2606459cc2c6SThierry Reding 			break;
2607459cc2c6SThierry Reding 
2608459cc2c6SThierry Reding 		usleep_range(250, 1000);
2609459cc2c6SThierry Reding 	}
2610459cc2c6SThierry Reding 
2611459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
2612459cc2c6SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
2613459cc2c6SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
2614459cc2c6SThierry Reding 
261536e90221SThierry Reding 	if (mode->clock < 340000) {
261636e90221SThierry Reding 		DRM_DEBUG_KMS("setting 2.7 GHz link speed\n");
2617459cc2c6SThierry Reding 		value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G2_70;
261836e90221SThierry Reding 	} else {
261936e90221SThierry Reding 		DRM_DEBUG_KMS("setting 5.4 GHz link speed\n");
2620459cc2c6SThierry Reding 		value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G5_40;
262136e90221SThierry Reding 	}
2622459cc2c6SThierry Reding 
2623459cc2c6SThierry Reding 	value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
2624459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
2625459cc2c6SThierry Reding 
2626c57997bcSThierry Reding 	/* SOR pad PLL stabilization time */
2627c57997bcSThierry Reding 	usleep_range(250, 1000);
2628c57997bcSThierry Reding 
2629c57997bcSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
2630c57997bcSThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
2631c57997bcSThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(4);
2632c57997bcSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
2633c57997bcSThierry Reding 
2634459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
2635c57997bcSThierry Reding 	value &= ~SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
2636459cc2c6SThierry Reding 	value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
2637c57997bcSThierry Reding 	value &= ~SOR_DP_SPARE_SEQ_ENABLE;
2638c57997bcSThierry Reding 	value &= ~SOR_DP_SPARE_MACRO_SOR_CLK;
2639459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
2640459cc2c6SThierry Reding 
2641459cc2c6SThierry Reding 	value = SOR_SEQ_CTL_PU_PC(0) | SOR_SEQ_CTL_PU_PC_ALT(0) |
2642459cc2c6SThierry Reding 		SOR_SEQ_CTL_PD_PC(8) | SOR_SEQ_CTL_PD_PC_ALT(8);
2643459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_CTL);
2644459cc2c6SThierry Reding 
2645459cc2c6SThierry Reding 	value = SOR_SEQ_INST_DRIVE_PWM_OUT_LO | SOR_SEQ_INST_HALT |
2646459cc2c6SThierry Reding 		SOR_SEQ_INST_WAIT_VSYNC | SOR_SEQ_INST_WAIT(1);
2647459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_INST(0));
2648459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_INST(8));
2649459cc2c6SThierry Reding 
2650c57997bcSThierry Reding 	if (!sor->soc->has_nvdisplay) {
2651459cc2c6SThierry Reding 		/* program the reference clock */
2652459cc2c6SThierry Reding 		value = SOR_REFCLK_DIV_INT(div) | SOR_REFCLK_DIV_FRAC(div);
2653459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, SOR_REFCLK);
2654c57997bcSThierry Reding 	}
2655459cc2c6SThierry Reding 
265630b49435SThierry Reding 	/* XXX not in TRM */
265730b49435SThierry Reding 	for (value = 0, i = 0; i < 5; i++)
26586d6c815dSThierry Reding 		value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->xbar_cfg[i]) |
265930b49435SThierry Reding 			 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
2660459cc2c6SThierry Reding 
2661459cc2c6SThierry Reding 	tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
266230b49435SThierry Reding 	tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
2663459cc2c6SThierry Reding 
266425bb2cecSThierry Reding 	/* switch to parent clock */
2665e1335e2fSThierry Reding 	err = clk_set_parent(sor->clk, sor->clk_parent);
2666e1335e2fSThierry Reding 	if (err < 0) {
2667459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to set parent clock: %d\n", err);
2668e1335e2fSThierry Reding 		return;
2669e1335e2fSThierry Reding 	}
2670e1335e2fSThierry Reding 
2671e1335e2fSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_pad);
2672e1335e2fSThierry Reding 	if (err < 0) {
2673e1335e2fSThierry Reding 		dev_err(sor->dev, "failed to set pad clock: %d\n", err);
2674e1335e2fSThierry Reding 		return;
2675e1335e2fSThierry Reding 	}
2676459cc2c6SThierry Reding 
267736e90221SThierry Reding 	/* adjust clock rate for HDMI 2.0 modes */
267836e90221SThierry Reding 	rate = clk_get_rate(sor->clk_parent);
267936e90221SThierry Reding 
268036e90221SThierry Reding 	if (mode->clock >= 340000)
268136e90221SThierry Reding 		rate /= 2;
268236e90221SThierry Reding 
268336e90221SThierry Reding 	DRM_DEBUG_KMS("setting clock to %lu Hz, mode: %lu Hz\n", rate, pclk);
268436e90221SThierry Reding 
268536e90221SThierry Reding 	clk_set_rate(sor->clk, rate);
2686c57997bcSThierry Reding 
2687c57997bcSThierry Reding 	if (!sor->soc->has_nvdisplay) {
2688459cc2c6SThierry Reding 		value = SOR_INPUT_CONTROL_HDMI_SRC_SELECT(dc->pipe);
2689459cc2c6SThierry Reding 
2690459cc2c6SThierry Reding 		/* XXX is this the proper check? */
2691459cc2c6SThierry Reding 		if (mode->clock < 75000)
2692459cc2c6SThierry Reding 			value |= SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_LIMITED;
2693459cc2c6SThierry Reding 
2694459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, SOR_INPUT_CONTROL);
2695c57997bcSThierry Reding 	}
2696459cc2c6SThierry Reding 
2697459cc2c6SThierry Reding 	max_ac = ((mode->htotal - mode->hdisplay) - SOR_REKEY - 18) / 32;
2698459cc2c6SThierry Reding 
2699459cc2c6SThierry Reding 	value = SOR_HDMI_CTRL_ENABLE | SOR_HDMI_CTRL_MAX_AC_PACKET(max_ac) |
2700459cc2c6SThierry Reding 		SOR_HDMI_CTRL_AUDIO_LAYOUT | SOR_HDMI_CTRL_REKEY(SOR_REKEY);
2701459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_CTRL);
2702459cc2c6SThierry Reding 
2703c57997bcSThierry Reding 	if (!dc->soc->has_nvdisplay) {
2704459cc2c6SThierry Reding 		/* H_PULSE2 setup */
2705c57997bcSThierry Reding 		pulse_start = h_ref_to_sync +
2706c57997bcSThierry Reding 			      (mode->hsync_end - mode->hsync_start) +
2707459cc2c6SThierry Reding 			      (mode->htotal - mode->hsync_end) - 10;
2708459cc2c6SThierry Reding 
2709459cc2c6SThierry Reding 		value = PULSE_LAST_END_A | PULSE_QUAL_VACTIVE |
2710459cc2c6SThierry Reding 			PULSE_POLARITY_HIGH | PULSE_MODE_NORMAL;
2711459cc2c6SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_CONTROL);
2712459cc2c6SThierry Reding 
2713459cc2c6SThierry Reding 		value = PULSE_END(pulse_start + 8) | PULSE_START(pulse_start);
2714459cc2c6SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_POSITION_A);
2715459cc2c6SThierry Reding 
2716459cc2c6SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_DISP_SIGNAL_OPTIONS0);
2717459cc2c6SThierry Reding 		value |= H_PULSE2_ENABLE;
2718459cc2c6SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_SIGNAL_OPTIONS0);
2719c57997bcSThierry Reding 	}
2720459cc2c6SThierry Reding 
2721459cc2c6SThierry Reding 	/* infoframe setup */
2722459cc2c6SThierry Reding 	err = tegra_sor_hdmi_setup_avi_infoframe(sor, mode);
2723459cc2c6SThierry Reding 	if (err < 0)
2724459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
2725459cc2c6SThierry Reding 
2726459cc2c6SThierry Reding 	/* XXX HDMI audio support not implemented yet */
2727459cc2c6SThierry Reding 	tegra_sor_hdmi_disable_audio_infoframe(sor);
2728459cc2c6SThierry Reding 
2729459cc2c6SThierry Reding 	/* use single TMDS protocol */
2730459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
2731459cc2c6SThierry Reding 	value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
2732459cc2c6SThierry Reding 	value |= SOR_STATE_ASY_PROTOCOL_SINGLE_TMDS_A;
2733459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
2734459cc2c6SThierry Reding 
2735459cc2c6SThierry Reding 	/* power up pad calibration */
2736880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2737459cc2c6SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
2738880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2739459cc2c6SThierry Reding 
2740459cc2c6SThierry Reding 	/* production settings */
2741459cc2c6SThierry Reding 	settings = tegra_sor_hdmi_find_settings(sor, mode->clock * 1000);
2742db8b42fbSDan Carpenter 	if (!settings) {
2743db8b42fbSDan Carpenter 		dev_err(sor->dev, "no settings for pixel clock %d Hz\n",
2744db8b42fbSDan Carpenter 			mode->clock * 1000);
2745459cc2c6SThierry Reding 		return;
2746459cc2c6SThierry Reding 	}
2747459cc2c6SThierry Reding 
2748880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
2749459cc2c6SThierry Reding 	value &= ~SOR_PLL0_ICHPMP_MASK;
2750c57997bcSThierry Reding 	value &= ~SOR_PLL0_FILTER_MASK;
2751459cc2c6SThierry Reding 	value &= ~SOR_PLL0_VCOCAP_MASK;
2752459cc2c6SThierry Reding 	value |= SOR_PLL0_ICHPMP(settings->ichpmp);
2753c57997bcSThierry Reding 	value |= SOR_PLL0_FILTER(settings->filter);
2754459cc2c6SThierry Reding 	value |= SOR_PLL0_VCOCAP(settings->vcocap);
2755880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
2756459cc2c6SThierry Reding 
2757c57997bcSThierry Reding 	/* XXX not in TRM */
2758880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll1);
2759459cc2c6SThierry Reding 	value &= ~SOR_PLL1_LOADADJ_MASK;
2760c57997bcSThierry Reding 	value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
2761459cc2c6SThierry Reding 	value |= SOR_PLL1_LOADADJ(settings->loadadj);
2762c57997bcSThierry Reding 	value |= SOR_PLL1_TMDS_TERMADJ(settings->tmds_termadj);
2763c57997bcSThierry Reding 	value |= SOR_PLL1_TMDS_TERM;
2764880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll1);
2765459cc2c6SThierry Reding 
2766880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll3);
2767c57997bcSThierry Reding 	value &= ~SOR_PLL3_BG_TEMP_COEF_MASK;
2768459cc2c6SThierry Reding 	value &= ~SOR_PLL3_BG_VREF_LEVEL_MASK;
2769c57997bcSThierry Reding 	value &= ~SOR_PLL3_AVDD10_LEVEL_MASK;
2770c57997bcSThierry Reding 	value &= ~SOR_PLL3_AVDD14_LEVEL_MASK;
2771c57997bcSThierry Reding 	value |= SOR_PLL3_BG_TEMP_COEF(settings->bg_temp_coef);
2772c57997bcSThierry Reding 	value |= SOR_PLL3_BG_VREF_LEVEL(settings->bg_vref_level);
2773c57997bcSThierry Reding 	value |= SOR_PLL3_AVDD10_LEVEL(settings->avdd10_level);
2774c57997bcSThierry Reding 	value |= SOR_PLL3_AVDD14_LEVEL(settings->avdd14_level);
2775880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll3);
2776459cc2c6SThierry Reding 
2777c57997bcSThierry Reding 	value = settings->drive_current[3] << 24 |
2778c57997bcSThierry Reding 		settings->drive_current[2] << 16 |
2779c57997bcSThierry Reding 		settings->drive_current[1] <<  8 |
2780c57997bcSThierry Reding 		settings->drive_current[0] <<  0;
2781459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
2782459cc2c6SThierry Reding 
2783c57997bcSThierry Reding 	value = settings->preemphasis[3] << 24 |
2784c57997bcSThierry Reding 		settings->preemphasis[2] << 16 |
2785c57997bcSThierry Reding 		settings->preemphasis[1] <<  8 |
2786c57997bcSThierry Reding 		settings->preemphasis[0] <<  0;
2787459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
2788459cc2c6SThierry Reding 
2789880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2790459cc2c6SThierry Reding 	value &= ~SOR_DP_PADCTL_TX_PU_MASK;
2791459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU_ENABLE;
2792c57997bcSThierry Reding 	value |= SOR_DP_PADCTL_TX_PU(settings->tx_pu_value);
2793880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2794459cc2c6SThierry Reding 
2795c57997bcSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl2);
2796c57997bcSThierry Reding 	value &= ~SOR_DP_PADCTL_SPAREPLL_MASK;
2797c57997bcSThierry Reding 	value |= SOR_DP_PADCTL_SPAREPLL(settings->sparepll);
2798c57997bcSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl2);
2799c57997bcSThierry Reding 
2800459cc2c6SThierry Reding 	/* power down pad calibration */
2801880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2802459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
2803880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2804459cc2c6SThierry Reding 
2805c57997bcSThierry Reding 	if (!dc->soc->has_nvdisplay) {
2806459cc2c6SThierry Reding 		/* miscellaneous display controller settings */
2807459cc2c6SThierry Reding 		value = VSYNC_H_POSITION(1);
2808459cc2c6SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_TIMING_OPTIONS);
2809c57997bcSThierry Reding 	}
2810459cc2c6SThierry Reding 
2811459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_COLOR_CONTROL);
2812459cc2c6SThierry Reding 	value &= ~DITHER_CONTROL_MASK;
2813459cc2c6SThierry Reding 	value &= ~BASE_COLOR_SIZE_MASK;
2814459cc2c6SThierry Reding 
2815c31efa7aSThierry Reding 	switch (state->bpc) {
2816459cc2c6SThierry Reding 	case 6:
2817459cc2c6SThierry Reding 		value |= BASE_COLOR_SIZE_666;
2818459cc2c6SThierry Reding 		break;
2819459cc2c6SThierry Reding 
2820459cc2c6SThierry Reding 	case 8:
2821459cc2c6SThierry Reding 		value |= BASE_COLOR_SIZE_888;
2822459cc2c6SThierry Reding 		break;
2823459cc2c6SThierry Reding 
2824c57997bcSThierry Reding 	case 10:
2825c57997bcSThierry Reding 		value |= BASE_COLOR_SIZE_101010;
2826c57997bcSThierry Reding 		break;
2827c57997bcSThierry Reding 
2828c57997bcSThierry Reding 	case 12:
2829c57997bcSThierry Reding 		value |= BASE_COLOR_SIZE_121212;
2830c57997bcSThierry Reding 		break;
2831c57997bcSThierry Reding 
2832459cc2c6SThierry Reding 	default:
2833c31efa7aSThierry Reding 		WARN(1, "%u bits-per-color not supported\n", state->bpc);
2834c31efa7aSThierry Reding 		value |= BASE_COLOR_SIZE_888;
2835459cc2c6SThierry Reding 		break;
2836459cc2c6SThierry Reding 	}
2837459cc2c6SThierry Reding 
2838459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_COLOR_CONTROL);
2839459cc2c6SThierry Reding 
2840c57997bcSThierry Reding 	/* XXX set display head owner */
2841c57997bcSThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
2842c57997bcSThierry Reding 	value &= ~SOR_STATE_ASY_OWNER_MASK;
2843c57997bcSThierry Reding 	value |= SOR_STATE_ASY_OWNER(1 + dc->pipe);
2844c57997bcSThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
2845c57997bcSThierry Reding 
2846459cc2c6SThierry Reding 	err = tegra_sor_power_up(sor, 250);
2847459cc2c6SThierry Reding 	if (err < 0)
2848459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
2849459cc2c6SThierry Reding 
28502bd1dd39SThierry Reding 	/* configure dynamic range of output */
2851880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
2852459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_RANGECOMPRESS_MASK;
2853459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_DYNRANGE_MASK;
2854880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
2855459cc2c6SThierry Reding 
28562bd1dd39SThierry Reding 	/* configure colorspace */
2857880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
2858459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_COLORSPACE_MASK;
2859459cc2c6SThierry Reding 	value |= SOR_HEAD_STATE_COLORSPACE_RGB;
2860880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
2861459cc2c6SThierry Reding 
2862c31efa7aSThierry Reding 	tegra_sor_mode_set(sor, mode, state);
2863459cc2c6SThierry Reding 
2864459cc2c6SThierry Reding 	tegra_sor_update(sor);
2865459cc2c6SThierry Reding 
2866c57997bcSThierry Reding 	/* program preamble timing in SOR (XXX) */
2867c57997bcSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
2868c57997bcSThierry Reding 	value &= ~SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
2869c57997bcSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
2870c57997bcSThierry Reding 
2871459cc2c6SThierry Reding 	err = tegra_sor_attach(sor);
2872459cc2c6SThierry Reding 	if (err < 0)
2873459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to attach SOR: %d\n", err);
2874459cc2c6SThierry Reding 
2875459cc2c6SThierry Reding 	/* enable display to SOR clock and generate HDMI preamble */
2876459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
2877c57997bcSThierry Reding 
2878c57997bcSThierry Reding 	if (!sor->soc->has_nvdisplay)
2879c57997bcSThierry Reding 		value |= SOR_ENABLE(1) | SOR1_TIMING_CYA;
2880c57997bcSThierry Reding 	else
2881c57997bcSThierry Reding 		value |= SOR_ENABLE(sor->index);
2882c57997bcSThierry Reding 
2883459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2884459cc2c6SThierry Reding 
2885c57997bcSThierry Reding 	if (dc->soc->has_nvdisplay) {
2886c57997bcSThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_CORE_SOR_SET_CONTROL(sor->index));
2887c57997bcSThierry Reding 		value &= ~PROTOCOL_MASK;
2888c57997bcSThierry Reding 		value |= PROTOCOL_SINGLE_TMDS_A;
2889c57997bcSThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_CORE_SOR_SET_CONTROL(sor->index));
2890c57997bcSThierry Reding 	}
2891c57997bcSThierry Reding 
2892459cc2c6SThierry Reding 	tegra_dc_commit(dc);
2893459cc2c6SThierry Reding 
2894459cc2c6SThierry Reding 	err = tegra_sor_wakeup(sor);
2895459cc2c6SThierry Reding 	if (err < 0)
2896459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
289736e90221SThierry Reding 
289836e90221SThierry Reding 	tegra_sor_hdmi_scdc_start(sor);
28998e2988a7SThierry Reding 	tegra_sor_audio_prepare(sor);
2900459cc2c6SThierry Reding }
2901459cc2c6SThierry Reding 
2902459cc2c6SThierry Reding static const struct drm_encoder_helper_funcs tegra_sor_hdmi_helpers = {
2903459cc2c6SThierry Reding 	.disable = tegra_sor_hdmi_disable,
2904459cc2c6SThierry Reding 	.enable = tegra_sor_hdmi_enable,
2905459cc2c6SThierry Reding 	.atomic_check = tegra_sor_encoder_atomic_check,
2906459cc2c6SThierry Reding };
2907459cc2c6SThierry Reding 
29080472c21bSThierry Reding static void tegra_sor_dp_disable(struct drm_encoder *encoder)
29090472c21bSThierry Reding {
29100472c21bSThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
29110472c21bSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
29120472c21bSThierry Reding 	struct tegra_sor *sor = to_sor(output);
29130472c21bSThierry Reding 	u32 value;
29140472c21bSThierry Reding 	int err;
29150472c21bSThierry Reding 
29160472c21bSThierry Reding 	err = drm_dp_link_power_down(sor->aux, &sor->link);
29170472c21bSThierry Reding 	if (err < 0)
29180472c21bSThierry Reding 		dev_err(sor->dev, "failed to power down link: %d\n", err);
29190472c21bSThierry Reding 
29200472c21bSThierry Reding 	err = tegra_sor_detach(sor);
29210472c21bSThierry Reding 	if (err < 0)
29220472c21bSThierry Reding 		dev_err(sor->dev, "failed to detach SOR: %d\n", err);
29230472c21bSThierry Reding 
29240472c21bSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE1);
29250472c21bSThierry Reding 	tegra_sor_update(sor);
29260472c21bSThierry Reding 
29270472c21bSThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
29280472c21bSThierry Reding 
29290472c21bSThierry Reding 	if (!sor->soc->has_nvdisplay)
29300472c21bSThierry Reding 		value &= ~(SOR1_TIMING_CYA | SOR_ENABLE(1));
29310472c21bSThierry Reding 	else
29320472c21bSThierry Reding 		value &= ~SOR_ENABLE(sor->index);
29330472c21bSThierry Reding 
29340472c21bSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
29350472c21bSThierry Reding 	tegra_dc_commit(dc);
29360472c21bSThierry Reding 
29370472c21bSThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
29380472c21bSThierry Reding 	value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
29390472c21bSThierry Reding 	value &= ~SOR_STATE_ASY_SUBOWNER_MASK;
29400472c21bSThierry Reding 	value &= ~SOR_STATE_ASY_OWNER_MASK;
29410472c21bSThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
29420472c21bSThierry Reding 	tegra_sor_update(sor);
29430472c21bSThierry Reding 
29440472c21bSThierry Reding 	/* switch to safe parent clock */
29450472c21bSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
29460472c21bSThierry Reding 	if (err < 0)
29470472c21bSThierry Reding 		dev_err(sor->dev, "failed to set safe clock: %d\n", err);
29480472c21bSThierry Reding 
29490472c21bSThierry Reding 	err = tegra_sor_power_down(sor);
29500472c21bSThierry Reding 	if (err < 0)
29510472c21bSThierry Reding 		dev_err(sor->dev, "failed to power down SOR: %d\n", err);
29520472c21bSThierry Reding 
29530472c21bSThierry Reding 	err = tegra_io_pad_power_disable(sor->pad);
29540472c21bSThierry Reding 	if (err < 0)
29550472c21bSThierry Reding 		dev_err(sor->dev, "failed to power off I/O pad: %d\n", err);
29560472c21bSThierry Reding 
29570472c21bSThierry Reding 	err = drm_dp_aux_disable(sor->aux);
29580472c21bSThierry Reding 	if (err < 0)
29590472c21bSThierry Reding 		dev_err(sor->dev, "failed disable DPAUX: %d\n", err);
29600472c21bSThierry Reding 
29610472c21bSThierry Reding 	pm_runtime_put(sor->dev);
29620472c21bSThierry Reding }
29630472c21bSThierry Reding 
29640472c21bSThierry Reding static void tegra_sor_dp_enable(struct drm_encoder *encoder)
29650472c21bSThierry Reding {
29660472c21bSThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
29670472c21bSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
29680472c21bSThierry Reding 	struct tegra_sor *sor = to_sor(output);
29690472c21bSThierry Reding 	struct tegra_sor_config config;
29700472c21bSThierry Reding 	struct tegra_sor_state *state;
29710472c21bSThierry Reding 	struct drm_display_mode *mode;
29720472c21bSThierry Reding 	struct drm_display_info *info;
29730472c21bSThierry Reding 	unsigned int i;
29740472c21bSThierry Reding 	u32 value;
29750472c21bSThierry Reding 	int err;
29760472c21bSThierry Reding 
29770472c21bSThierry Reding 	state = to_sor_state(output->connector.state);
29780472c21bSThierry Reding 	mode = &encoder->crtc->state->adjusted_mode;
29790472c21bSThierry Reding 	info = &output->connector.display_info;
29800472c21bSThierry Reding 
29810472c21bSThierry Reding 	pm_runtime_get_sync(sor->dev);
29820472c21bSThierry Reding 
29830472c21bSThierry Reding 	/* switch to safe parent clock */
29840472c21bSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
29850472c21bSThierry Reding 	if (err < 0)
29860472c21bSThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
29870472c21bSThierry Reding 
29880472c21bSThierry Reding 	err = tegra_io_pad_power_enable(sor->pad);
29890472c21bSThierry Reding 	if (err < 0)
29900472c21bSThierry Reding 		dev_err(sor->dev, "failed to power on LVDS rail: %d\n", err);
29910472c21bSThierry Reding 
29920472c21bSThierry Reding 	usleep_range(20, 100);
29930472c21bSThierry Reding 
29940472c21bSThierry Reding 	err = drm_dp_aux_enable(sor->aux);
29950472c21bSThierry Reding 	if (err < 0)
29960472c21bSThierry Reding 		dev_err(sor->dev, "failed to enable DPAUX: %d\n", err);
29970472c21bSThierry Reding 
29980472c21bSThierry Reding 	err = drm_dp_link_probe(sor->aux, &sor->link);
29990472c21bSThierry Reding 	if (err < 0)
30000472c21bSThierry Reding 		dev_err(sor->dev, "failed to probe DP link: %d\n", err);
30010472c21bSThierry Reding 
30020472c21bSThierry Reding 	err = drm_dp_link_choose(&sor->link, mode, info);
30030472c21bSThierry Reding 	if (err < 0)
30040472c21bSThierry Reding 		dev_err(sor->dev, "failed to choose link: %d\n", err);
30050472c21bSThierry Reding 
30060472c21bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
30070472c21bSThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
30080472c21bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
30090472c21bSThierry Reding 
30100472c21bSThierry Reding 	usleep_range(20, 40);
30110472c21bSThierry Reding 
30120472c21bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll3);
30130472c21bSThierry Reding 	value |= SOR_PLL3_PLL_VDD_MODE_3V3;
30140472c21bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll3);
30150472c21bSThierry Reding 
30160472c21bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
30170472c21bSThierry Reding 	value &= ~(SOR_PLL0_VCOPD | SOR_PLL0_PWR);
30180472c21bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
30190472c21bSThierry Reding 
30200472c21bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
30210472c21bSThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
30220472c21bSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD;
30230472c21bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
30240472c21bSThierry Reding 
30250472c21bSThierry Reding 	usleep_range(200, 400);
30260472c21bSThierry Reding 
30270472c21bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
30280472c21bSThierry Reding 	value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
30290472c21bSThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
30300472c21bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
30310472c21bSThierry Reding 
30320472c21bSThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
30330472c21bSThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
30340472c21bSThierry Reding 	value |= SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_DPCLK;
30350472c21bSThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
30360472c21bSThierry Reding 
30370472c21bSThierry Reding 	usleep_range(200, 400);
30380472c21bSThierry Reding 
30390472c21bSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
30400472c21bSThierry Reding 	/* XXX not in TRM */
30410472c21bSThierry Reding 	value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
30420472c21bSThierry Reding 	value |= SOR_DP_SPARE_SEQ_ENABLE;
30430472c21bSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
30440472c21bSThierry Reding 
30450472c21bSThierry Reding 	/* XXX not in TRM */
30460472c21bSThierry Reding 	tegra_sor_writel(sor, 0, SOR_LVDS);
30470472c21bSThierry Reding 
30480472c21bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
30490472c21bSThierry Reding 	value &= ~SOR_PLL0_ICHPMP_MASK;
30500472c21bSThierry Reding 	value &= ~SOR_PLL0_VCOCAP_MASK;
30510472c21bSThierry Reding 	value |= SOR_PLL0_ICHPMP(0x1);
30520472c21bSThierry Reding 	value |= SOR_PLL0_VCOCAP(0x3);
30530472c21bSThierry Reding 	value |= SOR_PLL0_RESISTOR_EXT;
30540472c21bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
30550472c21bSThierry Reding 
30560472c21bSThierry Reding 	/* XXX not in TRM */
30570472c21bSThierry Reding 	for (value = 0, i = 0; i < 5; i++)
30580472c21bSThierry Reding 		value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->soc->xbar_cfg[i]) |
30590472c21bSThierry Reding 			 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
30600472c21bSThierry Reding 
30610472c21bSThierry Reding 	tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
30620472c21bSThierry Reding 	tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
30630472c21bSThierry Reding 
30640472c21bSThierry Reding 	/* switch to DP parent clock */
30650472c21bSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_pad);
30660472c21bSThierry Reding 	if (err < 0) {
30670472c21bSThierry Reding 		dev_err(sor->dev, "failed to switch to pad clock: %d\n", err);
30680472c21bSThierry Reding 		return;
30690472c21bSThierry Reding 	}
30700472c21bSThierry Reding 
30710472c21bSThierry Reding 	err = clk_set_parent(sor->clk, sor->clk_parent);
30720472c21bSThierry Reding 	if (err < 0) {
30730472c21bSThierry Reding 		dev_err(sor->dev, "failed to switch to parent clock: %d\n", err);
30740472c21bSThierry Reding 		return;
30750472c21bSThierry Reding 	}
30760472c21bSThierry Reding 
30770472c21bSThierry Reding 	/* use DP-A protocol */
30780472c21bSThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
30790472c21bSThierry Reding 	value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
30800472c21bSThierry Reding 	value |= SOR_STATE_ASY_PROTOCOL_DP_A;
30810472c21bSThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
30820472c21bSThierry Reding 
30830472c21bSThierry Reding 	/* enable port */
30840472c21bSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
30850472c21bSThierry Reding 	value |= SOR_DP_LINKCTL_ENABLE;
30860472c21bSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
30870472c21bSThierry Reding 
30880472c21bSThierry Reding 	tegra_sor_dp_term_calibrate(sor);
30890472c21bSThierry Reding 
30900472c21bSThierry Reding 	err = drm_dp_link_train(&sor->link);
30910472c21bSThierry Reding 	if (err < 0)
30920472c21bSThierry Reding 		dev_err(sor->dev, "link training failed: %d\n", err);
30930472c21bSThierry Reding 	else
30940472c21bSThierry Reding 		dev_dbg(sor->dev, "link training succeeded\n");
30950472c21bSThierry Reding 
30960472c21bSThierry Reding 	err = drm_dp_link_power_up(sor->aux, &sor->link);
30970472c21bSThierry Reding 	if (err < 0)
30980472c21bSThierry Reding 		dev_err(sor->dev, "failed to power up DP link: %d\n", err);
30990472c21bSThierry Reding 
31000472c21bSThierry Reding 	/* compute configuration */
31010472c21bSThierry Reding 	memset(&config, 0, sizeof(config));
31020472c21bSThierry Reding 	config.bits_per_pixel = state->bpc * 3;
31030472c21bSThierry Reding 
31040472c21bSThierry Reding 	err = tegra_sor_compute_config(sor, mode, &config, &sor->link);
31050472c21bSThierry Reding 	if (err < 0)
31060472c21bSThierry Reding 		dev_err(sor->dev, "failed to compute configuration: %d\n", err);
31070472c21bSThierry Reding 
31080472c21bSThierry Reding 	tegra_sor_apply_config(sor, &config);
31090472c21bSThierry Reding 	tegra_sor_mode_set(sor, mode, state);
31100472c21bSThierry Reding 	tegra_sor_update(sor);
31110472c21bSThierry Reding 
31120472c21bSThierry Reding 	err = tegra_sor_power_up(sor, 250);
31130472c21bSThierry Reding 	if (err < 0)
31140472c21bSThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
31150472c21bSThierry Reding 
31160472c21bSThierry Reding 	/* attach and wake up */
31170472c21bSThierry Reding 	err = tegra_sor_attach(sor);
31180472c21bSThierry Reding 	if (err < 0)
31190472c21bSThierry Reding 		dev_err(sor->dev, "failed to attach SOR: %d\n", err);
31200472c21bSThierry Reding 
31210472c21bSThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
31220472c21bSThierry Reding 	value |= SOR_ENABLE(sor->index);
31230472c21bSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
31240472c21bSThierry Reding 
31250472c21bSThierry Reding 	tegra_dc_commit(dc);
31260472c21bSThierry Reding 
31270472c21bSThierry Reding 	err = tegra_sor_wakeup(sor);
31280472c21bSThierry Reding 	if (err < 0)
31290472c21bSThierry Reding 		dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
31300472c21bSThierry Reding }
31310472c21bSThierry Reding 
31320472c21bSThierry Reding static const struct drm_encoder_helper_funcs tegra_sor_dp_helpers = {
31330472c21bSThierry Reding 	.disable = tegra_sor_dp_disable,
31340472c21bSThierry Reding 	.enable = tegra_sor_dp_enable,
31350472c21bSThierry Reding 	.atomic_check = tegra_sor_encoder_atomic_check,
31360472c21bSThierry Reding };
31370472c21bSThierry Reding 
31386b6b6042SThierry Reding static int tegra_sor_init(struct host1x_client *client)
31396b6b6042SThierry Reding {
31409910f5c4SThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->parent);
3141459cc2c6SThierry Reding 	const struct drm_encoder_helper_funcs *helpers = NULL;
31426b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
3143459cc2c6SThierry Reding 	int connector = DRM_MODE_CONNECTOR_Unknown;
3144459cc2c6SThierry Reding 	int encoder = DRM_MODE_ENCODER_NONE;
31456b6b6042SThierry Reding 	int err;
31466b6b6042SThierry Reding 
31479542c237SThierry Reding 	if (!sor->aux) {
3148459cc2c6SThierry Reding 		if (sor->soc->supports_hdmi) {
3149459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_HDMIA;
3150459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
3151459cc2c6SThierry Reding 			helpers = &tegra_sor_hdmi_helpers;
3152459cc2c6SThierry Reding 		} else if (sor->soc->supports_lvds) {
3153459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_LVDS;
3154459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_LVDS;
3155459cc2c6SThierry Reding 		}
3156459cc2c6SThierry Reding 	} else {
3157459cc2c6SThierry Reding 		if (sor->soc->supports_edp) {
3158459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_eDP;
3159459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
3160459cc2c6SThierry Reding 			helpers = &tegra_sor_edp_helpers;
3161459cc2c6SThierry Reding 		} else if (sor->soc->supports_dp) {
3162459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_DisplayPort;
3163459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
31640472c21bSThierry Reding 			helpers = &tegra_sor_dp_helpers;
3165459cc2c6SThierry Reding 		}
3166c1763937SThierry Reding 
3167c1763937SThierry Reding 		sor->link.ops = &tegra_sor_dp_link_ops;
3168c1763937SThierry Reding 		sor->link.aux = sor->aux;
3169459cc2c6SThierry Reding 	}
31706b6b6042SThierry Reding 
31716b6b6042SThierry Reding 	sor->output.dev = sor->dev;
31726b6b6042SThierry Reding 
31736fad8f66SThierry Reding 	drm_connector_init(drm, &sor->output.connector,
31746fad8f66SThierry Reding 			   &tegra_sor_connector_funcs,
3175459cc2c6SThierry Reding 			   connector);
31766fad8f66SThierry Reding 	drm_connector_helper_add(&sor->output.connector,
31776fad8f66SThierry Reding 				 &tegra_sor_connector_helper_funcs);
31786fad8f66SThierry Reding 	sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
31796fad8f66SThierry Reding 
31806fad8f66SThierry Reding 	drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
318113a3d91fSVille Syrjälä 			 encoder, NULL);
3182459cc2c6SThierry Reding 	drm_encoder_helper_add(&sor->output.encoder, helpers);
31836fad8f66SThierry Reding 
3184cde4c44dSDaniel Vetter 	drm_connector_attach_encoder(&sor->output.connector,
31856fad8f66SThierry Reding 					  &sor->output.encoder);
31866fad8f66SThierry Reding 	drm_connector_register(&sor->output.connector);
31876fad8f66SThierry Reding 
3188ea130b24SThierry Reding 	err = tegra_output_init(drm, &sor->output);
3189ea130b24SThierry Reding 	if (err < 0) {
3190ea130b24SThierry Reding 		dev_err(client->dev, "failed to initialize output: %d\n", err);
3191ea130b24SThierry Reding 		return err;
3192ea130b24SThierry Reding 	}
31936fad8f66SThierry Reding 
3194c57997bcSThierry Reding 	tegra_output_find_possible_crtcs(&sor->output, drm);
31956b6b6042SThierry Reding 
31969542c237SThierry Reding 	if (sor->aux) {
31979542c237SThierry Reding 		err = drm_dp_aux_attach(sor->aux, &sor->output);
31986b6b6042SThierry Reding 		if (err < 0) {
31996b6b6042SThierry Reding 			dev_err(sor->dev, "failed to attach DP: %d\n", err);
32006b6b6042SThierry Reding 			return err;
32016b6b6042SThierry Reding 		}
32026b6b6042SThierry Reding 	}
32036b6b6042SThierry Reding 
3204535a65dbSTomeu Vizoso 	/*
3205535a65dbSTomeu Vizoso 	 * XXX: Remove this reset once proper hand-over from firmware to
3206535a65dbSTomeu Vizoso 	 * kernel is possible.
3207535a65dbSTomeu Vizoso 	 */
3208f8c79120SJon Hunter 	if (sor->rst) {
320911c632e1SThierry Reding 		err = reset_control_acquire(sor->rst);
321011c632e1SThierry Reding 		if (err < 0) {
321111c632e1SThierry Reding 			dev_err(sor->dev, "failed to acquire SOR reset: %d\n",
321211c632e1SThierry Reding 				err);
321311c632e1SThierry Reding 			return err;
321411c632e1SThierry Reding 		}
321511c632e1SThierry Reding 
3216535a65dbSTomeu Vizoso 		err = reset_control_assert(sor->rst);
3217535a65dbSTomeu Vizoso 		if (err < 0) {
3218f8c79120SJon Hunter 			dev_err(sor->dev, "failed to assert SOR reset: %d\n",
3219f8c79120SJon Hunter 				err);
3220535a65dbSTomeu Vizoso 			return err;
3221535a65dbSTomeu Vizoso 		}
3222f8c79120SJon Hunter 	}
3223535a65dbSTomeu Vizoso 
32246fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk);
32256fad8f66SThierry Reding 	if (err < 0) {
32266fad8f66SThierry Reding 		dev_err(sor->dev, "failed to enable clock: %d\n", err);
32276fad8f66SThierry Reding 		return err;
32286fad8f66SThierry Reding 	}
32296fad8f66SThierry Reding 
3230535a65dbSTomeu Vizoso 	usleep_range(1000, 3000);
3231535a65dbSTomeu Vizoso 
3232f8c79120SJon Hunter 	if (sor->rst) {
3233535a65dbSTomeu Vizoso 		err = reset_control_deassert(sor->rst);
3234535a65dbSTomeu Vizoso 		if (err < 0) {
3235f8c79120SJon Hunter 			dev_err(sor->dev, "failed to deassert SOR reset: %d\n",
3236f8c79120SJon Hunter 				err);
3237535a65dbSTomeu Vizoso 			return err;
3238535a65dbSTomeu Vizoso 		}
323911c632e1SThierry Reding 
324011c632e1SThierry Reding 		reset_control_release(sor->rst);
3241f8c79120SJon Hunter 	}
3242535a65dbSTomeu Vizoso 
32436fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk_safe);
32446fad8f66SThierry Reding 	if (err < 0)
32456fad8f66SThierry Reding 		return err;
32466fad8f66SThierry Reding 
32476fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk_dp);
32486fad8f66SThierry Reding 	if (err < 0)
32496fad8f66SThierry Reding 		return err;
32506fad8f66SThierry Reding 
32516b6b6042SThierry Reding 	return 0;
32526b6b6042SThierry Reding }
32536b6b6042SThierry Reding 
32546b6b6042SThierry Reding static int tegra_sor_exit(struct host1x_client *client)
32556b6b6042SThierry Reding {
32566b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
32576b6b6042SThierry Reding 	int err;
32586b6b6042SThierry Reding 
3259328ec69eSThierry Reding 	tegra_output_exit(&sor->output);
3260328ec69eSThierry Reding 
32619542c237SThierry Reding 	if (sor->aux) {
32629542c237SThierry Reding 		err = drm_dp_aux_detach(sor->aux);
32636b6b6042SThierry Reding 		if (err < 0) {
32646b6b6042SThierry Reding 			dev_err(sor->dev, "failed to detach DP: %d\n", err);
32656b6b6042SThierry Reding 			return err;
32666b6b6042SThierry Reding 		}
32676b6b6042SThierry Reding 	}
32686b6b6042SThierry Reding 
32696fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk_safe);
32706fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk_dp);
32716fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk);
32726fad8f66SThierry Reding 
32736b6b6042SThierry Reding 	return 0;
32746b6b6042SThierry Reding }
32756b6b6042SThierry Reding 
32766b6b6042SThierry Reding static const struct host1x_client_ops sor_client_ops = {
32776b6b6042SThierry Reding 	.init = tegra_sor_init,
32786b6b6042SThierry Reding 	.exit = tegra_sor_exit,
32796b6b6042SThierry Reding };
32806b6b6042SThierry Reding 
3281459cc2c6SThierry Reding static const struct tegra_sor_ops tegra_sor_edp_ops = {
3282459cc2c6SThierry Reding 	.name = "eDP",
3283459cc2c6SThierry Reding };
3284459cc2c6SThierry Reding 
3285459cc2c6SThierry Reding static int tegra_sor_hdmi_probe(struct tegra_sor *sor)
3286459cc2c6SThierry Reding {
3287459cc2c6SThierry Reding 	int err;
3288459cc2c6SThierry Reding 
3289459cc2c6SThierry Reding 	sor->avdd_io_supply = devm_regulator_get(sor->dev, "avdd-io");
3290459cc2c6SThierry Reding 	if (IS_ERR(sor->avdd_io_supply)) {
3291459cc2c6SThierry Reding 		dev_err(sor->dev, "cannot get AVDD I/O supply: %ld\n",
3292459cc2c6SThierry Reding 			PTR_ERR(sor->avdd_io_supply));
3293459cc2c6SThierry Reding 		return PTR_ERR(sor->avdd_io_supply);
3294459cc2c6SThierry Reding 	}
3295459cc2c6SThierry Reding 
3296459cc2c6SThierry Reding 	err = regulator_enable(sor->avdd_io_supply);
3297459cc2c6SThierry Reding 	if (err < 0) {
3298459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable AVDD I/O supply: %d\n",
3299459cc2c6SThierry Reding 			err);
3300459cc2c6SThierry Reding 		return err;
3301459cc2c6SThierry Reding 	}
3302459cc2c6SThierry Reding 
3303459cc2c6SThierry Reding 	sor->vdd_pll_supply = devm_regulator_get(sor->dev, "vdd-pll");
3304459cc2c6SThierry Reding 	if (IS_ERR(sor->vdd_pll_supply)) {
3305459cc2c6SThierry Reding 		dev_err(sor->dev, "cannot get VDD PLL supply: %ld\n",
3306459cc2c6SThierry Reding 			PTR_ERR(sor->vdd_pll_supply));
3307459cc2c6SThierry Reding 		return PTR_ERR(sor->vdd_pll_supply);
3308459cc2c6SThierry Reding 	}
3309459cc2c6SThierry Reding 
3310459cc2c6SThierry Reding 	err = regulator_enable(sor->vdd_pll_supply);
3311459cc2c6SThierry Reding 	if (err < 0) {
3312459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable VDD PLL supply: %d\n",
3313459cc2c6SThierry Reding 			err);
3314459cc2c6SThierry Reding 		return err;
3315459cc2c6SThierry Reding 	}
3316459cc2c6SThierry Reding 
3317459cc2c6SThierry Reding 	sor->hdmi_supply = devm_regulator_get(sor->dev, "hdmi");
3318459cc2c6SThierry Reding 	if (IS_ERR(sor->hdmi_supply)) {
3319459cc2c6SThierry Reding 		dev_err(sor->dev, "cannot get HDMI supply: %ld\n",
3320459cc2c6SThierry Reding 			PTR_ERR(sor->hdmi_supply));
3321459cc2c6SThierry Reding 		return PTR_ERR(sor->hdmi_supply);
3322459cc2c6SThierry Reding 	}
3323459cc2c6SThierry Reding 
3324459cc2c6SThierry Reding 	err = regulator_enable(sor->hdmi_supply);
3325459cc2c6SThierry Reding 	if (err < 0) {
3326459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable HDMI supply: %d\n", err);
3327459cc2c6SThierry Reding 		return err;
3328459cc2c6SThierry Reding 	}
3329459cc2c6SThierry Reding 
333036e90221SThierry Reding 	INIT_DELAYED_WORK(&sor->scdc, tegra_sor_hdmi_scdc_work);
333136e90221SThierry Reding 
3332459cc2c6SThierry Reding 	return 0;
3333459cc2c6SThierry Reding }
3334459cc2c6SThierry Reding 
3335459cc2c6SThierry Reding static int tegra_sor_hdmi_remove(struct tegra_sor *sor)
3336459cc2c6SThierry Reding {
3337459cc2c6SThierry Reding 	regulator_disable(sor->hdmi_supply);
3338459cc2c6SThierry Reding 	regulator_disable(sor->vdd_pll_supply);
3339459cc2c6SThierry Reding 	regulator_disable(sor->avdd_io_supply);
3340459cc2c6SThierry Reding 
3341459cc2c6SThierry Reding 	return 0;
3342459cc2c6SThierry Reding }
3343459cc2c6SThierry Reding 
3344459cc2c6SThierry Reding static const struct tegra_sor_ops tegra_sor_hdmi_ops = {
3345459cc2c6SThierry Reding 	.name = "HDMI",
3346459cc2c6SThierry Reding 	.probe = tegra_sor_hdmi_probe,
3347459cc2c6SThierry Reding 	.remove = tegra_sor_hdmi_remove,
3348459cc2c6SThierry Reding };
3349459cc2c6SThierry Reding 
33500472c21bSThierry Reding static int tegra_sor_dp_probe(struct tegra_sor *sor)
33510472c21bSThierry Reding {
33520472c21bSThierry Reding 	int err;
33530472c21bSThierry Reding 
33540472c21bSThierry Reding 	sor->avdd_io_supply = devm_regulator_get(sor->dev, "avdd-io-hdmi-dp");
33550472c21bSThierry Reding 	if (IS_ERR(sor->avdd_io_supply))
33560472c21bSThierry Reding 		return PTR_ERR(sor->avdd_io_supply);
33570472c21bSThierry Reding 
33580472c21bSThierry Reding 	err = regulator_enable(sor->avdd_io_supply);
33590472c21bSThierry Reding 	if (err < 0)
33600472c21bSThierry Reding 		return err;
33610472c21bSThierry Reding 
33620472c21bSThierry Reding 	sor->vdd_pll_supply = devm_regulator_get(sor->dev, "vdd-hdmi-dp-pll");
33630472c21bSThierry Reding 	if (IS_ERR(sor->vdd_pll_supply))
33640472c21bSThierry Reding 		return PTR_ERR(sor->vdd_pll_supply);
33650472c21bSThierry Reding 
33660472c21bSThierry Reding 	err = regulator_enable(sor->vdd_pll_supply);
33670472c21bSThierry Reding 	if (err < 0)
33680472c21bSThierry Reding 		return err;
33690472c21bSThierry Reding 
33700472c21bSThierry Reding 	return 0;
33710472c21bSThierry Reding }
33720472c21bSThierry Reding 
33730472c21bSThierry Reding static int tegra_sor_dp_remove(struct tegra_sor *sor)
33740472c21bSThierry Reding {
33750472c21bSThierry Reding 	regulator_disable(sor->vdd_pll_supply);
33760472c21bSThierry Reding 	regulator_disable(sor->avdd_io_supply);
33770472c21bSThierry Reding 
33780472c21bSThierry Reding 	return 0;
33790472c21bSThierry Reding }
33800472c21bSThierry Reding 
33810472c21bSThierry Reding static const struct tegra_sor_ops tegra_sor_dp_ops = {
33820472c21bSThierry Reding 	.name = "DP",
33830472c21bSThierry Reding 	.probe = tegra_sor_dp_probe,
33840472c21bSThierry Reding 	.remove = tegra_sor_dp_remove,
33850472c21bSThierry Reding };
33860472c21bSThierry Reding 
338730b49435SThierry Reding static const u8 tegra124_sor_xbar_cfg[5] = {
338830b49435SThierry Reding 	0, 1, 2, 3, 4
338930b49435SThierry Reding };
339030b49435SThierry Reding 
3391880cee0bSThierry Reding static const struct tegra_sor_regs tegra124_sor_regs = {
3392880cee0bSThierry Reding 	.head_state0 = 0x05,
3393880cee0bSThierry Reding 	.head_state1 = 0x07,
3394880cee0bSThierry Reding 	.head_state2 = 0x09,
3395880cee0bSThierry Reding 	.head_state3 = 0x0b,
3396880cee0bSThierry Reding 	.head_state4 = 0x0d,
3397880cee0bSThierry Reding 	.head_state5 = 0x0f,
3398880cee0bSThierry Reding 	.pll0 = 0x17,
3399880cee0bSThierry Reding 	.pll1 = 0x18,
3400880cee0bSThierry Reding 	.pll2 = 0x19,
3401880cee0bSThierry Reding 	.pll3 = 0x1a,
3402880cee0bSThierry Reding 	.dp_padctl0 = 0x5c,
3403880cee0bSThierry Reding 	.dp_padctl2 = 0x73,
3404880cee0bSThierry Reding };
3405880cee0bSThierry Reding 
3406c1763937SThierry Reding /* Tegra124 and Tegra132 have lanes 0 and 2 swapped. */
3407c1763937SThierry Reding static const u8 tegra124_sor_lane_map[4] = {
3408c1763937SThierry Reding 	2, 1, 0, 3,
3409c1763937SThierry Reding };
3410c1763937SThierry Reding 
3411c1763937SThierry Reding static const u8 tegra124_sor_voltage_swing[4][4][4] = {
3412c1763937SThierry Reding 	{
3413c1763937SThierry Reding 		{ 0x13, 0x19, 0x1e, 0x28 },
3414c1763937SThierry Reding 		{ 0x1e, 0x25, 0x2d, },
3415c1763937SThierry Reding 		{ 0x28, 0x32, },
3416c1763937SThierry Reding 		{ 0x3c, },
3417c1763937SThierry Reding 	}, {
3418c1763937SThierry Reding 		{ 0x12, 0x17, 0x1b, 0x25 },
3419c1763937SThierry Reding 		{ 0x1c, 0x23, 0x2a, },
3420c1763937SThierry Reding 		{ 0x25, 0x2f, },
3421c1763937SThierry Reding 		{ 0x39, }
3422c1763937SThierry Reding 	}, {
3423c1763937SThierry Reding 		{ 0x12, 0x16, 0x1a, 0x22 },
3424c1763937SThierry Reding 		{ 0x1b, 0x20, 0x27, },
3425c1763937SThierry Reding 		{ 0x24, 0x2d, },
3426c1763937SThierry Reding 		{ 0x36, },
3427c1763937SThierry Reding 	}, {
3428c1763937SThierry Reding 		{ 0x11, 0x14, 0x17, 0x1f },
3429c1763937SThierry Reding 		{ 0x19, 0x1e, 0x24, },
3430c1763937SThierry Reding 		{ 0x22, 0x2a, },
3431c1763937SThierry Reding 		{ 0x32, },
3432c1763937SThierry Reding 	},
3433c1763937SThierry Reding };
3434c1763937SThierry Reding 
3435c1763937SThierry Reding static const u8 tegra124_sor_pre_emphasis[4][4][4] = {
3436c1763937SThierry Reding 	{
3437c1763937SThierry Reding 		{ 0x00, 0x09, 0x13, 0x25 },
3438c1763937SThierry Reding 		{ 0x00, 0x0f, 0x1e, },
3439c1763937SThierry Reding 		{ 0x00, 0x14, },
3440c1763937SThierry Reding 		{ 0x00, },
3441c1763937SThierry Reding 	}, {
3442c1763937SThierry Reding 		{ 0x00, 0x0a, 0x14, 0x28 },
3443c1763937SThierry Reding 		{ 0x00, 0x0f, 0x1e, },
3444c1763937SThierry Reding 		{ 0x00, 0x14, },
3445c1763937SThierry Reding 		{ 0x00 },
3446c1763937SThierry Reding 	}, {
3447c1763937SThierry Reding 		{ 0x00, 0x0a, 0x14, 0x28 },
3448c1763937SThierry Reding 		{ 0x00, 0x0f, 0x1e, },
3449c1763937SThierry Reding 		{ 0x00, 0x14, },
3450c1763937SThierry Reding 		{ 0x00, },
3451c1763937SThierry Reding 	}, {
3452c1763937SThierry Reding 		{ 0x00, 0x0a, 0x14, 0x28 },
3453c1763937SThierry Reding 		{ 0x00, 0x0f, 0x1e, },
3454c1763937SThierry Reding 		{ 0x00, 0x14, },
3455c1763937SThierry Reding 		{ 0x00, },
3456c1763937SThierry Reding 	},
3457c1763937SThierry Reding };
3458c1763937SThierry Reding 
3459c1763937SThierry Reding static const u8 tegra124_sor_post_cursor[4][4][4] = {
3460c1763937SThierry Reding 	{
3461c1763937SThierry Reding 		{ 0x00, 0x00, 0x00, 0x00 },
3462c1763937SThierry Reding 		{ 0x00, 0x00, 0x00, },
3463c1763937SThierry Reding 		{ 0x00, 0x00, },
3464c1763937SThierry Reding 		{ 0x00, },
3465c1763937SThierry Reding 	}, {
3466c1763937SThierry Reding 		{ 0x02, 0x02, 0x04, 0x05 },
3467c1763937SThierry Reding 		{ 0x02, 0x04, 0x05, },
3468c1763937SThierry Reding 		{ 0x04, 0x05, },
3469c1763937SThierry Reding 		{ 0x05, },
3470c1763937SThierry Reding 	}, {
3471c1763937SThierry Reding 		{ 0x04, 0x05, 0x08, 0x0b },
3472c1763937SThierry Reding 		{ 0x05, 0x09, 0x0b, },
3473c1763937SThierry Reding 		{ 0x08, 0x0a, },
3474c1763937SThierry Reding 		{ 0x0b, },
3475c1763937SThierry Reding 	}, {
3476c1763937SThierry Reding 		{ 0x05, 0x09, 0x0b, 0x12 },
3477c1763937SThierry Reding 		{ 0x09, 0x0d, 0x12, },
3478c1763937SThierry Reding 		{ 0x0b, 0x0f, },
3479c1763937SThierry Reding 		{ 0x12, },
3480c1763937SThierry Reding 	},
3481c1763937SThierry Reding };
3482c1763937SThierry Reding 
3483c1763937SThierry Reding static const u8 tegra124_sor_tx_pu[4][4][4] = {
3484c1763937SThierry Reding 	{
3485c1763937SThierry Reding 		{ 0x20, 0x30, 0x40, 0x60 },
3486c1763937SThierry Reding 		{ 0x30, 0x40, 0x60, },
3487c1763937SThierry Reding 		{ 0x40, 0x60, },
3488c1763937SThierry Reding 		{ 0x60, },
3489c1763937SThierry Reding 	}, {
3490c1763937SThierry Reding 		{ 0x20, 0x20, 0x30, 0x50 },
3491c1763937SThierry Reding 		{ 0x30, 0x40, 0x50, },
3492c1763937SThierry Reding 		{ 0x40, 0x50, },
3493c1763937SThierry Reding 		{ 0x60, },
3494c1763937SThierry Reding 	}, {
3495c1763937SThierry Reding 		{ 0x20, 0x20, 0x30, 0x40, },
3496c1763937SThierry Reding 		{ 0x30, 0x30, 0x40, },
3497c1763937SThierry Reding 		{ 0x40, 0x50, },
3498c1763937SThierry Reding 		{ 0x60, },
3499c1763937SThierry Reding 	}, {
3500c1763937SThierry Reding 		{ 0x20, 0x20, 0x20, 0x40, },
3501c1763937SThierry Reding 		{ 0x30, 0x30, 0x40, },
3502c1763937SThierry Reding 		{ 0x40, 0x40, },
3503c1763937SThierry Reding 		{ 0x60, },
3504c1763937SThierry Reding 	},
3505c1763937SThierry Reding };
3506c1763937SThierry Reding 
3507459cc2c6SThierry Reding static const struct tegra_sor_soc tegra124_sor = {
3508459cc2c6SThierry Reding 	.supports_edp = true,
3509459cc2c6SThierry Reding 	.supports_lvds = true,
3510459cc2c6SThierry Reding 	.supports_hdmi = false,
3511459cc2c6SThierry Reding 	.supports_dp = false,
3512880cee0bSThierry Reding 	.regs = &tegra124_sor_regs,
3513c57997bcSThierry Reding 	.has_nvdisplay = false,
351430b49435SThierry Reding 	.xbar_cfg = tegra124_sor_xbar_cfg,
3515c1763937SThierry Reding 	.lane_map = tegra124_sor_lane_map,
3516c1763937SThierry Reding 	.voltage_swing = tegra124_sor_voltage_swing,
3517c1763937SThierry Reding 	.pre_emphasis = tegra124_sor_pre_emphasis,
3518c1763937SThierry Reding 	.post_cursor = tegra124_sor_post_cursor,
3519c1763937SThierry Reding 	.tx_pu = tegra124_sor_tx_pu,
3520c1763937SThierry Reding };
3521c1763937SThierry Reding 
3522c1763937SThierry Reding static const u8 tegra132_sor_pre_emphasis[4][4][4] = {
3523c1763937SThierry Reding 	{
3524c1763937SThierry Reding 		{ 0x00, 0x08, 0x12, 0x24 },
3525c1763937SThierry Reding 		{ 0x01, 0x0e, 0x1d, },
3526c1763937SThierry Reding 		{ 0x01, 0x13, },
3527c1763937SThierry Reding 		{ 0x00, },
3528c1763937SThierry Reding 	}, {
3529c1763937SThierry Reding 		{ 0x00, 0x08, 0x12, 0x24 },
3530c1763937SThierry Reding 		{ 0x00, 0x0e, 0x1d, },
3531c1763937SThierry Reding 		{ 0x00, 0x13, },
3532c1763937SThierry Reding 		{ 0x00 },
3533c1763937SThierry Reding 	}, {
3534c1763937SThierry Reding 		{ 0x00, 0x08, 0x12, 0x24 },
3535c1763937SThierry Reding 		{ 0x00, 0x0e, 0x1d, },
3536c1763937SThierry Reding 		{ 0x00, 0x13, },
3537c1763937SThierry Reding 		{ 0x00, },
3538c1763937SThierry Reding 	}, {
3539c1763937SThierry Reding 		{ 0x00, 0x08, 0x12, 0x24 },
3540c1763937SThierry Reding 		{ 0x00, 0x0e, 0x1d, },
3541c1763937SThierry Reding 		{ 0x00, 0x13, },
3542c1763937SThierry Reding 		{ 0x00, },
3543c1763937SThierry Reding 	},
3544c1763937SThierry Reding };
3545c1763937SThierry Reding 
3546c1763937SThierry Reding static const struct tegra_sor_soc tegra132_sor = {
3547c1763937SThierry Reding 	.supports_edp = true,
3548c1763937SThierry Reding 	.supports_lvds = true,
3549c1763937SThierry Reding 	.supports_hdmi = false,
3550c1763937SThierry Reding 	.supports_dp = false,
3551c1763937SThierry Reding 	.regs = &tegra124_sor_regs,
3552c1763937SThierry Reding 	.has_nvdisplay = false,
3553c1763937SThierry Reding 	.xbar_cfg = tegra124_sor_xbar_cfg,
3554c1763937SThierry Reding 	.lane_map = tegra124_sor_lane_map,
3555c1763937SThierry Reding 	.voltage_swing = tegra124_sor_voltage_swing,
3556c1763937SThierry Reding 	.pre_emphasis = tegra132_sor_pre_emphasis,
3557c1763937SThierry Reding 	.post_cursor = tegra124_sor_post_cursor,
3558c1763937SThierry Reding 	.tx_pu = tegra124_sor_tx_pu,
3559459cc2c6SThierry Reding };
3560459cc2c6SThierry Reding 
3561880cee0bSThierry Reding static const struct tegra_sor_regs tegra210_sor_regs = {
3562880cee0bSThierry Reding 	.head_state0 = 0x05,
3563880cee0bSThierry Reding 	.head_state1 = 0x07,
3564880cee0bSThierry Reding 	.head_state2 = 0x09,
3565880cee0bSThierry Reding 	.head_state3 = 0x0b,
3566880cee0bSThierry Reding 	.head_state4 = 0x0d,
3567880cee0bSThierry Reding 	.head_state5 = 0x0f,
3568880cee0bSThierry Reding 	.pll0 = 0x17,
3569880cee0bSThierry Reding 	.pll1 = 0x18,
3570880cee0bSThierry Reding 	.pll2 = 0x19,
3571880cee0bSThierry Reding 	.pll3 = 0x1a,
3572880cee0bSThierry Reding 	.dp_padctl0 = 0x5c,
3573880cee0bSThierry Reding 	.dp_padctl2 = 0x73,
3574880cee0bSThierry Reding };
3575880cee0bSThierry Reding 
3576c1763937SThierry Reding static const u8 tegra210_sor_xbar_cfg[5] = {
3577c1763937SThierry Reding 	2, 1, 0, 3, 4
3578c1763937SThierry Reding };
3579c1763937SThierry Reding 
35800472c21bSThierry Reding static const u8 tegra210_sor_lane_map[4] = {
35810472c21bSThierry Reding 	0, 1, 2, 3,
35820472c21bSThierry Reding };
35830472c21bSThierry Reding 
3584459cc2c6SThierry Reding static const struct tegra_sor_soc tegra210_sor = {
3585459cc2c6SThierry Reding 	.supports_edp = true,
3586459cc2c6SThierry Reding 	.supports_lvds = false,
3587459cc2c6SThierry Reding 	.supports_hdmi = false,
3588459cc2c6SThierry Reding 	.supports_dp = false,
3589c1763937SThierry Reding 
3590880cee0bSThierry Reding 	.regs = &tegra210_sor_regs,
3591c57997bcSThierry Reding 	.has_nvdisplay = false,
359230b49435SThierry Reding 
3593c1763937SThierry Reding 	.xbar_cfg = tegra210_sor_xbar_cfg,
35940472c21bSThierry Reding 	.lane_map = tegra210_sor_lane_map,
35950472c21bSThierry Reding 	.voltage_swing = tegra124_sor_voltage_swing,
35960472c21bSThierry Reding 	.pre_emphasis = tegra124_sor_pre_emphasis,
35970472c21bSThierry Reding 	.post_cursor = tegra124_sor_post_cursor,
35980472c21bSThierry Reding 	.tx_pu = tegra124_sor_tx_pu,
3599459cc2c6SThierry Reding };
3600459cc2c6SThierry Reding 
3601459cc2c6SThierry Reding static const struct tegra_sor_soc tegra210_sor1 = {
3602459cc2c6SThierry Reding 	.supports_edp = false,
3603459cc2c6SThierry Reding 	.supports_lvds = false,
3604459cc2c6SThierry Reding 	.supports_hdmi = true,
3605459cc2c6SThierry Reding 	.supports_dp = true,
3606459cc2c6SThierry Reding 
3607880cee0bSThierry Reding 	.regs = &tegra210_sor_regs,
3608c57997bcSThierry Reding 	.has_nvdisplay = false,
3609880cee0bSThierry Reding 
3610459cc2c6SThierry Reding 	.num_settings = ARRAY_SIZE(tegra210_sor_hdmi_defaults),
3611459cc2c6SThierry Reding 	.settings = tegra210_sor_hdmi_defaults,
361230b49435SThierry Reding 	.xbar_cfg = tegra210_sor_xbar_cfg,
36130472c21bSThierry Reding 	.lane_map = tegra210_sor_lane_map,
36140472c21bSThierry Reding 	.voltage_swing = tegra124_sor_voltage_swing,
36150472c21bSThierry Reding 	.pre_emphasis = tegra124_sor_pre_emphasis,
36160472c21bSThierry Reding 	.post_cursor = tegra124_sor_post_cursor,
36170472c21bSThierry Reding 	.tx_pu = tegra124_sor_tx_pu,
3618459cc2c6SThierry Reding };
3619459cc2c6SThierry Reding 
3620c57997bcSThierry Reding static const struct tegra_sor_regs tegra186_sor_regs = {
3621c57997bcSThierry Reding 	.head_state0 = 0x151,
3622c57997bcSThierry Reding 	.head_state1 = 0x154,
3623c57997bcSThierry Reding 	.head_state2 = 0x157,
3624c57997bcSThierry Reding 	.head_state3 = 0x15a,
3625c57997bcSThierry Reding 	.head_state4 = 0x15d,
3626c57997bcSThierry Reding 	.head_state5 = 0x160,
3627c57997bcSThierry Reding 	.pll0 = 0x163,
3628c57997bcSThierry Reding 	.pll1 = 0x164,
3629c57997bcSThierry Reding 	.pll2 = 0x165,
3630c57997bcSThierry Reding 	.pll3 = 0x166,
3631c57997bcSThierry Reding 	.dp_padctl0 = 0x168,
3632c57997bcSThierry Reding 	.dp_padctl2 = 0x16a,
3633c57997bcSThierry Reding };
3634c57997bcSThierry Reding 
36350472c21bSThierry Reding static const u8 tegra186_sor_voltage_swing[4][4][4] = {
36360472c21bSThierry Reding 	{
36370472c21bSThierry Reding 		{ 0x13, 0x19, 0x1e, 0x28 },
36380472c21bSThierry Reding 		{ 0x1e, 0x25, 0x2d, },
36390472c21bSThierry Reding 		{ 0x28, 0x32, },
36400472c21bSThierry Reding 		{ 0x39, },
36410472c21bSThierry Reding 	}, {
36420472c21bSThierry Reding 		{ 0x12, 0x16, 0x1b, 0x25 },
36430472c21bSThierry Reding 		{ 0x1c, 0x23, 0x2a, },
36440472c21bSThierry Reding 		{ 0x25, 0x2f, },
36450472c21bSThierry Reding 		{ 0x37, }
36460472c21bSThierry Reding 	}, {
36470472c21bSThierry Reding 		{ 0x12, 0x16, 0x1a, 0x22 },
36480472c21bSThierry Reding 		{ 0x1b, 0x20, 0x27, },
36490472c21bSThierry Reding 		{ 0x24, 0x2d, },
36500472c21bSThierry Reding 		{ 0x35, },
36510472c21bSThierry Reding 	}, {
36520472c21bSThierry Reding 		{ 0x11, 0x14, 0x17, 0x1f },
36530472c21bSThierry Reding 		{ 0x19, 0x1e, 0x24, },
36540472c21bSThierry Reding 		{ 0x22, 0x2a, },
36550472c21bSThierry Reding 		{ 0x32, },
36560472c21bSThierry Reding 	},
36570472c21bSThierry Reding };
36580472c21bSThierry Reding 
36590472c21bSThierry Reding static const u8 tegra186_sor_pre_emphasis[4][4][4] = {
36600472c21bSThierry Reding 	{
36610472c21bSThierry Reding 		{ 0x00, 0x08, 0x12, 0x24 },
36620472c21bSThierry Reding 		{ 0x01, 0x0e, 0x1d, },
36630472c21bSThierry Reding 		{ 0x01, 0x13, },
36640472c21bSThierry Reding 		{ 0x00, },
36650472c21bSThierry Reding 	}, {
36660472c21bSThierry Reding 		{ 0x00, 0x08, 0x12, 0x24 },
36670472c21bSThierry Reding 		{ 0x00, 0x0e, 0x1d, },
36680472c21bSThierry Reding 		{ 0x00, 0x13, },
36690472c21bSThierry Reding 		{ 0x00 },
36700472c21bSThierry Reding 	}, {
36710472c21bSThierry Reding 		{ 0x00, 0x08, 0x14, 0x24 },
36720472c21bSThierry Reding 		{ 0x00, 0x0e, 0x1d, },
36730472c21bSThierry Reding 		{ 0x00, 0x13, },
36740472c21bSThierry Reding 		{ 0x00, },
36750472c21bSThierry Reding 	}, {
36760472c21bSThierry Reding 		{ 0x00, 0x08, 0x12, 0x24 },
36770472c21bSThierry Reding 		{ 0x00, 0x0e, 0x1d, },
36780472c21bSThierry Reding 		{ 0x00, 0x13, },
36790472c21bSThierry Reding 		{ 0x00, },
36800472c21bSThierry Reding 	},
36810472c21bSThierry Reding };
36820472c21bSThierry Reding 
3683c57997bcSThierry Reding static const struct tegra_sor_soc tegra186_sor = {
3684c57997bcSThierry Reding 	.supports_edp = false,
3685c57997bcSThierry Reding 	.supports_lvds = false,
3686c57997bcSThierry Reding 	.supports_hdmi = true,
3687c57997bcSThierry Reding 	.supports_dp = true,
3688c57997bcSThierry Reding 
3689c57997bcSThierry Reding 	.regs = &tegra186_sor_regs,
3690c57997bcSThierry Reding 	.has_nvdisplay = true,
3691c57997bcSThierry Reding 
3692c57997bcSThierry Reding 	.num_settings = ARRAY_SIZE(tegra186_sor_hdmi_defaults),
3693c57997bcSThierry Reding 	.settings = tegra186_sor_hdmi_defaults,
3694c57997bcSThierry Reding 	.xbar_cfg = tegra124_sor_xbar_cfg,
36950472c21bSThierry Reding 	.lane_map = tegra124_sor_lane_map,
36960472c21bSThierry Reding 	.voltage_swing = tegra186_sor_voltage_swing,
36970472c21bSThierry Reding 	.pre_emphasis = tegra186_sor_pre_emphasis,
36980472c21bSThierry Reding 	.post_cursor = tegra124_sor_post_cursor,
36990472c21bSThierry Reding 	.tx_pu = tegra124_sor_tx_pu,
3700c57997bcSThierry Reding };
3701c57997bcSThierry Reding 
37029b6c14b8SThierry Reding static const struct tegra_sor_regs tegra194_sor_regs = {
37039b6c14b8SThierry Reding 	.head_state0 = 0x151,
37049b6c14b8SThierry Reding 	.head_state1 = 0x155,
37059b6c14b8SThierry Reding 	.head_state2 = 0x159,
37069b6c14b8SThierry Reding 	.head_state3 = 0x15d,
37079b6c14b8SThierry Reding 	.head_state4 = 0x161,
37089b6c14b8SThierry Reding 	.head_state5 = 0x165,
37099b6c14b8SThierry Reding 	.pll0 = 0x169,
37109b6c14b8SThierry Reding 	.pll1 = 0x16a,
37119b6c14b8SThierry Reding 	.pll2 = 0x16b,
37129b6c14b8SThierry Reding 	.pll3 = 0x16c,
37139b6c14b8SThierry Reding 	.dp_padctl0 = 0x16e,
37149b6c14b8SThierry Reding 	.dp_padctl2 = 0x16f,
37159b6c14b8SThierry Reding };
37169b6c14b8SThierry Reding 
37179b6c14b8SThierry Reding static const struct tegra_sor_soc tegra194_sor = {
37189b6c14b8SThierry Reding 	.supports_edp = true,
37199b6c14b8SThierry Reding 	.supports_lvds = false,
37209b6c14b8SThierry Reding 	.supports_hdmi = true,
37219b6c14b8SThierry Reding 	.supports_dp = true,
37229b6c14b8SThierry Reding 
37239b6c14b8SThierry Reding 	.regs = &tegra194_sor_regs,
37249b6c14b8SThierry Reding 	.has_nvdisplay = true,
37259b6c14b8SThierry Reding 
37269b6c14b8SThierry Reding 	.num_settings = ARRAY_SIZE(tegra194_sor_hdmi_defaults),
37279b6c14b8SThierry Reding 	.settings = tegra194_sor_hdmi_defaults,
37289b6c14b8SThierry Reding 
37299b6c14b8SThierry Reding 	.xbar_cfg = tegra210_sor_xbar_cfg,
37309b6c14b8SThierry Reding };
37319b6c14b8SThierry Reding 
3732459cc2c6SThierry Reding static const struct of_device_id tegra_sor_of_match[] = {
37339b6c14b8SThierry Reding 	{ .compatible = "nvidia,tegra194-sor", .data = &tegra194_sor },
3734c57997bcSThierry Reding 	{ .compatible = "nvidia,tegra186-sor", .data = &tegra186_sor },
3735459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra210-sor1", .data = &tegra210_sor1 },
3736459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra210-sor", .data = &tegra210_sor },
3737c1763937SThierry Reding 	{ .compatible = "nvidia,tegra132-sor", .data = &tegra132_sor },
3738459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra124-sor", .data = &tegra124_sor },
3739459cc2c6SThierry Reding 	{ },
3740459cc2c6SThierry Reding };
3741459cc2c6SThierry Reding MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
3742459cc2c6SThierry Reding 
3743c57997bcSThierry Reding static int tegra_sor_parse_dt(struct tegra_sor *sor)
3744c57997bcSThierry Reding {
3745c57997bcSThierry Reding 	struct device_node *np = sor->dev->of_node;
37466d6c815dSThierry Reding 	u32 xbar_cfg[5];
37476d6c815dSThierry Reding 	unsigned int i;
3748c57997bcSThierry Reding 	u32 value;
3749c57997bcSThierry Reding 	int err;
3750c57997bcSThierry Reding 
3751c57997bcSThierry Reding 	if (sor->soc->has_nvdisplay) {
3752c57997bcSThierry Reding 		err = of_property_read_u32(np, "nvidia,interface", &value);
3753c57997bcSThierry Reding 		if (err < 0)
3754c57997bcSThierry Reding 			return err;
3755c57997bcSThierry Reding 
3756c57997bcSThierry Reding 		sor->index = value;
3757c57997bcSThierry Reding 
3758c57997bcSThierry Reding 		/*
3759c57997bcSThierry Reding 		 * override the default that we already set for Tegra210 and
3760c57997bcSThierry Reding 		 * earlier
3761c57997bcSThierry Reding 		 */
3762c57997bcSThierry Reding 		sor->pad = TEGRA_IO_PAD_HDMI_DP0 + sor->index;
376324e64f86SThierry Reding 	} else {
376424e64f86SThierry Reding 		if (sor->soc->supports_edp)
376524e64f86SThierry Reding 			sor->index = 0;
376624e64f86SThierry Reding 		else
376724e64f86SThierry Reding 			sor->index = 1;
3768c57997bcSThierry Reding 	}
3769c57997bcSThierry Reding 
37706d6c815dSThierry Reding 	err = of_property_read_u32_array(np, "nvidia,xbar-cfg", xbar_cfg, 5);
37716d6c815dSThierry Reding 	if (err < 0) {
37726d6c815dSThierry Reding 		/* fall back to default per-SoC XBAR configuration */
37736d6c815dSThierry Reding 		for (i = 0; i < 5; i++)
37746d6c815dSThierry Reding 			sor->xbar_cfg[i] = sor->soc->xbar_cfg[i];
37756d6c815dSThierry Reding 	} else {
37766d6c815dSThierry Reding 		/* copy cells to SOR XBAR configuration */
37776d6c815dSThierry Reding 		for (i = 0; i < 5; i++)
37786d6c815dSThierry Reding 			sor->xbar_cfg[i] = xbar_cfg[i];
3779c57997bcSThierry Reding 	}
3780c57997bcSThierry Reding 
37816b6b6042SThierry Reding 	return 0;
37828e2988a7SThierry Reding }
37838e2988a7SThierry Reding 
37848e2988a7SThierry Reding static irqreturn_t tegra_sor_irq(int irq, void *data)
37858e2988a7SThierry Reding {
37868e2988a7SThierry Reding 	struct tegra_sor *sor = data;
37878e2988a7SThierry Reding 	u32 value;
37888e2988a7SThierry Reding 
37898e2988a7SThierry Reding 	value = tegra_sor_readl(sor, SOR_INT_STATUS);
37908e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_INT_STATUS);
37918e2988a7SThierry Reding 
37928e2988a7SThierry Reding 	if (value & SOR_INT_CODEC_SCRATCH0) {
37938e2988a7SThierry Reding 		value = tegra_sor_readl(sor, SOR_AUDIO_HDA_CODEC_SCRATCH0);
37948e2988a7SThierry Reding 
37958e2988a7SThierry Reding 		if (value & SOR_AUDIO_HDA_CODEC_SCRATCH0_VALID) {
3796cd54fb96SThierry Reding 			unsigned int format;
37978e2988a7SThierry Reding 
37988e2988a7SThierry Reding 			format = value & SOR_AUDIO_HDA_CODEC_SCRATCH0_FMT_MASK;
37998e2988a7SThierry Reding 
3800fad7b806SThierry Reding 			tegra_hda_parse_format(format, &sor->format);
38018e2988a7SThierry Reding 
38028e2988a7SThierry Reding 			tegra_sor_hdmi_audio_enable(sor);
38038e2988a7SThierry Reding 		} else {
38048e2988a7SThierry Reding 			tegra_sor_hdmi_audio_disable(sor);
38058e2988a7SThierry Reding 		}
38068e2988a7SThierry Reding 	}
38078e2988a7SThierry Reding 
38088e2988a7SThierry Reding 	return IRQ_HANDLED;
38098e2988a7SThierry Reding }
38108e2988a7SThierry Reding 
38116b6b6042SThierry Reding static int tegra_sor_probe(struct platform_device *pdev)
38126b6b6042SThierry Reding {
38136b6b6042SThierry Reding 	struct device_node *np;
38146b6b6042SThierry Reding 	struct tegra_sor *sor;
38156b6b6042SThierry Reding 	struct resource *regs;
38166b6b6042SThierry Reding 	int err;
38176b6b6042SThierry Reding 
38186b6b6042SThierry Reding 	sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
38196b6b6042SThierry Reding 	if (!sor)
38206b6b6042SThierry Reding 		return -ENOMEM;
38216b6b6042SThierry Reding 
38225faea3d0SThierry Reding 	sor->soc = of_device_get_match_data(&pdev->dev);
38236b6b6042SThierry Reding 	sor->output.dev = sor->dev = &pdev->dev;
3824459cc2c6SThierry Reding 
3825459cc2c6SThierry Reding 	sor->settings = devm_kmemdup(&pdev->dev, sor->soc->settings,
3826459cc2c6SThierry Reding 				     sor->soc->num_settings *
3827459cc2c6SThierry Reding 					sizeof(*sor->settings),
3828459cc2c6SThierry Reding 				     GFP_KERNEL);
3829459cc2c6SThierry Reding 	if (!sor->settings)
3830459cc2c6SThierry Reding 		return -ENOMEM;
3831459cc2c6SThierry Reding 
3832459cc2c6SThierry Reding 	sor->num_settings = sor->soc->num_settings;
38336b6b6042SThierry Reding 
38346b6b6042SThierry Reding 	np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
38356b6b6042SThierry Reding 	if (np) {
38369542c237SThierry Reding 		sor->aux = drm_dp_aux_find_by_of_node(np);
38376b6b6042SThierry Reding 		of_node_put(np);
38386b6b6042SThierry Reding 
38399542c237SThierry Reding 		if (!sor->aux)
38406b6b6042SThierry Reding 			return -EPROBE_DEFER;
38416f684de5SThierry Reding 
38426f684de5SThierry Reding 		sor->output.ddc = &sor->aux->ddc;
38436b6b6042SThierry Reding 	}
38446b6b6042SThierry Reding 
38459542c237SThierry Reding 	if (!sor->aux) {
3846459cc2c6SThierry Reding 		if (sor->soc->supports_hdmi) {
3847459cc2c6SThierry Reding 			sor->ops = &tegra_sor_hdmi_ops;
3848c57997bcSThierry Reding 			sor->pad = TEGRA_IO_PAD_HDMI;
3849459cc2c6SThierry Reding 		} else if (sor->soc->supports_lvds) {
3850459cc2c6SThierry Reding 			dev_err(&pdev->dev, "LVDS not supported yet\n");
3851459cc2c6SThierry Reding 			return -ENODEV;
3852459cc2c6SThierry Reding 		} else {
3853459cc2c6SThierry Reding 			dev_err(&pdev->dev, "unknown (non-DP) support\n");
3854459cc2c6SThierry Reding 			return -ENODEV;
3855459cc2c6SThierry Reding 		}
3856459cc2c6SThierry Reding 	} else {
3857459cc2c6SThierry Reding 		if (sor->soc->supports_edp) {
3858459cc2c6SThierry Reding 			sor->ops = &tegra_sor_edp_ops;
3859c57997bcSThierry Reding 			sor->pad = TEGRA_IO_PAD_LVDS;
3860459cc2c6SThierry Reding 		} else if (sor->soc->supports_dp) {
38610472c21bSThierry Reding 			sor->ops = &tegra_sor_dp_ops;
3862459cc2c6SThierry Reding 		} else {
3863459cc2c6SThierry Reding 			dev_err(&pdev->dev, "unknown (DP) support\n");
3864459cc2c6SThierry Reding 			return -ENODEV;
3865459cc2c6SThierry Reding 		}
3866459cc2c6SThierry Reding 	}
3867459cc2c6SThierry Reding 
3868c57997bcSThierry Reding 	err = tegra_sor_parse_dt(sor);
3869c57997bcSThierry Reding 	if (err < 0)
3870c57997bcSThierry Reding 		return err;
3871c57997bcSThierry Reding 
38726b6b6042SThierry Reding 	err = tegra_output_probe(&sor->output);
38734dbdc740SThierry Reding 	if (err < 0) {
38744dbdc740SThierry Reding 		dev_err(&pdev->dev, "failed to probe output: %d\n", err);
38756b6b6042SThierry Reding 		return err;
38764dbdc740SThierry Reding 	}
38776b6b6042SThierry Reding 
3878459cc2c6SThierry Reding 	if (sor->ops && sor->ops->probe) {
3879459cc2c6SThierry Reding 		err = sor->ops->probe(sor);
3880459cc2c6SThierry Reding 		if (err < 0) {
3881459cc2c6SThierry Reding 			dev_err(&pdev->dev, "failed to probe %s: %d\n",
3882459cc2c6SThierry Reding 				sor->ops->name, err);
3883459cc2c6SThierry Reding 			goto output;
3884459cc2c6SThierry Reding 		}
3885459cc2c6SThierry Reding 	}
3886459cc2c6SThierry Reding 
38876b6b6042SThierry Reding 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
38886b6b6042SThierry Reding 	sor->regs = devm_ioremap_resource(&pdev->dev, regs);
3889459cc2c6SThierry Reding 	if (IS_ERR(sor->regs)) {
3890459cc2c6SThierry Reding 		err = PTR_ERR(sor->regs);
3891459cc2c6SThierry Reding 		goto remove;
3892459cc2c6SThierry Reding 	}
38936b6b6042SThierry Reding 
38948e2988a7SThierry Reding 	err = platform_get_irq(pdev, 0);
38958e2988a7SThierry Reding 	if (err < 0) {
38968e2988a7SThierry Reding 		dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
38978e2988a7SThierry Reding 		goto remove;
38988e2988a7SThierry Reding 	}
38998e2988a7SThierry Reding 
39008e2988a7SThierry Reding 	sor->irq = err;
39018e2988a7SThierry Reding 
39028e2988a7SThierry Reding 	err = devm_request_irq(sor->dev, sor->irq, tegra_sor_irq, 0,
39038e2988a7SThierry Reding 			       dev_name(sor->dev), sor);
39048e2988a7SThierry Reding 	if (err < 0) {
39058e2988a7SThierry Reding 		dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
39068e2988a7SThierry Reding 		goto remove;
39078e2988a7SThierry Reding 	}
39088e2988a7SThierry Reding 
390911c632e1SThierry Reding 	sor->rst = devm_reset_control_get_exclusive_released(&pdev->dev, "sor");
39104dbdc740SThierry Reding 	if (IS_ERR(sor->rst)) {
3911459cc2c6SThierry Reding 		err = PTR_ERR(sor->rst);
3912180b46ecSThierry Reding 
3913180b46ecSThierry Reding 		if (err != -EBUSY || WARN_ON(!pdev->dev.pm_domain)) {
3914f8c79120SJon Hunter 			dev_err(&pdev->dev, "failed to get reset control: %d\n",
3915f8c79120SJon Hunter 				err);
3916459cc2c6SThierry Reding 			goto remove;
39174dbdc740SThierry Reding 		}
3918180b46ecSThierry Reding 
3919180b46ecSThierry Reding 		/*
3920180b46ecSThierry Reding 		 * At this point, the reset control is most likely being used
3921180b46ecSThierry Reding 		 * by the generic power domain implementation. With any luck
3922180b46ecSThierry Reding 		 * the power domain will have taken care of resetting the SOR
3923180b46ecSThierry Reding 		 * and we don't have to do anything.
3924180b46ecSThierry Reding 		 */
3925180b46ecSThierry Reding 		sor->rst = NULL;
3926f8c79120SJon Hunter 	}
39276b6b6042SThierry Reding 
39286b6b6042SThierry Reding 	sor->clk = devm_clk_get(&pdev->dev, NULL);
39294dbdc740SThierry Reding 	if (IS_ERR(sor->clk)) {
3930459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk);
3931459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get module clock: %d\n", err);
3932459cc2c6SThierry Reding 		goto remove;
39334dbdc740SThierry Reding 	}
39346b6b6042SThierry Reding 
3935618dee39SThierry Reding 	if (sor->soc->supports_hdmi || sor->soc->supports_dp) {
3936e1335e2fSThierry Reding 		struct device_node *np = pdev->dev.of_node;
3937e1335e2fSThierry Reding 		const char *name;
3938e1335e2fSThierry Reding 
3939e1335e2fSThierry Reding 		/*
3940e1335e2fSThierry Reding 		 * For backwards compatibility with Tegra210 device trees,
3941e1335e2fSThierry Reding 		 * fall back to the old clock name "source" if the new "out"
3942e1335e2fSThierry Reding 		 * clock is not available.
3943e1335e2fSThierry Reding 		 */
3944e1335e2fSThierry Reding 		if (of_property_match_string(np, "clock-names", "out") < 0)
3945e1335e2fSThierry Reding 			name = "source";
3946e1335e2fSThierry Reding 		else
3947e1335e2fSThierry Reding 			name = "out";
3948e1335e2fSThierry Reding 
3949e1335e2fSThierry Reding 		sor->clk_out = devm_clk_get(&pdev->dev, name);
3950e1335e2fSThierry Reding 		if (IS_ERR(sor->clk_out)) {
3951e1335e2fSThierry Reding 			err = PTR_ERR(sor->clk_out);
3952e1335e2fSThierry Reding 			dev_err(sor->dev, "failed to get %s clock: %d\n",
3953e1335e2fSThierry Reding 				name, err);
3954618dee39SThierry Reding 			goto remove;
3955618dee39SThierry Reding 		}
39561087fac1SThierry Reding 	} else {
3957d780537fSThierry Reding 		/* fall back to the module clock on SOR0 (eDP/LVDS only) */
39581087fac1SThierry Reding 		sor->clk_out = sor->clk;
3959618dee39SThierry Reding 	}
3960618dee39SThierry Reding 
39616b6b6042SThierry Reding 	sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
39624dbdc740SThierry Reding 	if (IS_ERR(sor->clk_parent)) {
3963459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_parent);
3964459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get parent clock: %d\n", err);
3965459cc2c6SThierry Reding 		goto remove;
39664dbdc740SThierry Reding 	}
39676b6b6042SThierry Reding 
39686b6b6042SThierry Reding 	sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
39694dbdc740SThierry Reding 	if (IS_ERR(sor->clk_safe)) {
3970459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_safe);
3971459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get safe clock: %d\n", err);
3972459cc2c6SThierry Reding 		goto remove;
39734dbdc740SThierry Reding 	}
39746b6b6042SThierry Reding 
39756b6b6042SThierry Reding 	sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
39764dbdc740SThierry Reding 	if (IS_ERR(sor->clk_dp)) {
3977459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_dp);
3978459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get DP clock: %d\n", err);
3979459cc2c6SThierry Reding 		goto remove;
39804dbdc740SThierry Reding 	}
39816b6b6042SThierry Reding 
3982e1335e2fSThierry Reding 	/*
3983e1335e2fSThierry Reding 	 * Starting with Tegra186, the BPMP provides an implementation for
3984e1335e2fSThierry Reding 	 * the pad output clock, so we have to look it up from device tree.
3985e1335e2fSThierry Reding 	 */
3986e1335e2fSThierry Reding 	sor->clk_pad = devm_clk_get(&pdev->dev, "pad");
3987e1335e2fSThierry Reding 	if (IS_ERR(sor->clk_pad)) {
3988e1335e2fSThierry Reding 		if (sor->clk_pad != ERR_PTR(-ENOENT)) {
3989e1335e2fSThierry Reding 			err = PTR_ERR(sor->clk_pad);
3990e1335e2fSThierry Reding 			goto remove;
3991e1335e2fSThierry Reding 		}
3992e1335e2fSThierry Reding 
3993e1335e2fSThierry Reding 		/*
3994e1335e2fSThierry Reding 		 * If the pad output clock is not available, then we assume
3995e1335e2fSThierry Reding 		 * we're on Tegra210 or earlier and have to provide our own
3996e1335e2fSThierry Reding 		 * implementation.
3997e1335e2fSThierry Reding 		 */
3998e1335e2fSThierry Reding 		sor->clk_pad = NULL;
3999e1335e2fSThierry Reding 	}
4000e1335e2fSThierry Reding 
4001e1335e2fSThierry Reding 	/*
4002e1335e2fSThierry Reding 	 * The bootloader may have set up the SOR such that it's module clock
4003e1335e2fSThierry Reding 	 * is sourced by one of the display PLLs. However, that doesn't work
4004e1335e2fSThierry Reding 	 * without properly having set up other bits of the SOR.
4005e1335e2fSThierry Reding 	 */
4006e1335e2fSThierry Reding 	err = clk_set_parent(sor->clk_out, sor->clk_safe);
4007e1335e2fSThierry Reding 	if (err < 0) {
4008e1335e2fSThierry Reding 		dev_err(&pdev->dev, "failed to use safe clock: %d\n", err);
4009e1335e2fSThierry Reding 		goto remove;
4010e1335e2fSThierry Reding 	}
4011e1335e2fSThierry Reding 
4012aaff8bd2SThierry Reding 	platform_set_drvdata(pdev, sor);
4013aaff8bd2SThierry Reding 	pm_runtime_enable(&pdev->dev);
4014aaff8bd2SThierry Reding 
4015e1335e2fSThierry Reding 	/*
4016e1335e2fSThierry Reding 	 * On Tegra210 and earlier, provide our own implementation for the
4017e1335e2fSThierry Reding 	 * pad output clock.
4018e1335e2fSThierry Reding 	 */
4019e1335e2fSThierry Reding 	if (!sor->clk_pad) {
4020*4bdf4710SThierry Reding 		char *name;
4021*4bdf4710SThierry Reding 
4022e1335e2fSThierry Reding 		err = pm_runtime_get_sync(&pdev->dev);
4023e1335e2fSThierry Reding 		if (err < 0) {
4024e1335e2fSThierry Reding 			dev_err(&pdev->dev, "failed to get runtime PM: %d\n",
4025e1335e2fSThierry Reding 				err);
4026e1335e2fSThierry Reding 			goto remove;
4027e1335e2fSThierry Reding 		}
4028b299221cSThierry Reding 
4029*4bdf4710SThierry Reding 		name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "sor%u_pad_clkout", sor->index);
4030*4bdf4710SThierry Reding 		if (!name) {
4031*4bdf4710SThierry Reding 			err = -ENOMEM;
4032*4bdf4710SThierry Reding 			goto remove;
4033*4bdf4710SThierry Reding 		}
4034*4bdf4710SThierry Reding 
4035*4bdf4710SThierry Reding 		sor->clk_pad = tegra_clk_sor_pad_register(sor, name);
4036e1335e2fSThierry Reding 		pm_runtime_put(&pdev->dev);
4037e1335e2fSThierry Reding 	}
4038e1335e2fSThierry Reding 
4039e1335e2fSThierry Reding 	if (IS_ERR(sor->clk_pad)) {
4040e1335e2fSThierry Reding 		err = PTR_ERR(sor->clk_pad);
4041e1335e2fSThierry Reding 		dev_err(&pdev->dev, "failed to register SOR pad clock: %d\n",
4042e1335e2fSThierry Reding 			err);
4043b299221cSThierry Reding 		goto remove;
4044b299221cSThierry Reding 	}
4045b299221cSThierry Reding 
40466b6b6042SThierry Reding 	INIT_LIST_HEAD(&sor->client.list);
40476b6b6042SThierry Reding 	sor->client.ops = &sor_client_ops;
40486b6b6042SThierry Reding 	sor->client.dev = &pdev->dev;
40496b6b6042SThierry Reding 
40506b6b6042SThierry Reding 	err = host1x_client_register(&sor->client);
40516b6b6042SThierry Reding 	if (err < 0) {
40526b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
40536b6b6042SThierry Reding 			err);
4054459cc2c6SThierry Reding 		goto remove;
40556b6b6042SThierry Reding 	}
40566b6b6042SThierry Reding 
40576b6b6042SThierry Reding 	return 0;
4058459cc2c6SThierry Reding 
4059459cc2c6SThierry Reding remove:
4060459cc2c6SThierry Reding 	if (sor->ops && sor->ops->remove)
4061459cc2c6SThierry Reding 		sor->ops->remove(sor);
4062459cc2c6SThierry Reding output:
4063459cc2c6SThierry Reding 	tegra_output_remove(&sor->output);
4064459cc2c6SThierry Reding 	return err;
40656b6b6042SThierry Reding }
40666b6b6042SThierry Reding 
40676b6b6042SThierry Reding static int tegra_sor_remove(struct platform_device *pdev)
40686b6b6042SThierry Reding {
40696b6b6042SThierry Reding 	struct tegra_sor *sor = platform_get_drvdata(pdev);
40706b6b6042SThierry Reding 	int err;
40716b6b6042SThierry Reding 
4072aaff8bd2SThierry Reding 	pm_runtime_disable(&pdev->dev);
4073aaff8bd2SThierry Reding 
40746b6b6042SThierry Reding 	err = host1x_client_unregister(&sor->client);
40756b6b6042SThierry Reding 	if (err < 0) {
40766b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
40776b6b6042SThierry Reding 			err);
40786b6b6042SThierry Reding 		return err;
40796b6b6042SThierry Reding 	}
40806b6b6042SThierry Reding 
4081459cc2c6SThierry Reding 	if (sor->ops && sor->ops->remove) {
4082459cc2c6SThierry Reding 		err = sor->ops->remove(sor);
4083459cc2c6SThierry Reding 		if (err < 0)
4084459cc2c6SThierry Reding 			dev_err(&pdev->dev, "failed to remove SOR: %d\n", err);
4085459cc2c6SThierry Reding 	}
4086459cc2c6SThierry Reding 
4087328ec69eSThierry Reding 	tegra_output_remove(&sor->output);
40886b6b6042SThierry Reding 
40896b6b6042SThierry Reding 	return 0;
40906b6b6042SThierry Reding }
40916b6b6042SThierry Reding 
4092aaff8bd2SThierry Reding #ifdef CONFIG_PM
4093aaff8bd2SThierry Reding static int tegra_sor_suspend(struct device *dev)
4094aaff8bd2SThierry Reding {
4095aaff8bd2SThierry Reding 	struct tegra_sor *sor = dev_get_drvdata(dev);
4096aaff8bd2SThierry Reding 	int err;
4097aaff8bd2SThierry Reding 
4098f8c79120SJon Hunter 	if (sor->rst) {
4099aaff8bd2SThierry Reding 		err = reset_control_assert(sor->rst);
4100aaff8bd2SThierry Reding 		if (err < 0) {
4101aaff8bd2SThierry Reding 			dev_err(dev, "failed to assert reset: %d\n", err);
4102aaff8bd2SThierry Reding 			return err;
4103aaff8bd2SThierry Reding 		}
410411c632e1SThierry Reding 
410511c632e1SThierry Reding 		reset_control_release(sor->rst);
4106f8c79120SJon Hunter 	}
4107aaff8bd2SThierry Reding 
4108aaff8bd2SThierry Reding 	usleep_range(1000, 2000);
4109aaff8bd2SThierry Reding 
4110aaff8bd2SThierry Reding 	clk_disable_unprepare(sor->clk);
4111aaff8bd2SThierry Reding 
4112aaff8bd2SThierry Reding 	return 0;
4113aaff8bd2SThierry Reding }
4114aaff8bd2SThierry Reding 
4115aaff8bd2SThierry Reding static int tegra_sor_resume(struct device *dev)
4116aaff8bd2SThierry Reding {
4117aaff8bd2SThierry Reding 	struct tegra_sor *sor = dev_get_drvdata(dev);
4118aaff8bd2SThierry Reding 	int err;
4119aaff8bd2SThierry Reding 
4120aaff8bd2SThierry Reding 	err = clk_prepare_enable(sor->clk);
4121aaff8bd2SThierry Reding 	if (err < 0) {
4122aaff8bd2SThierry Reding 		dev_err(dev, "failed to enable clock: %d\n", err);
4123aaff8bd2SThierry Reding 		return err;
4124aaff8bd2SThierry Reding 	}
4125aaff8bd2SThierry Reding 
4126aaff8bd2SThierry Reding 	usleep_range(1000, 2000);
4127aaff8bd2SThierry Reding 
4128f8c79120SJon Hunter 	if (sor->rst) {
412911c632e1SThierry Reding 		err = reset_control_acquire(sor->rst);
413011c632e1SThierry Reding 		if (err < 0) {
413111c632e1SThierry Reding 			dev_err(dev, "failed to acquire reset: %d\n", err);
413211c632e1SThierry Reding 			clk_disable_unprepare(sor->clk);
413311c632e1SThierry Reding 			return err;
413411c632e1SThierry Reding 		}
413511c632e1SThierry Reding 
4136aaff8bd2SThierry Reding 		err = reset_control_deassert(sor->rst);
4137aaff8bd2SThierry Reding 		if (err < 0) {
4138aaff8bd2SThierry Reding 			dev_err(dev, "failed to deassert reset: %d\n", err);
413911c632e1SThierry Reding 			reset_control_release(sor->rst);
4140aaff8bd2SThierry Reding 			clk_disable_unprepare(sor->clk);
4141aaff8bd2SThierry Reding 			return err;
4142aaff8bd2SThierry Reding 		}
4143f8c79120SJon Hunter 	}
4144aaff8bd2SThierry Reding 
4145aaff8bd2SThierry Reding 	return 0;
4146aaff8bd2SThierry Reding }
4147aaff8bd2SThierry Reding #endif
4148aaff8bd2SThierry Reding 
4149aaff8bd2SThierry Reding static const struct dev_pm_ops tegra_sor_pm_ops = {
4150aaff8bd2SThierry Reding 	SET_RUNTIME_PM_OPS(tegra_sor_suspend, tegra_sor_resume, NULL)
4151aaff8bd2SThierry Reding };
4152aaff8bd2SThierry Reding 
41536b6b6042SThierry Reding struct platform_driver tegra_sor_driver = {
41546b6b6042SThierry Reding 	.driver = {
41556b6b6042SThierry Reding 		.name = "tegra-sor",
41566b6b6042SThierry Reding 		.of_match_table = tegra_sor_of_match,
4157aaff8bd2SThierry Reding 		.pm = &tegra_sor_pm_ops,
41586b6b6042SThierry Reding 	},
41596b6b6042SThierry Reding 	.probe = tegra_sor_probe,
41606b6b6042SThierry Reding 	.remove = tegra_sor_remove,
41616b6b6042SThierry Reding };
4162