xref: /openbmc/linux/drivers/gpu/drm/tegra/sor.c (revision d23691f647855ff765c67d281ec73c59104d9eb1)
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 
5254bdf4710SThierry Reding static const char * const tegra_clk_sor_pad_parents[2][2] = {
5264bdf4710SThierry Reding 	{ "pll_d_out0", "pll_dp" },
5274bdf4710SThierry Reding 	{ "pll_d2_out0", "pll_dp" },
528b299221cSThierry Reding };
529b299221cSThierry Reding 
53061417aaaSThierry Reding /*
53161417aaaSThierry Reding  * Implementing ->set_parent() here isn't really required because the parent
53261417aaaSThierry Reding  * will be explicitly selected in the driver code via the DP_CLK_SEL mux in
53361417aaaSThierry Reding  * the SOR_CLK_CNTRL register. This is primarily for compatibility with the
53461417aaaSThierry Reding  * Tegra186 and later SoC generations where the BPMP implements this clock
53561417aaaSThierry Reding  * and doesn't expose the mux via the common clock framework.
53661417aaaSThierry Reding  */
53761417aaaSThierry Reding 
538e1335e2fSThierry Reding static int tegra_clk_sor_pad_set_parent(struct clk_hw *hw, u8 index)
539b299221cSThierry Reding {
540e1335e2fSThierry Reding 	struct tegra_clk_sor_pad *pad = to_pad(hw);
541e1335e2fSThierry Reding 	struct tegra_sor *sor = pad->sor;
542b299221cSThierry Reding 	u32 value;
543b299221cSThierry Reding 
544b299221cSThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
545b299221cSThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
546b299221cSThierry Reding 
547b299221cSThierry Reding 	switch (index) {
548b299221cSThierry Reding 	case 0:
549b299221cSThierry Reding 		value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
550b299221cSThierry Reding 		break;
551b299221cSThierry Reding 
552b299221cSThierry Reding 	case 1:
553b299221cSThierry Reding 		value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
554b299221cSThierry Reding 		break;
555b299221cSThierry Reding 	}
556b299221cSThierry Reding 
557b299221cSThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
558b299221cSThierry Reding 
559b299221cSThierry Reding 	return 0;
560b299221cSThierry Reding }
561b299221cSThierry Reding 
562e1335e2fSThierry Reding static u8 tegra_clk_sor_pad_get_parent(struct clk_hw *hw)
563b299221cSThierry Reding {
564e1335e2fSThierry Reding 	struct tegra_clk_sor_pad *pad = to_pad(hw);
565e1335e2fSThierry Reding 	struct tegra_sor *sor = pad->sor;
566b299221cSThierry Reding 	u8 parent = U8_MAX;
567b299221cSThierry Reding 	u32 value;
568b299221cSThierry Reding 
569b299221cSThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
570b299221cSThierry Reding 
571b299221cSThierry Reding 	switch (value & SOR_CLK_CNTRL_DP_CLK_SEL_MASK) {
572b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK:
573b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_PCLK:
574b299221cSThierry Reding 		parent = 0;
575b299221cSThierry Reding 		break;
576b299221cSThierry Reding 
577b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK:
578b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_DPCLK:
579b299221cSThierry Reding 		parent = 1;
580b299221cSThierry Reding 		break;
581b299221cSThierry Reding 	}
582b299221cSThierry Reding 
583b299221cSThierry Reding 	return parent;
584b299221cSThierry Reding }
585b299221cSThierry Reding 
586e1335e2fSThierry Reding static const struct clk_ops tegra_clk_sor_pad_ops = {
587e1335e2fSThierry Reding 	.set_parent = tegra_clk_sor_pad_set_parent,
588e1335e2fSThierry Reding 	.get_parent = tegra_clk_sor_pad_get_parent,
589b299221cSThierry Reding };
590b299221cSThierry Reding 
591e1335e2fSThierry Reding static struct clk *tegra_clk_sor_pad_register(struct tegra_sor *sor,
592b299221cSThierry Reding 					      const char *name)
593b299221cSThierry Reding {
594e1335e2fSThierry Reding 	struct tegra_clk_sor_pad *pad;
595b299221cSThierry Reding 	struct clk_init_data init;
596b299221cSThierry Reding 	struct clk *clk;
597b299221cSThierry Reding 
598e1335e2fSThierry Reding 	pad = devm_kzalloc(sor->dev, sizeof(*pad), GFP_KERNEL);
599e1335e2fSThierry Reding 	if (!pad)
600b299221cSThierry Reding 		return ERR_PTR(-ENOMEM);
601b299221cSThierry Reding 
602e1335e2fSThierry Reding 	pad->sor = sor;
603b299221cSThierry Reding 
604b299221cSThierry Reding 	init.name = name;
605b299221cSThierry Reding 	init.flags = 0;
6064bdf4710SThierry Reding 	init.parent_names = tegra_clk_sor_pad_parents[sor->index];
6074bdf4710SThierry Reding 	init.num_parents = ARRAY_SIZE(tegra_clk_sor_pad_parents[sor->index]);
608e1335e2fSThierry Reding 	init.ops = &tegra_clk_sor_pad_ops;
609b299221cSThierry Reding 
610e1335e2fSThierry Reding 	pad->hw.init = &init;
611b299221cSThierry Reding 
612e1335e2fSThierry Reding 	clk = devm_clk_register(sor->dev, &pad->hw);
613b299221cSThierry Reding 
614b299221cSThierry Reding 	return clk;
615b299221cSThierry Reding }
616b299221cSThierry Reding 
617c9533131SThierry Reding static void tegra_sor_filter_rates(struct tegra_sor *sor)
618c9533131SThierry Reding {
619c9533131SThierry Reding 	struct drm_dp_link *link = &sor->link;
620c9533131SThierry Reding 	unsigned int i;
621c9533131SThierry Reding 
622c9533131SThierry Reding 	/* Tegra only supports RBR, HBR and HBR2 */
623c9533131SThierry Reding 	for (i = 0; i < link->num_rates; i++) {
624c9533131SThierry Reding 		switch (link->rates[i]) {
625c9533131SThierry Reding 		case 1620000:
626c9533131SThierry Reding 		case 2700000:
627c9533131SThierry Reding 		case 5400000:
628c9533131SThierry Reding 			break;
629c9533131SThierry Reding 
630c9533131SThierry Reding 		default:
631c9533131SThierry Reding 			DRM_DEBUG_KMS("link rate %lu kHz not supported\n",
632c9533131SThierry Reding 				      link->rates[i]);
633c9533131SThierry Reding 			link->rates[i] = 0;
634c9533131SThierry Reding 			break;
635c9533131SThierry Reding 		}
636c9533131SThierry Reding 	}
637c9533131SThierry Reding 
638c9533131SThierry Reding 	drm_dp_link_update_rates(link);
639c9533131SThierry Reding }
640c9533131SThierry Reding 
641c1763937SThierry Reding static int tegra_sor_power_up_lanes(struct tegra_sor *sor, unsigned int lanes)
6426b6b6042SThierry Reding {
643c1763937SThierry Reding 	unsigned long timeout;
64428fe2076SThierry Reding 	u32 value;
6456b6b6042SThierry Reding 
646c1763937SThierry Reding 	/*
647c1763937SThierry Reding 	 * Clear or set the PD_TXD bit corresponding to each lane, depending
648c1763937SThierry Reding 	 * on whether it is used or not.
649c1763937SThierry Reding 	 */
650880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
651c1763937SThierry Reding 
652c1763937SThierry Reding 	if (lanes <= 2)
653c1763937SThierry Reding 		value &= ~(SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[3]) |
654c1763937SThierry Reding 			   SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[2]));
655c1763937SThierry Reding 	else
656c1763937SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[3]) |
657c1763937SThierry Reding 			 SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[2]);
658c1763937SThierry Reding 
659c1763937SThierry Reding 	if (lanes <= 1)
660c1763937SThierry Reding 		value &= ~SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[1]);
661c1763937SThierry Reding 	else
662c1763937SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[1]);
663c1763937SThierry Reding 
664c1763937SThierry Reding 	if (lanes == 0)
665c1763937SThierry Reding 		value &= ~SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[0]);
666c1763937SThierry Reding 	else
667c1763937SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[0]);
668c1763937SThierry Reding 
669880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
6706b6b6042SThierry Reding 
671c1763937SThierry Reding 	/* start lane sequencer */
672c1763937SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
673c1763937SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_UP;
674c1763937SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
675c1763937SThierry Reding 
676c1763937SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
677c1763937SThierry Reding 
678c1763937SThierry Reding 	while (time_before(jiffies, timeout)) {
679c1763937SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
680c1763937SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
681c1763937SThierry Reding 			break;
682c1763937SThierry Reding 
683c1763937SThierry Reding 		usleep_range(250, 1000);
684c1763937SThierry Reding 	}
685c1763937SThierry Reding 
686c1763937SThierry Reding 	if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
687c1763937SThierry Reding 		return -ETIMEDOUT;
688c1763937SThierry Reding 
689c1763937SThierry Reding 	return 0;
690c1763937SThierry Reding }
691c1763937SThierry Reding 
692c1763937SThierry Reding static int tegra_sor_power_down_lanes(struct tegra_sor *sor)
693c1763937SThierry Reding {
694c1763937SThierry Reding 	unsigned long timeout;
695c1763937SThierry Reding 	u32 value;
696c1763937SThierry Reding 
697c1763937SThierry Reding 	/* power down all lanes */
698880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
699c1763937SThierry Reding 	value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
700c1763937SThierry Reding 		   SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
701880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
7026b6b6042SThierry Reding 
703c1763937SThierry Reding 	/* start lane sequencer */
704c1763937SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
705c1763937SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
706c1763937SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
707c1763937SThierry Reding 
708c1763937SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
709c1763937SThierry Reding 
710c1763937SThierry Reding 	while (time_before(jiffies, timeout)) {
711c1763937SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
712c1763937SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
713c1763937SThierry Reding 			break;
714c1763937SThierry Reding 
715c1763937SThierry Reding 		usleep_range(25, 100);
716c1763937SThierry Reding 	}
717c1763937SThierry Reding 
718c1763937SThierry Reding 	if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
719c1763937SThierry Reding 		return -ETIMEDOUT;
720c1763937SThierry Reding 
721c1763937SThierry Reding 	return 0;
722c1763937SThierry Reding }
723c1763937SThierry Reding 
724c1763937SThierry Reding static void tegra_sor_dp_precharge(struct tegra_sor *sor, unsigned int lanes)
725c1763937SThierry Reding {
726c1763937SThierry Reding 	u32 value;
727c1763937SThierry Reding 
728c1763937SThierry Reding 	/* pre-charge all used lanes */
729c1763937SThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
730c1763937SThierry Reding 
731c1763937SThierry Reding 	if (lanes <= 2)
732c1763937SThierry Reding 		value &= ~(SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[3]) |
733c1763937SThierry Reding 			   SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[2]));
734c1763937SThierry Reding 	else
735c1763937SThierry Reding 		value |= SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[3]) |
736c1763937SThierry Reding 			 SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[2]);
737c1763937SThierry Reding 
738c1763937SThierry Reding 	if (lanes <= 1)
739c1763937SThierry Reding 		value &= ~SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[1]);
740c1763937SThierry Reding 	else
741c1763937SThierry Reding 		value |= SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[1]);
742c1763937SThierry Reding 
743c1763937SThierry Reding 	if (lanes == 0)
744c1763937SThierry Reding 		value &= ~SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[0]);
745c1763937SThierry Reding 	else
746c1763937SThierry Reding 		value |= SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[0]);
747c1763937SThierry Reding 
748c1763937SThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
749c1763937SThierry Reding 
750c1763937SThierry Reding 	usleep_range(15, 100);
7516b6b6042SThierry Reding 
752880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
7536b6b6042SThierry Reding 	value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
7546b6b6042SThierry Reding 		   SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
755880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
7566b6b6042SThierry Reding }
7576b6b6042SThierry Reding 
758c1763937SThierry Reding static void tegra_sor_dp_term_calibrate(struct tegra_sor *sor)
759c1763937SThierry Reding {
760c1763937SThierry Reding 	u32 mask = 0x08, adj = 0, value;
7616b6b6042SThierry Reding 
762c1763937SThierry Reding 	/* enable pad calibration logic */
763c1763937SThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
764c1763937SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
765c1763937SThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
7666b6b6042SThierry Reding 
767c1763937SThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll1);
768c1763937SThierry Reding 	value |= SOR_PLL1_TMDS_TERM;
769c1763937SThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll1);
7706b6b6042SThierry Reding 
771c1763937SThierry Reding 	while (mask) {
772c1763937SThierry Reding 		adj |= mask;
7736b6b6042SThierry Reding 
774c1763937SThierry Reding 		value = tegra_sor_readl(sor, sor->soc->regs->pll1);
775c1763937SThierry Reding 		value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
776c1763937SThierry Reding 		value |= SOR_PLL1_TMDS_TERMADJ(adj);
777c1763937SThierry Reding 		tegra_sor_writel(sor, value, sor->soc->regs->pll1);
778c1763937SThierry Reding 
779c1763937SThierry Reding 		usleep_range(100, 200);
780c1763937SThierry Reding 
781c1763937SThierry Reding 		value = tegra_sor_readl(sor, sor->soc->regs->pll1);
782c1763937SThierry Reding 		if (value & SOR_PLL1_TERM_COMPOUT)
783c1763937SThierry Reding 			adj &= ~mask;
784c1763937SThierry Reding 
785c1763937SThierry Reding 		mask >>= 1;
7866b6b6042SThierry Reding 	}
7876b6b6042SThierry Reding 
788c1763937SThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll1);
789c1763937SThierry Reding 	value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
790c1763937SThierry Reding 	value |= SOR_PLL1_TMDS_TERMADJ(adj);
791c1763937SThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll1);
7926b6b6042SThierry Reding 
793c1763937SThierry Reding 	/* disable pad calibration logic */
794c1763937SThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
795c1763937SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
796c1763937SThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
797c1763937SThierry Reding }
7986b6b6042SThierry Reding 
799c1763937SThierry Reding static int tegra_sor_dp_link_apply_training(struct drm_dp_link *link)
800c1763937SThierry Reding {
801c1763937SThierry Reding 	struct tegra_sor *sor = container_of(link, struct tegra_sor, link);
802c1763937SThierry Reding 	u32 voltage_swing = 0, pre_emphasis = 0, post_cursor = 0;
803c1763937SThierry Reding 	const struct tegra_sor_soc *soc = sor->soc;
804c1763937SThierry Reding 	u32 pattern = 0, tx_pu = 0, value;
805c1763937SThierry Reding 	unsigned int i;
8066b6b6042SThierry Reding 
807c1763937SThierry Reding 	for (value = 0, i = 0; i < link->lanes; i++) {
808c1763937SThierry Reding 		u8 vs = link->train.request.voltage_swing[i];
809c1763937SThierry Reding 		u8 pe = link->train.request.pre_emphasis[i];
810c1763937SThierry Reding 		u8 pc = link->train.request.post_cursor[i];
811c1763937SThierry Reding 		u8 shift = sor->soc->lane_map[i] << 3;
812c1763937SThierry Reding 
813c1763937SThierry Reding 		voltage_swing |= soc->voltage_swing[pc][vs][pe] << shift;
814c1763937SThierry Reding 		pre_emphasis |= soc->pre_emphasis[pc][vs][pe] << shift;
815c1763937SThierry Reding 		post_cursor |= soc->post_cursor[pc][vs][pe] << shift;
816c1763937SThierry Reding 
817c1763937SThierry Reding 		if (sor->soc->tx_pu[pc][vs][pe] > tx_pu)
818c1763937SThierry Reding 			tx_pu = sor->soc->tx_pu[pc][vs][pe];
819c1763937SThierry Reding 
820c1763937SThierry Reding 		switch (link->train.pattern) {
821c1763937SThierry Reding 		case DP_TRAINING_PATTERN_DISABLE:
822c1763937SThierry Reding 			value = SOR_DP_TPG_SCRAMBLER_GALIOS |
8236b6b6042SThierry Reding 				SOR_DP_TPG_PATTERN_NONE;
824c1763937SThierry Reding 			break;
825c1763937SThierry Reding 
826c1763937SThierry Reding 		case DP_TRAINING_PATTERN_1:
827c1763937SThierry Reding 			value = SOR_DP_TPG_SCRAMBLER_NONE |
828c1763937SThierry Reding 				SOR_DP_TPG_PATTERN_TRAIN1;
829c1763937SThierry Reding 			break;
830c1763937SThierry Reding 
831c1763937SThierry Reding 		case DP_TRAINING_PATTERN_2:
832c1763937SThierry Reding 			value = SOR_DP_TPG_SCRAMBLER_NONE |
833c1763937SThierry Reding 				SOR_DP_TPG_PATTERN_TRAIN2;
834c1763937SThierry Reding 			break;
835c1763937SThierry Reding 
836c1763937SThierry Reding 		case DP_TRAINING_PATTERN_3:
837c1763937SThierry Reding 			value = SOR_DP_TPG_SCRAMBLER_NONE |
838c1763937SThierry Reding 				SOR_DP_TPG_PATTERN_TRAIN3;
839c1763937SThierry Reding 			break;
840c1763937SThierry Reding 
841c1763937SThierry Reding 		default:
842c1763937SThierry Reding 			return -EINVAL;
8436b6b6042SThierry Reding 		}
8446b6b6042SThierry Reding 
845c1763937SThierry Reding 		if (link->caps.channel_coding)
846c1763937SThierry Reding 			value |= SOR_DP_TPG_CHANNEL_CODING;
8476b6b6042SThierry Reding 
848c1763937SThierry Reding 		pattern = pattern << 8 | value;
849c1763937SThierry Reding 	}
8506b6b6042SThierry Reding 
851c1763937SThierry Reding 	tegra_sor_writel(sor, voltage_swing, SOR_LANE_DRIVE_CURRENT0);
852c1763937SThierry Reding 	tegra_sor_writel(sor, pre_emphasis, SOR_LANE_PREEMPHASIS0);
853c1763937SThierry Reding 
854c1763937SThierry Reding 	if (link->caps.tps3_supported)
855c1763937SThierry Reding 		tegra_sor_writel(sor, post_cursor, SOR_LANE_POSTCURSOR0);
856c1763937SThierry Reding 
857c1763937SThierry Reding 	tegra_sor_writel(sor, pattern, SOR_DP_TPG);
858c1763937SThierry Reding 
859c1763937SThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
860c1763937SThierry Reding 	value &= ~SOR_DP_PADCTL_TX_PU_MASK;
861c1763937SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU_ENABLE;
862c1763937SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU(tx_pu);
863c1763937SThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
864c1763937SThierry Reding 
865c1763937SThierry Reding 	usleep_range(20, 100);
8666b6b6042SThierry Reding 
8676b6b6042SThierry Reding 	return 0;
8686b6b6042SThierry Reding }
8696b6b6042SThierry Reding 
870c1763937SThierry Reding static int tegra_sor_dp_link_configure(struct drm_dp_link *link)
871c1763937SThierry Reding {
872c1763937SThierry Reding 	struct tegra_sor *sor = container_of(link, struct tegra_sor, link);
873c1763937SThierry Reding 	unsigned int rate, lanes;
874c1763937SThierry Reding 	u32 value;
875c1763937SThierry Reding 	int err;
876c1763937SThierry Reding 
877c1763937SThierry Reding 	rate = drm_dp_link_rate_to_bw_code(link->rate);
878c1763937SThierry Reding 	lanes = link->lanes;
879c1763937SThierry Reding 
880c1763937SThierry Reding 	/* configure link speed and lane count */
881c1763937SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
882c1763937SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
883c1763937SThierry Reding 	value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
884c1763937SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
885c1763937SThierry Reding 
886c1763937SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
887c1763937SThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
888c1763937SThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
889c1763937SThierry Reding 
890c1763937SThierry Reding 	if (link->caps.enhanced_framing)
891c1763937SThierry Reding 		value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
892c1763937SThierry Reding 
893c1763937SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
894c1763937SThierry Reding 
895c1763937SThierry Reding 	usleep_range(400, 1000);
896c1763937SThierry Reding 
897c1763937SThierry Reding 	/* configure load pulse position adjustment */
898c1763937SThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll1);
899c1763937SThierry Reding 	value &= ~SOR_PLL1_LOADADJ_MASK;
900c1763937SThierry Reding 
901c1763937SThierry Reding 	switch (rate) {
902c1763937SThierry Reding 	case DP_LINK_BW_1_62:
903c1763937SThierry Reding 		value |= SOR_PLL1_LOADADJ(0x3);
904c1763937SThierry Reding 		break;
905c1763937SThierry Reding 
906c1763937SThierry Reding 	case DP_LINK_BW_2_7:
907c1763937SThierry Reding 		value |= SOR_PLL1_LOADADJ(0x4);
908c1763937SThierry Reding 		break;
909c1763937SThierry Reding 
910c1763937SThierry Reding 	case DP_LINK_BW_5_4:
911c1763937SThierry Reding 		value |= SOR_PLL1_LOADADJ(0x6);
912c1763937SThierry Reding 		break;
913c1763937SThierry Reding 	}
914c1763937SThierry Reding 
915c1763937SThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll1);
916c1763937SThierry Reding 
917c1763937SThierry Reding 	/* use alternate scrambler reset for eDP */
918c1763937SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
919c1763937SThierry Reding 
920c1763937SThierry Reding 	if (link->edp == 0)
921c1763937SThierry Reding 		value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
922c1763937SThierry Reding 	else
923c1763937SThierry Reding 		value |= SOR_DP_SPARE_PANEL_INTERNAL;
924c1763937SThierry Reding 
925c1763937SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
926c1763937SThierry Reding 
927c1763937SThierry Reding 	err = tegra_sor_power_down_lanes(sor);
928c1763937SThierry Reding 	if (err < 0) {
929c1763937SThierry Reding 		dev_err(sor->dev, "failed to power down lanes: %d\n", err);
930c1763937SThierry Reding 		return err;
931c1763937SThierry Reding 	}
932c1763937SThierry Reding 
933c1763937SThierry Reding 	/* power up and pre-charge lanes */
934c1763937SThierry Reding 	err = tegra_sor_power_up_lanes(sor, lanes);
935c1763937SThierry Reding 	if (err < 0) {
936c1763937SThierry Reding 		dev_err(sor->dev, "failed to power up %u lane%s: %d\n",
937c1763937SThierry Reding 			lanes, (lanes != 1) ? "s" : "", err);
938c1763937SThierry Reding 		return err;
939c1763937SThierry Reding 	}
940c1763937SThierry Reding 
941c1763937SThierry Reding 	tegra_sor_dp_precharge(sor, lanes);
942c1763937SThierry Reding 
943c1763937SThierry Reding 	return 0;
944c1763937SThierry Reding }
945c1763937SThierry Reding 
946c1763937SThierry Reding static const struct drm_dp_link_ops tegra_sor_dp_link_ops = {
947c1763937SThierry Reding 	.apply_training = tegra_sor_dp_link_apply_training,
948c1763937SThierry Reding 	.configure = tegra_sor_dp_link_configure,
949c1763937SThierry Reding };
950c1763937SThierry Reding 
9516b6b6042SThierry Reding static void tegra_sor_super_update(struct tegra_sor *sor)
9526b6b6042SThierry Reding {
953a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
954a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 1, SOR_SUPER_STATE0);
955a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
9566b6b6042SThierry Reding }
9576b6b6042SThierry Reding 
9586b6b6042SThierry Reding static void tegra_sor_update(struct tegra_sor *sor)
9596b6b6042SThierry Reding {
960a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE0);
961a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 1, SOR_STATE0);
962a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE0);
9636b6b6042SThierry Reding }
9646b6b6042SThierry Reding 
9656b6b6042SThierry Reding static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
9666b6b6042SThierry Reding {
96728fe2076SThierry Reding 	u32 value;
9686b6b6042SThierry Reding 
9696b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWM_DIV);
9706b6b6042SThierry Reding 	value &= ~SOR_PWM_DIV_MASK;
9716b6b6042SThierry Reding 	value |= 0x400; /* period */
9726b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWM_DIV);
9736b6b6042SThierry Reding 
9746b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWM_CTL);
9756b6b6042SThierry Reding 	value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
9766b6b6042SThierry Reding 	value |= 0x400; /* duty cycle */
9776b6b6042SThierry Reding 	value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
9786b6b6042SThierry Reding 	value |= SOR_PWM_CTL_TRIGGER;
9796b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWM_CTL);
9806b6b6042SThierry Reding 
9816b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
9826b6b6042SThierry Reding 
9836b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
9846b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWM_CTL);
9856b6b6042SThierry Reding 		if ((value & SOR_PWM_CTL_TRIGGER) == 0)
9866b6b6042SThierry Reding 			return 0;
9876b6b6042SThierry Reding 
9886b6b6042SThierry Reding 		usleep_range(25, 100);
9896b6b6042SThierry Reding 	}
9906b6b6042SThierry Reding 
9916b6b6042SThierry Reding 	return -ETIMEDOUT;
9926b6b6042SThierry Reding }
9936b6b6042SThierry Reding 
9946b6b6042SThierry Reding static int tegra_sor_attach(struct tegra_sor *sor)
9956b6b6042SThierry Reding {
9966b6b6042SThierry Reding 	unsigned long value, timeout;
9976b6b6042SThierry Reding 
9986b6b6042SThierry Reding 	/* wake up in normal mode */
999a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
10006b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
10016b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_MODE_NORMAL;
1002a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
10036b6b6042SThierry Reding 	tegra_sor_super_update(sor);
10046b6b6042SThierry Reding 
10056b6b6042SThierry Reding 	/* attach */
1006a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
10076b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_ATTACHED;
1008a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
10096b6b6042SThierry Reding 	tegra_sor_super_update(sor);
10106b6b6042SThierry Reding 
10116b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
10126b6b6042SThierry Reding 
10136b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
10146b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
10156b6b6042SThierry Reding 		if ((value & SOR_TEST_ATTACHED) != 0)
10166b6b6042SThierry Reding 			return 0;
10176b6b6042SThierry Reding 
10186b6b6042SThierry Reding 		usleep_range(25, 100);
10196b6b6042SThierry Reding 	}
10206b6b6042SThierry Reding 
10216b6b6042SThierry Reding 	return -ETIMEDOUT;
10226b6b6042SThierry Reding }
10236b6b6042SThierry Reding 
10246b6b6042SThierry Reding static int tegra_sor_wakeup(struct tegra_sor *sor)
10256b6b6042SThierry Reding {
10266b6b6042SThierry Reding 	unsigned long value, timeout;
10276b6b6042SThierry Reding 
10286b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
10296b6b6042SThierry Reding 
10306b6b6042SThierry Reding 	/* wait for head to wake up */
10316b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
10326b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
10336b6b6042SThierry Reding 		value &= SOR_TEST_HEAD_MODE_MASK;
10346b6b6042SThierry Reding 
10356b6b6042SThierry Reding 		if (value == SOR_TEST_HEAD_MODE_AWAKE)
10366b6b6042SThierry Reding 			return 0;
10376b6b6042SThierry Reding 
10386b6b6042SThierry Reding 		usleep_range(25, 100);
10396b6b6042SThierry Reding 	}
10406b6b6042SThierry Reding 
10416b6b6042SThierry Reding 	return -ETIMEDOUT;
10426b6b6042SThierry Reding }
10436b6b6042SThierry Reding 
10446b6b6042SThierry Reding static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
10456b6b6042SThierry Reding {
104628fe2076SThierry Reding 	u32 value;
10476b6b6042SThierry Reding 
10486b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWR);
10496b6b6042SThierry Reding 	value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
10506b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWR);
10516b6b6042SThierry Reding 
10526b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
10536b6b6042SThierry Reding 
10546b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
10556b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
10566b6b6042SThierry Reding 		if ((value & SOR_PWR_TRIGGER) == 0)
10576b6b6042SThierry Reding 			return 0;
10586b6b6042SThierry Reding 
10596b6b6042SThierry Reding 		usleep_range(25, 100);
10606b6b6042SThierry Reding 	}
10616b6b6042SThierry Reding 
10626b6b6042SThierry Reding 	return -ETIMEDOUT;
10636b6b6042SThierry Reding }
10646b6b6042SThierry Reding 
106534fa183bSThierry Reding struct tegra_sor_params {
106634fa183bSThierry Reding 	/* number of link clocks per line */
106734fa183bSThierry Reding 	unsigned int num_clocks;
106834fa183bSThierry Reding 	/* ratio between input and output */
106934fa183bSThierry Reding 	u64 ratio;
107034fa183bSThierry Reding 	/* precision factor */
107134fa183bSThierry Reding 	u64 precision;
107234fa183bSThierry Reding 
107334fa183bSThierry Reding 	unsigned int active_polarity;
107434fa183bSThierry Reding 	unsigned int active_count;
107534fa183bSThierry Reding 	unsigned int active_frac;
107634fa183bSThierry Reding 	unsigned int tu_size;
107734fa183bSThierry Reding 	unsigned int error;
107834fa183bSThierry Reding };
107934fa183bSThierry Reding 
108034fa183bSThierry Reding static int tegra_sor_compute_params(struct tegra_sor *sor,
108134fa183bSThierry Reding 				    struct tegra_sor_params *params,
108234fa183bSThierry Reding 				    unsigned int tu_size)
108334fa183bSThierry Reding {
108434fa183bSThierry Reding 	u64 active_sym, active_count, frac, approx;
108534fa183bSThierry Reding 	u32 active_polarity, active_frac = 0;
108634fa183bSThierry Reding 	const u64 f = params->precision;
108734fa183bSThierry Reding 	s64 error;
108834fa183bSThierry Reding 
108934fa183bSThierry Reding 	active_sym = params->ratio * tu_size;
109034fa183bSThierry Reding 	active_count = div_u64(active_sym, f) * f;
109134fa183bSThierry Reding 	frac = active_sym - active_count;
109234fa183bSThierry Reding 
109334fa183bSThierry Reding 	/* fraction < 0.5 */
109434fa183bSThierry Reding 	if (frac >= (f / 2)) {
109534fa183bSThierry Reding 		active_polarity = 1;
109634fa183bSThierry Reding 		frac = f - frac;
109734fa183bSThierry Reding 	} else {
109834fa183bSThierry Reding 		active_polarity = 0;
109934fa183bSThierry Reding 	}
110034fa183bSThierry Reding 
110134fa183bSThierry Reding 	if (frac != 0) {
110234fa183bSThierry Reding 		frac = div_u64(f * f,  frac); /* 1/fraction */
110334fa183bSThierry Reding 		if (frac <= (15 * f)) {
110434fa183bSThierry Reding 			active_frac = div_u64(frac, f);
110534fa183bSThierry Reding 
110634fa183bSThierry Reding 			/* round up */
110734fa183bSThierry Reding 			if (active_polarity)
110834fa183bSThierry Reding 				active_frac++;
110934fa183bSThierry Reding 		} else {
111034fa183bSThierry Reding 			active_frac = active_polarity ? 1 : 15;
111134fa183bSThierry Reding 		}
111234fa183bSThierry Reding 	}
111334fa183bSThierry Reding 
111434fa183bSThierry Reding 	if (active_frac == 1)
111534fa183bSThierry Reding 		active_polarity = 0;
111634fa183bSThierry Reding 
111734fa183bSThierry Reding 	if (active_polarity == 1) {
111834fa183bSThierry Reding 		if (active_frac) {
111934fa183bSThierry Reding 			approx = active_count + (active_frac * (f - 1)) * f;
112034fa183bSThierry Reding 			approx = div_u64(approx, active_frac * f);
112134fa183bSThierry Reding 		} else {
112234fa183bSThierry Reding 			approx = active_count + f;
112334fa183bSThierry Reding 		}
112434fa183bSThierry Reding 	} else {
112534fa183bSThierry Reding 		if (active_frac)
112634fa183bSThierry Reding 			approx = active_count + div_u64(f, active_frac);
112734fa183bSThierry Reding 		else
112834fa183bSThierry Reding 			approx = active_count;
112934fa183bSThierry Reding 	}
113034fa183bSThierry Reding 
113134fa183bSThierry Reding 	error = div_s64(active_sym - approx, tu_size);
113234fa183bSThierry Reding 	error *= params->num_clocks;
113334fa183bSThierry Reding 
113479211c8eSAndrew Morton 	if (error <= 0 && abs(error) < params->error) {
113534fa183bSThierry Reding 		params->active_count = div_u64(active_count, f);
113634fa183bSThierry Reding 		params->active_polarity = active_polarity;
113734fa183bSThierry Reding 		params->active_frac = active_frac;
113879211c8eSAndrew Morton 		params->error = abs(error);
113934fa183bSThierry Reding 		params->tu_size = tu_size;
114034fa183bSThierry Reding 
114134fa183bSThierry Reding 		if (error == 0)
114234fa183bSThierry Reding 			return true;
114334fa183bSThierry Reding 	}
114434fa183bSThierry Reding 
114534fa183bSThierry Reding 	return false;
114634fa183bSThierry Reding }
114734fa183bSThierry Reding 
1148a198359eSThierry Reding static int tegra_sor_compute_config(struct tegra_sor *sor,
114980444495SThierry Reding 				    const struct drm_display_mode *mode,
115034fa183bSThierry Reding 				    struct tegra_sor_config *config,
115134fa183bSThierry Reding 				    struct drm_dp_link *link)
115234fa183bSThierry Reding {
115334fa183bSThierry Reding 	const u64 f = 100000, link_rate = link->rate * 1000;
115434fa183bSThierry Reding 	const u64 pclk = mode->clock * 1000;
11557890b576SThierry Reding 	u64 input, output, watermark, num;
115634fa183bSThierry Reding 	struct tegra_sor_params params;
115734fa183bSThierry Reding 	u32 num_syms_per_line;
115834fa183bSThierry Reding 	unsigned int i;
115934fa183bSThierry Reding 
1160c728e2d4SThierry Reding 	if (!link_rate || !link->lanes || !pclk || !config->bits_per_pixel)
116134fa183bSThierry Reding 		return -EINVAL;
116234fa183bSThierry Reding 
116334fa183bSThierry Reding 	input = pclk * config->bits_per_pixel;
1164c728e2d4SThierry Reding 	output = link_rate * 8 * link->lanes;
116534fa183bSThierry Reding 
116634fa183bSThierry Reding 	if (input >= output)
116734fa183bSThierry Reding 		return -ERANGE;
116834fa183bSThierry Reding 
116934fa183bSThierry Reding 	memset(&params, 0, sizeof(params));
117034fa183bSThierry Reding 	params.ratio = div64_u64(input * f, output);
117134fa183bSThierry Reding 	params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
117234fa183bSThierry Reding 	params.precision = f;
117334fa183bSThierry Reding 	params.error = 64 * f;
117434fa183bSThierry Reding 	params.tu_size = 64;
117534fa183bSThierry Reding 
117634fa183bSThierry Reding 	for (i = params.tu_size; i >= 32; i--)
117734fa183bSThierry Reding 		if (tegra_sor_compute_params(sor, &params, i))
117834fa183bSThierry Reding 			break;
117934fa183bSThierry Reding 
118034fa183bSThierry Reding 	if (params.active_frac == 0) {
118134fa183bSThierry Reding 		config->active_polarity = 0;
118234fa183bSThierry Reding 		config->active_count = params.active_count;
118334fa183bSThierry Reding 
118434fa183bSThierry Reding 		if (!params.active_polarity)
118534fa183bSThierry Reding 			config->active_count--;
118634fa183bSThierry Reding 
118734fa183bSThierry Reding 		config->tu_size = params.tu_size;
118834fa183bSThierry Reding 		config->active_frac = 1;
118934fa183bSThierry Reding 	} else {
119034fa183bSThierry Reding 		config->active_polarity = params.active_polarity;
119134fa183bSThierry Reding 		config->active_count = params.active_count;
119234fa183bSThierry Reding 		config->active_frac = params.active_frac;
119334fa183bSThierry Reding 		config->tu_size = params.tu_size;
119434fa183bSThierry Reding 	}
119534fa183bSThierry Reding 
119634fa183bSThierry Reding 	dev_dbg(sor->dev,
119734fa183bSThierry Reding 		"polarity: %d active count: %d tu size: %d active frac: %d\n",
119834fa183bSThierry Reding 		config->active_polarity, config->active_count,
119934fa183bSThierry Reding 		config->tu_size, config->active_frac);
120034fa183bSThierry Reding 
120134fa183bSThierry Reding 	watermark = params.ratio * config->tu_size * (f - params.ratio);
120234fa183bSThierry Reding 	watermark = div_u64(watermark, f);
120334fa183bSThierry Reding 
120434fa183bSThierry Reding 	watermark = div_u64(watermark + params.error, f);
120534fa183bSThierry Reding 	config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
120634fa183bSThierry Reding 	num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
1207c728e2d4SThierry Reding 			    (link->lanes * 8);
120834fa183bSThierry Reding 
120934fa183bSThierry Reding 	if (config->watermark > 30) {
121034fa183bSThierry Reding 		config->watermark = 30;
121134fa183bSThierry Reding 		dev_err(sor->dev,
121234fa183bSThierry Reding 			"unable to compute TU size, forcing watermark to %u\n",
121334fa183bSThierry Reding 			config->watermark);
121434fa183bSThierry Reding 	} else if (config->watermark > num_syms_per_line) {
121534fa183bSThierry Reding 		config->watermark = num_syms_per_line;
121634fa183bSThierry Reding 		dev_err(sor->dev, "watermark too high, forcing to %u\n",
121734fa183bSThierry Reding 			config->watermark);
121834fa183bSThierry Reding 	}
121934fa183bSThierry Reding 
12207890b576SThierry Reding 	/* compute the number of symbols per horizontal blanking interval */
12217890b576SThierry Reding 	num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
12227890b576SThierry Reding 	config->hblank_symbols = div_u64(num, pclk);
12237890b576SThierry Reding 
122427ba465cSThierry Reding 	if (link->caps.enhanced_framing)
12257890b576SThierry Reding 		config->hblank_symbols -= 3;
12267890b576SThierry Reding 
1227c728e2d4SThierry Reding 	config->hblank_symbols -= 12 / link->lanes;
12287890b576SThierry Reding 
12297890b576SThierry Reding 	/* compute the number of symbols per vertical blanking interval */
12307890b576SThierry Reding 	num = (mode->hdisplay - 25) * link_rate;
12317890b576SThierry Reding 	config->vblank_symbols = div_u64(num, pclk);
1232c728e2d4SThierry Reding 	config->vblank_symbols -= 36 / link->lanes + 4;
12337890b576SThierry Reding 
12347890b576SThierry Reding 	dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
12357890b576SThierry Reding 		config->vblank_symbols);
12367890b576SThierry Reding 
123734fa183bSThierry Reding 	return 0;
123834fa183bSThierry Reding }
123934fa183bSThierry Reding 
1240402f6bcdSThierry Reding static void tegra_sor_apply_config(struct tegra_sor *sor,
1241402f6bcdSThierry Reding 				   const struct tegra_sor_config *config)
1242402f6bcdSThierry Reding {
1243402f6bcdSThierry Reding 	u32 value;
1244402f6bcdSThierry Reding 
1245402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
1246402f6bcdSThierry Reding 	value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
1247402f6bcdSThierry Reding 	value |= SOR_DP_LINKCTL_TU_SIZE(config->tu_size);
1248402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
1249402f6bcdSThierry Reding 
1250402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_CONFIG0);
1251402f6bcdSThierry Reding 	value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
1252402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_WATERMARK(config->watermark);
1253402f6bcdSThierry Reding 
1254402f6bcdSThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
1255402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config->active_count);
1256402f6bcdSThierry Reding 
1257402f6bcdSThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
1258402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config->active_frac);
1259402f6bcdSThierry Reding 
1260402f6bcdSThierry Reding 	if (config->active_polarity)
1261402f6bcdSThierry Reding 		value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
1262402f6bcdSThierry Reding 	else
1263402f6bcdSThierry Reding 		value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
1264402f6bcdSThierry Reding 
1265402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
1266402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
1267402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_CONFIG0);
1268402f6bcdSThierry Reding 
1269402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
1270402f6bcdSThierry Reding 	value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
1271402f6bcdSThierry Reding 	value |= config->hblank_symbols & 0xffff;
1272402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
1273402f6bcdSThierry Reding 
1274402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
1275402f6bcdSThierry Reding 	value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
1276402f6bcdSThierry Reding 	value |= config->vblank_symbols & 0xffff;
1277402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
1278402f6bcdSThierry Reding }
1279402f6bcdSThierry Reding 
12802bd1dd39SThierry Reding static void tegra_sor_mode_set(struct tegra_sor *sor,
12812bd1dd39SThierry Reding 			       const struct drm_display_mode *mode,
1282c31efa7aSThierry Reding 			       struct tegra_sor_state *state)
12832bd1dd39SThierry Reding {
12842bd1dd39SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(sor->output.encoder.crtc);
12852bd1dd39SThierry Reding 	unsigned int vbe, vse, hbe, hse, vbs, hbs;
12862bd1dd39SThierry Reding 	u32 value;
12872bd1dd39SThierry Reding 
12882bd1dd39SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
12892bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_PIXELDEPTH_MASK;
12902bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
12912bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_OWNER_MASK;
12922bd1dd39SThierry Reding 
12932bd1dd39SThierry Reding 	value |= SOR_STATE_ASY_CRC_MODE_COMPLETE |
12942bd1dd39SThierry Reding 		 SOR_STATE_ASY_OWNER(dc->pipe + 1);
12952bd1dd39SThierry Reding 
12962bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PHSYNC)
12972bd1dd39SThierry Reding 		value &= ~SOR_STATE_ASY_HSYNCPOL;
12982bd1dd39SThierry Reding 
12992bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
13002bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_HSYNCPOL;
13012bd1dd39SThierry Reding 
13022bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PVSYNC)
13032bd1dd39SThierry Reding 		value &= ~SOR_STATE_ASY_VSYNCPOL;
13042bd1dd39SThierry Reding 
13052bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
13062bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_VSYNCPOL;
13072bd1dd39SThierry Reding 
1308c31efa7aSThierry Reding 	switch (state->bpc) {
1309c31efa7aSThierry Reding 	case 16:
1310c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_48_444;
1311c31efa7aSThierry Reding 		break;
1312c31efa7aSThierry Reding 
1313c31efa7aSThierry Reding 	case 12:
1314c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_36_444;
1315c31efa7aSThierry Reding 		break;
1316c31efa7aSThierry Reding 
1317c31efa7aSThierry Reding 	case 10:
1318c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_30_444;
1319c31efa7aSThierry Reding 		break;
1320c31efa7aSThierry Reding 
13212bd1dd39SThierry Reding 	case 8:
13222bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
13232bd1dd39SThierry Reding 		break;
13242bd1dd39SThierry Reding 
13252bd1dd39SThierry Reding 	case 6:
13262bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
13272bd1dd39SThierry Reding 		break;
13282bd1dd39SThierry Reding 
13292bd1dd39SThierry Reding 	default:
1330c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
13312bd1dd39SThierry Reding 		break;
13322bd1dd39SThierry Reding 	}
13332bd1dd39SThierry Reding 
13342bd1dd39SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
13352bd1dd39SThierry Reding 
13362bd1dd39SThierry Reding 	/*
13372bd1dd39SThierry Reding 	 * TODO: The video timing programming below doesn't seem to match the
13382bd1dd39SThierry Reding 	 * register definitions.
13392bd1dd39SThierry Reding 	 */
13402bd1dd39SThierry Reding 
13412bd1dd39SThierry Reding 	value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
1342880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state1 + dc->pipe);
13432bd1dd39SThierry Reding 
13442bd1dd39SThierry Reding 	/* sync end = sync width - 1 */
13452bd1dd39SThierry Reding 	vse = mode->vsync_end - mode->vsync_start - 1;
13462bd1dd39SThierry Reding 	hse = mode->hsync_end - mode->hsync_start - 1;
13472bd1dd39SThierry Reding 
13482bd1dd39SThierry Reding 	value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
1349880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state2 + dc->pipe);
13502bd1dd39SThierry Reding 
13512bd1dd39SThierry Reding 	/* blank end = sync end + back porch */
13522bd1dd39SThierry Reding 	vbe = vse + (mode->vtotal - mode->vsync_end);
13532bd1dd39SThierry Reding 	hbe = hse + (mode->htotal - mode->hsync_end);
13542bd1dd39SThierry Reding 
13552bd1dd39SThierry Reding 	value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
1356880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state3 + dc->pipe);
13572bd1dd39SThierry Reding 
13582bd1dd39SThierry Reding 	/* blank start = blank end + active */
13592bd1dd39SThierry Reding 	vbs = vbe + mode->vdisplay;
13602bd1dd39SThierry Reding 	hbs = hbe + mode->hdisplay;
13612bd1dd39SThierry Reding 
13622bd1dd39SThierry Reding 	value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
1363880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state4 + dc->pipe);
13642bd1dd39SThierry Reding 
13652bd1dd39SThierry Reding 	/* XXX interlacing support */
1366880cee0bSThierry Reding 	tegra_sor_writel(sor, 0x001, sor->soc->regs->head_state5 + dc->pipe);
13672bd1dd39SThierry Reding }
13682bd1dd39SThierry Reding 
13696fad8f66SThierry Reding static int tegra_sor_detach(struct tegra_sor *sor)
13706b6b6042SThierry Reding {
13716fad8f66SThierry Reding 	unsigned long value, timeout;
13726fad8f66SThierry Reding 
13736fad8f66SThierry Reding 	/* switch to safe mode */
1374a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
13756fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_MODE_NORMAL;
1376a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
13776fad8f66SThierry Reding 	tegra_sor_super_update(sor);
13786fad8f66SThierry Reding 
13796fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
13806fad8f66SThierry Reding 
13816fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
13826fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
13836fad8f66SThierry Reding 		if (value & SOR_PWR_MODE_SAFE)
13846fad8f66SThierry Reding 			break;
13856fad8f66SThierry Reding 	}
13866fad8f66SThierry Reding 
13876fad8f66SThierry Reding 	if ((value & SOR_PWR_MODE_SAFE) == 0)
13886fad8f66SThierry Reding 		return -ETIMEDOUT;
13896fad8f66SThierry Reding 
13906fad8f66SThierry Reding 	/* go to sleep */
1391a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
13926fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
1393a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
13946fad8f66SThierry Reding 	tegra_sor_super_update(sor);
13956fad8f66SThierry Reding 
13966fad8f66SThierry Reding 	/* detach */
1397a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
13986fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_ATTACHED;
1399a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
14006fad8f66SThierry Reding 	tegra_sor_super_update(sor);
14016fad8f66SThierry Reding 
14026fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
14036fad8f66SThierry Reding 
14046fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
14056fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
14066fad8f66SThierry Reding 		if ((value & SOR_TEST_ATTACHED) == 0)
14076fad8f66SThierry Reding 			break;
14086fad8f66SThierry Reding 
14096fad8f66SThierry Reding 		usleep_range(25, 100);
14106fad8f66SThierry Reding 	}
14116fad8f66SThierry Reding 
14126fad8f66SThierry Reding 	if ((value & SOR_TEST_ATTACHED) != 0)
14136fad8f66SThierry Reding 		return -ETIMEDOUT;
14146fad8f66SThierry Reding 
14156fad8f66SThierry Reding 	return 0;
14166fad8f66SThierry Reding }
14176fad8f66SThierry Reding 
14186fad8f66SThierry Reding static int tegra_sor_power_down(struct tegra_sor *sor)
14196fad8f66SThierry Reding {
14206fad8f66SThierry Reding 	unsigned long value, timeout;
14216fad8f66SThierry Reding 	int err;
14226fad8f66SThierry Reding 
14236fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWR);
14246fad8f66SThierry Reding 	value &= ~SOR_PWR_NORMAL_STATE_PU;
14256fad8f66SThierry Reding 	value |= SOR_PWR_TRIGGER;
14266fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWR);
14276fad8f66SThierry Reding 
14286fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
14296fad8f66SThierry Reding 
14306fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
14316fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
14326fad8f66SThierry Reding 		if ((value & SOR_PWR_TRIGGER) == 0)
14336fad8f66SThierry Reding 			return 0;
14346fad8f66SThierry Reding 
14356fad8f66SThierry Reding 		usleep_range(25, 100);
14366fad8f66SThierry Reding 	}
14376fad8f66SThierry Reding 
14386fad8f66SThierry Reding 	if ((value & SOR_PWR_TRIGGER) != 0)
14396fad8f66SThierry Reding 		return -ETIMEDOUT;
14406fad8f66SThierry Reding 
144125bb2cecSThierry Reding 	/* switch to safe parent clock */
144225bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
1443e1335e2fSThierry Reding 	if (err < 0) {
14446fad8f66SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
1445e1335e2fSThierry Reding 		return err;
1446e1335e2fSThierry Reding 	}
14476fad8f66SThierry Reding 
1448880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1449a9a9e4fdSThierry Reding 	value |= SOR_PLL2_PORT_POWERDOWN;
1450880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
14516fad8f66SThierry Reding 
14526fad8f66SThierry Reding 	usleep_range(20, 100);
14536fad8f66SThierry Reding 
1454880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
1455a9a9e4fdSThierry Reding 	value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
1456880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
14576fad8f66SThierry Reding 
1458880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1459a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD;
1460a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1461880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
14626fad8f66SThierry Reding 
14636fad8f66SThierry Reding 	usleep_range(20, 100);
14646fad8f66SThierry Reding 
14656fad8f66SThierry Reding 	return 0;
14666fad8f66SThierry Reding }
14676fad8f66SThierry Reding 
14686fad8f66SThierry Reding static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
14696fad8f66SThierry Reding {
14706fad8f66SThierry Reding 	u32 value;
14716fad8f66SThierry Reding 
14726fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
14736fad8f66SThierry Reding 
14746fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
1475a9a9e4fdSThierry Reding 		value = tegra_sor_readl(sor, SOR_CRCA);
1476a9a9e4fdSThierry Reding 		if (value & SOR_CRCA_VALID)
14776fad8f66SThierry Reding 			return 0;
14786fad8f66SThierry Reding 
14796fad8f66SThierry Reding 		usleep_range(100, 200);
14806fad8f66SThierry Reding 	}
14816fad8f66SThierry Reding 
14826fad8f66SThierry Reding 	return -ETIMEDOUT;
14836fad8f66SThierry Reding }
14846fad8f66SThierry Reding 
1485530239a8SThierry Reding static int tegra_sor_show_crc(struct seq_file *s, void *data)
14866fad8f66SThierry Reding {
1487530239a8SThierry Reding 	struct drm_info_node *node = s->private;
1488530239a8SThierry Reding 	struct tegra_sor *sor = node->info_ent->data;
1489850bab44SThierry Reding 	struct drm_crtc *crtc = sor->output.encoder.crtc;
1490850bab44SThierry Reding 	struct drm_device *drm = node->minor->dev;
1491530239a8SThierry Reding 	int err = 0;
14926fad8f66SThierry Reding 	u32 value;
14936fad8f66SThierry Reding 
1494850bab44SThierry Reding 	drm_modeset_lock_all(drm);
14956fad8f66SThierry Reding 
1496850bab44SThierry Reding 	if (!crtc || !crtc->state->active) {
1497850bab44SThierry Reding 		err = -EBUSY;
14986fad8f66SThierry Reding 		goto unlock;
14996fad8f66SThierry Reding 	}
15006fad8f66SThierry Reding 
1501a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
15026fad8f66SThierry Reding 	value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
1503a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
15046fad8f66SThierry Reding 
15056fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
15066fad8f66SThierry Reding 	value |= SOR_CRC_CNTRL_ENABLE;
15076fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
15086fad8f66SThierry Reding 
15096fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_TEST);
15106fad8f66SThierry Reding 	value &= ~SOR_TEST_CRC_POST_SERIALIZE;
15116fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_TEST);
15126fad8f66SThierry Reding 
15136fad8f66SThierry Reding 	err = tegra_sor_crc_wait(sor, 100);
15146fad8f66SThierry Reding 	if (err < 0)
15156fad8f66SThierry Reding 		goto unlock;
15166fad8f66SThierry Reding 
1517a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, SOR_CRCA_RESET, SOR_CRCA);
1518a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_CRCB);
15196fad8f66SThierry Reding 
1520530239a8SThierry Reding 	seq_printf(s, "%08x\n", value);
15216fad8f66SThierry Reding 
15226fad8f66SThierry Reding unlock:
1523850bab44SThierry Reding 	drm_modeset_unlock_all(drm);
15246fad8f66SThierry Reding 	return err;
15256fad8f66SThierry Reding }
15266fad8f66SThierry Reding 
1527062f5b2cSThierry Reding #define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
1528062f5b2cSThierry Reding 
1529062f5b2cSThierry Reding static const struct debugfs_reg32 tegra_sor_regs[] = {
1530062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CTXSW),
1531062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SUPER_STATE0),
1532062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SUPER_STATE1),
1533062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_STATE0),
1534062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_STATE1),
1535062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE0(0)),
1536062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE0(1)),
1537062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE1(0)),
1538062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE1(1)),
1539062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE2(0)),
1540062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE2(1)),
1541062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE3(0)),
1542062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE3(1)),
1543062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE4(0)),
1544062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE4(1)),
1545062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE5(0)),
1546062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE5(1)),
1547062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CRC_CNTRL),
1548062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_DEBUG_MVID),
1549062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CLK_CNTRL),
1550062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CAP),
1551062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PWR),
1552062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_TEST),
1553062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL0),
1554062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL1),
1555062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL2),
1556062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL3),
1557062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CSTM),
1558062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LVDS),
1559062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CRCA),
1560062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CRCB),
1561062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_BLANK),
1562062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_CTL),
1563062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_SEQ_CTL),
1564062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(0)),
1565062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(1)),
1566062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(2)),
1567062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(3)),
1568062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(4)),
1569062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(5)),
1570062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(6)),
1571062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(7)),
1572062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(8)),
1573062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(9)),
1574062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(10)),
1575062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(11)),
1576062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(12)),
1577062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(13)),
1578062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(14)),
1579062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(15)),
1580062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PWM_DIV),
1581062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PWM_CTL),
1582062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_A0),
1583062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_A1),
1584062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_B0),
1585062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_B1),
1586062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_A0),
1587062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_A1),
1588062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_B0),
1589062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_B1),
1590062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_A0),
1591062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_A1),
1592062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_B0),
1593062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_B1),
1594062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_A0),
1595062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_A1),
1596062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_B0),
1597062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_B1),
1598062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_A0),
1599062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_A1),
1600062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_B0),
1601062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_B1),
1602062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_TRIG),
1603062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_MSCHECK),
1604062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_XBAR_CTRL),
1605062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_XBAR_POL),
1606062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LINKCTL0),
1607062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LINKCTL1),
1608062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT0),
1609062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT1),
1610062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT0),
1611062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT1),
1612062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_PREEMPHASIS0),
1613062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_PREEMPHASIS1),
1614062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS0),
1615062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS1),
1616062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_POSTCURSOR0),
1617062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_POSTCURSOR1),
1618062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_CONFIG0),
1619062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_CONFIG1),
1620062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_MN0),
1621062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_MN1),
1622062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_PADCTL0),
1623062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_PADCTL1),
1624c57997bcSThierry Reding 	DEBUGFS_REG32(SOR_DP_PADCTL2),
1625062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_DEBUG0),
1626062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_DEBUG1),
1627062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_SPARE0),
1628062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_SPARE1),
1629062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_AUDIO_CTRL),
1630062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_AUDIO_HBLANK_SYMBOLS),
1631062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_AUDIO_VBLANK_SYMBOLS),
1632062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_HEADER),
1633062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK0),
1634062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK1),
1635062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK2),
1636062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK3),
1637062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK4),
1638062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK5),
1639062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK6),
1640062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_TPG),
1641062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_TPG_CONFIG),
1642062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LQ_CSTM0),
1643062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LQ_CSTM1),
1644062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LQ_CSTM2),
1645062f5b2cSThierry Reding };
1646062f5b2cSThierry Reding 
1647dab16336SThierry Reding static int tegra_sor_show_regs(struct seq_file *s, void *data)
1648dab16336SThierry Reding {
1649dab16336SThierry Reding 	struct drm_info_node *node = s->private;
1650dab16336SThierry Reding 	struct tegra_sor *sor = node->info_ent->data;
1651850bab44SThierry Reding 	struct drm_crtc *crtc = sor->output.encoder.crtc;
1652850bab44SThierry Reding 	struct drm_device *drm = node->minor->dev;
1653062f5b2cSThierry Reding 	unsigned int i;
1654850bab44SThierry Reding 	int err = 0;
1655850bab44SThierry Reding 
1656850bab44SThierry Reding 	drm_modeset_lock_all(drm);
1657850bab44SThierry Reding 
1658850bab44SThierry Reding 	if (!crtc || !crtc->state->active) {
1659850bab44SThierry Reding 		err = -EBUSY;
1660850bab44SThierry Reding 		goto unlock;
1661850bab44SThierry Reding 	}
1662dab16336SThierry Reding 
1663062f5b2cSThierry Reding 	for (i = 0; i < ARRAY_SIZE(tegra_sor_regs); i++) {
1664062f5b2cSThierry Reding 		unsigned int offset = tegra_sor_regs[i].offset;
1665dab16336SThierry Reding 
1666062f5b2cSThierry Reding 		seq_printf(s, "%-38s %#05x %08x\n", tegra_sor_regs[i].name,
1667062f5b2cSThierry Reding 			   offset, tegra_sor_readl(sor, offset));
1668062f5b2cSThierry Reding 	}
1669dab16336SThierry Reding 
1670850bab44SThierry Reding unlock:
1671850bab44SThierry Reding 	drm_modeset_unlock_all(drm);
1672850bab44SThierry Reding 	return err;
1673dab16336SThierry Reding }
1674dab16336SThierry Reding 
1675dab16336SThierry Reding static const struct drm_info_list debugfs_files[] = {
1676530239a8SThierry Reding 	{ "crc", tegra_sor_show_crc, 0, NULL },
1677dab16336SThierry Reding 	{ "regs", tegra_sor_show_regs, 0, NULL },
1678dab16336SThierry Reding };
1679dab16336SThierry Reding 
16805b8e043bSThierry Reding static int tegra_sor_late_register(struct drm_connector *connector)
16816fad8f66SThierry Reding {
16825b8e043bSThierry Reding 	struct tegra_output *output = connector_to_output(connector);
16835b8e043bSThierry Reding 	unsigned int i, count = ARRAY_SIZE(debugfs_files);
16845b8e043bSThierry Reding 	struct drm_minor *minor = connector->dev->primary;
16855b8e043bSThierry Reding 	struct dentry *root = connector->debugfs_entry;
16865b8e043bSThierry Reding 	struct tegra_sor *sor = to_sor(output);
1687530239a8SThierry Reding 	int err;
16886fad8f66SThierry Reding 
1689dab16336SThierry Reding 	sor->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1690dab16336SThierry Reding 				     GFP_KERNEL);
16915b8e043bSThierry Reding 	if (!sor->debugfs_files)
16925b8e043bSThierry Reding 		return -ENOMEM;
16936fad8f66SThierry Reding 
16945b8e043bSThierry Reding 	for (i = 0; i < count; i++)
1695dab16336SThierry Reding 		sor->debugfs_files[i].data = sor;
1696dab16336SThierry Reding 
16975b8e043bSThierry Reding 	err = drm_debugfs_create_files(sor->debugfs_files, count, root, minor);
1698dab16336SThierry Reding 	if (err < 0)
1699dab16336SThierry Reding 		goto free;
1700dab16336SThierry Reding 
1701530239a8SThierry Reding 	return 0;
17026fad8f66SThierry Reding 
1703dab16336SThierry Reding free:
1704dab16336SThierry Reding 	kfree(sor->debugfs_files);
1705dab16336SThierry Reding 	sor->debugfs_files = NULL;
17065b8e043bSThierry Reding 
17076fad8f66SThierry Reding 	return err;
17086fad8f66SThierry Reding }
17096fad8f66SThierry Reding 
17105b8e043bSThierry Reding static void tegra_sor_early_unregister(struct drm_connector *connector)
17116fad8f66SThierry Reding {
17125b8e043bSThierry Reding 	struct tegra_output *output = connector_to_output(connector);
17135b8e043bSThierry Reding 	unsigned int count = ARRAY_SIZE(debugfs_files);
17145b8e043bSThierry Reding 	struct tegra_sor *sor = to_sor(output);
1715d92e6009SThierry Reding 
17165b8e043bSThierry Reding 	drm_debugfs_remove_files(sor->debugfs_files, count,
17175b8e043bSThierry Reding 				 connector->dev->primary);
1718dab16336SThierry Reding 	kfree(sor->debugfs_files);
1719066d30f8SThierry Reding 	sor->debugfs_files = NULL;
17206fad8f66SThierry Reding }
17216fad8f66SThierry Reding 
1722c31efa7aSThierry Reding static void tegra_sor_connector_reset(struct drm_connector *connector)
1723c31efa7aSThierry Reding {
1724c31efa7aSThierry Reding 	struct tegra_sor_state *state;
1725c31efa7aSThierry Reding 
1726c31efa7aSThierry Reding 	state = kzalloc(sizeof(*state), GFP_KERNEL);
1727c31efa7aSThierry Reding 	if (!state)
1728c31efa7aSThierry Reding 		return;
1729c31efa7aSThierry Reding 
1730c31efa7aSThierry Reding 	if (connector->state) {
1731c31efa7aSThierry Reding 		__drm_atomic_helper_connector_destroy_state(connector->state);
1732c31efa7aSThierry Reding 		kfree(connector->state);
1733c31efa7aSThierry Reding 	}
1734c31efa7aSThierry Reding 
1735c31efa7aSThierry Reding 	__drm_atomic_helper_connector_reset(connector, &state->base);
1736c31efa7aSThierry Reding }
1737c31efa7aSThierry Reding 
17386fad8f66SThierry Reding static enum drm_connector_status
17396fad8f66SThierry Reding tegra_sor_connector_detect(struct drm_connector *connector, bool force)
17406fad8f66SThierry Reding {
17416fad8f66SThierry Reding 	struct tegra_output *output = connector_to_output(connector);
17426fad8f66SThierry Reding 	struct tegra_sor *sor = to_sor(output);
17436fad8f66SThierry Reding 
17449542c237SThierry Reding 	if (sor->aux)
17459542c237SThierry Reding 		return drm_dp_aux_detect(sor->aux);
17466fad8f66SThierry Reding 
1747459cc2c6SThierry Reding 	return tegra_output_connector_detect(connector, force);
17486fad8f66SThierry Reding }
17496fad8f66SThierry Reding 
1750c31efa7aSThierry Reding static struct drm_connector_state *
1751c31efa7aSThierry Reding tegra_sor_connector_duplicate_state(struct drm_connector *connector)
1752c31efa7aSThierry Reding {
1753c31efa7aSThierry Reding 	struct tegra_sor_state *state = to_sor_state(connector->state);
1754c31efa7aSThierry Reding 	struct tegra_sor_state *copy;
1755c31efa7aSThierry Reding 
1756c31efa7aSThierry Reding 	copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
1757c31efa7aSThierry Reding 	if (!copy)
1758c31efa7aSThierry Reding 		return NULL;
1759c31efa7aSThierry Reding 
1760c31efa7aSThierry Reding 	__drm_atomic_helper_connector_duplicate_state(connector, &copy->base);
1761c31efa7aSThierry Reding 
1762c31efa7aSThierry Reding 	return &copy->base;
1763c31efa7aSThierry Reding }
1764c31efa7aSThierry Reding 
17656fad8f66SThierry Reding static const struct drm_connector_funcs tegra_sor_connector_funcs = {
1766c31efa7aSThierry Reding 	.reset = tegra_sor_connector_reset,
17676fad8f66SThierry Reding 	.detect = tegra_sor_connector_detect,
17686fad8f66SThierry Reding 	.fill_modes = drm_helper_probe_single_connector_modes,
17696fad8f66SThierry Reding 	.destroy = tegra_output_connector_destroy,
1770c31efa7aSThierry Reding 	.atomic_duplicate_state = tegra_sor_connector_duplicate_state,
17714aa3df71SThierry Reding 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
17725b8e043bSThierry Reding 	.late_register = tegra_sor_late_register,
17735b8e043bSThierry Reding 	.early_unregister = tegra_sor_early_unregister,
17746fad8f66SThierry Reding };
17756fad8f66SThierry Reding 
17766fad8f66SThierry Reding static int tegra_sor_connector_get_modes(struct drm_connector *connector)
17776fad8f66SThierry Reding {
17786fad8f66SThierry Reding 	struct tegra_output *output = connector_to_output(connector);
17796fad8f66SThierry Reding 	struct tegra_sor *sor = to_sor(output);
17806fad8f66SThierry Reding 	int err;
17816fad8f66SThierry Reding 
17829542c237SThierry Reding 	if (sor->aux)
17839542c237SThierry Reding 		drm_dp_aux_enable(sor->aux);
17846fad8f66SThierry Reding 
17856fad8f66SThierry Reding 	err = tegra_output_connector_get_modes(connector);
17866fad8f66SThierry Reding 
17879542c237SThierry Reding 	if (sor->aux)
17889542c237SThierry Reding 		drm_dp_aux_disable(sor->aux);
17896fad8f66SThierry Reding 
17906fad8f66SThierry Reding 	return err;
17916fad8f66SThierry Reding }
17926fad8f66SThierry Reding 
17936fad8f66SThierry Reding static enum drm_mode_status
17946fad8f66SThierry Reding tegra_sor_connector_mode_valid(struct drm_connector *connector,
17956fad8f66SThierry Reding 			       struct drm_display_mode *mode)
17966fad8f66SThierry Reding {
17976fad8f66SThierry Reding 	return MODE_OK;
17986fad8f66SThierry Reding }
17996fad8f66SThierry Reding 
18006fad8f66SThierry Reding static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
18016fad8f66SThierry Reding 	.get_modes = tegra_sor_connector_get_modes,
18026fad8f66SThierry Reding 	.mode_valid = tegra_sor_connector_mode_valid,
18036fad8f66SThierry Reding };
18046fad8f66SThierry Reding 
18056fad8f66SThierry Reding static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
18066fad8f66SThierry Reding 	.destroy = tegra_output_encoder_destroy,
18076fad8f66SThierry Reding };
18086fad8f66SThierry Reding 
1809850bab44SThierry Reding static void tegra_sor_edp_disable(struct drm_encoder *encoder)
18106fad8f66SThierry Reding {
1811850bab44SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
1812850bab44SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1813850bab44SThierry Reding 	struct tegra_sor *sor = to_sor(output);
1814850bab44SThierry Reding 	u32 value;
1815850bab44SThierry Reding 	int err;
1816850bab44SThierry Reding 
1817850bab44SThierry Reding 	if (output->panel)
1818850bab44SThierry Reding 		drm_panel_disable(output->panel);
1819850bab44SThierry Reding 
1820850bab44SThierry Reding 	err = tegra_sor_detach(sor);
1821850bab44SThierry Reding 	if (err < 0)
1822850bab44SThierry Reding 		dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1823850bab44SThierry Reding 
1824850bab44SThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE1);
1825850bab44SThierry Reding 	tegra_sor_update(sor);
1826850bab44SThierry Reding 
1827850bab44SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1828c57997bcSThierry Reding 	value &= ~SOR_ENABLE(0);
1829850bab44SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1830850bab44SThierry Reding 
1831850bab44SThierry Reding 	tegra_dc_commit(dc);
18326fad8f66SThierry Reding 
1833850bab44SThierry Reding 	err = tegra_sor_power_down(sor);
1834850bab44SThierry Reding 	if (err < 0)
1835850bab44SThierry Reding 		dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1836850bab44SThierry Reding 
18379542c237SThierry Reding 	if (sor->aux) {
18389542c237SThierry Reding 		err = drm_dp_aux_disable(sor->aux);
1839850bab44SThierry Reding 		if (err < 0)
1840850bab44SThierry Reding 			dev_err(sor->dev, "failed to disable DP: %d\n", err);
18416fad8f66SThierry Reding 	}
18426fad8f66SThierry Reding 
1843c57997bcSThierry Reding 	err = tegra_io_pad_power_disable(sor->pad);
1844850bab44SThierry Reding 	if (err < 0)
1845c57997bcSThierry Reding 		dev_err(sor->dev, "failed to power off I/O pad: %d\n", err);
1846850bab44SThierry Reding 
1847850bab44SThierry Reding 	if (output->panel)
1848850bab44SThierry Reding 		drm_panel_unprepare(output->panel);
1849850bab44SThierry Reding 
1850aaff8bd2SThierry Reding 	pm_runtime_put(sor->dev);
18516fad8f66SThierry Reding }
18526fad8f66SThierry Reding 
1853459cc2c6SThierry Reding #if 0
1854459cc2c6SThierry Reding static int calc_h_ref_to_sync(const struct drm_display_mode *mode,
1855459cc2c6SThierry Reding 			      unsigned int *value)
1856459cc2c6SThierry Reding {
1857459cc2c6SThierry Reding 	unsigned int hfp, hsw, hbp, a = 0, b;
1858459cc2c6SThierry Reding 
1859459cc2c6SThierry Reding 	hfp = mode->hsync_start - mode->hdisplay;
1860459cc2c6SThierry Reding 	hsw = mode->hsync_end - mode->hsync_start;
1861459cc2c6SThierry Reding 	hbp = mode->htotal - mode->hsync_end;
1862459cc2c6SThierry Reding 
1863459cc2c6SThierry Reding 	pr_info("hfp: %u, hsw: %u, hbp: %u\n", hfp, hsw, hbp);
1864459cc2c6SThierry Reding 
1865459cc2c6SThierry Reding 	b = hfp - 1;
1866459cc2c6SThierry Reding 
1867459cc2c6SThierry Reding 	pr_info("a: %u, b: %u\n", a, b);
1868459cc2c6SThierry Reding 	pr_info("a + hsw + hbp = %u\n", a + hsw + hbp);
1869459cc2c6SThierry Reding 
1870459cc2c6SThierry Reding 	if (a + hsw + hbp <= 11) {
1871459cc2c6SThierry Reding 		a = 1 + 11 - hsw - hbp;
1872459cc2c6SThierry Reding 		pr_info("a: %u\n", a);
1873459cc2c6SThierry Reding 	}
1874459cc2c6SThierry Reding 
1875459cc2c6SThierry Reding 	if (a > b)
1876459cc2c6SThierry Reding 		return -EINVAL;
1877459cc2c6SThierry Reding 
1878459cc2c6SThierry Reding 	if (hsw < 1)
1879459cc2c6SThierry Reding 		return -EINVAL;
1880459cc2c6SThierry Reding 
1881459cc2c6SThierry Reding 	if (mode->hdisplay < 16)
1882459cc2c6SThierry Reding 		return -EINVAL;
1883459cc2c6SThierry Reding 
1884459cc2c6SThierry Reding 	if (value) {
1885459cc2c6SThierry Reding 		if (b > a && a % 2)
1886459cc2c6SThierry Reding 			*value = a + 1;
1887459cc2c6SThierry Reding 		else
1888459cc2c6SThierry Reding 			*value = a;
1889459cc2c6SThierry Reding 	}
1890459cc2c6SThierry Reding 
1891459cc2c6SThierry Reding 	return 0;
1892459cc2c6SThierry Reding }
1893459cc2c6SThierry Reding #endif
1894459cc2c6SThierry Reding 
1895850bab44SThierry Reding static void tegra_sor_edp_enable(struct drm_encoder *encoder)
18966fad8f66SThierry Reding {
18976fad8f66SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
18986fad8f66SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
18996b6b6042SThierry Reding 	struct tegra_sor *sor = to_sor(output);
190034fa183bSThierry Reding 	struct tegra_sor_config config;
1901c31efa7aSThierry Reding 	struct tegra_sor_state *state;
1902c1763937SThierry Reding 	struct drm_display_mode *mode;
1903c1763937SThierry Reding 	struct drm_display_info *info;
19042bd1dd39SThierry Reding 	unsigned int i;
190528fe2076SThierry Reding 	u32 value;
1906c1763937SThierry Reding 	int err;
190786f5c52dSThierry Reding 
1908c31efa7aSThierry Reding 	state = to_sor_state(output->connector.state);
1909c1763937SThierry Reding 	mode = &encoder->crtc->state->adjusted_mode;
1910c1763937SThierry Reding 	info = &output->connector.display_info;
19116b6b6042SThierry Reding 
1912aaff8bd2SThierry Reding 	pm_runtime_get_sync(sor->dev);
19136b6b6042SThierry Reding 
191425bb2cecSThierry Reding 	/* switch to safe parent clock */
191525bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
19166b6b6042SThierry Reding 	if (err < 0)
19176b6b6042SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
19186b6b6042SThierry Reding 
191938b445bcSThierry Reding 	err = tegra_io_rail_power_on(TEGRA_IO_RAIL_LVDS);
192038b445bcSThierry Reding 	if (err < 0)
192138b445bcSThierry Reding 		dev_err(sor->dev, "failed to power on LVDS rail: %d\n", err);
192238b445bcSThierry Reding 
192338b445bcSThierry Reding 	usleep_range(20, 100);
192438b445bcSThierry Reding 
192538b445bcSThierry Reding 	err = drm_dp_aux_enable(sor->aux);
192638b445bcSThierry Reding 	if (err < 0)
192738b445bcSThierry Reding 		dev_err(sor->dev, "failed to enable DPAUX: %d\n", err);
192838b445bcSThierry Reding 
192938b445bcSThierry Reding 	err = drm_dp_link_probe(sor->aux, &sor->link);
193038b445bcSThierry Reding 	if (err < 0)
193138b445bcSThierry Reding 		dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
193238b445bcSThierry Reding 
1933c9533131SThierry Reding 	tegra_sor_filter_rates(sor);
1934c9533131SThierry Reding 
193538b445bcSThierry Reding 	err = drm_dp_link_choose(&sor->link, mode, info);
193638b445bcSThierry Reding 	if (err < 0)
193738b445bcSThierry Reding 		dev_err(sor->dev, "failed to choose link: %d\n", err);
193838b445bcSThierry Reding 
193938b445bcSThierry Reding 	if (output->panel)
194038b445bcSThierry Reding 		drm_panel_prepare(output->panel);
19416b6b6042SThierry Reding 
1942880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1943a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1944880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
194538b445bcSThierry Reding 
194638b445bcSThierry Reding 	usleep_range(20, 40);
19476b6b6042SThierry Reding 
1948880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll3);
1949a9a9e4fdSThierry Reding 	value |= SOR_PLL3_PLL_VDD_MODE_3V3;
1950880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll3);
19516b6b6042SThierry Reding 
195238b445bcSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
195338b445bcSThierry Reding 	value &= ~(SOR_PLL0_VCOPD | SOR_PLL0_PWR);
1954880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
19556b6b6042SThierry Reding 
1956880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1957a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
195838b445bcSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD;
1959880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
19606b6b6042SThierry Reding 
196138b445bcSThierry Reding 	usleep_range(200, 400);
19626b6b6042SThierry Reding 
1963880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1964a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
1965a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
1966880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
19676b6b6042SThierry Reding 
19686b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
196938b445bcSThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
197038b445bcSThierry Reding 	value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
19716b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
19726b6b6042SThierry Reding 
197338b445bcSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
197438b445bcSThierry Reding 	/* XXX not in TRM */
197538b445bcSThierry Reding 	value |= SOR_DP_SPARE_PANEL_INTERNAL;
197638b445bcSThierry Reding 	value |= SOR_DP_SPARE_SEQ_ENABLE;
197738b445bcSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
197838b445bcSThierry Reding 
197938b445bcSThierry Reding 	/* XXX not in TRM */
198038b445bcSThierry Reding 	tegra_sor_writel(sor, 0, SOR_LVDS);
19816b6b6042SThierry Reding 
1982880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
198338b445bcSThierry Reding 	value &= ~SOR_PLL0_ICHPMP_MASK;
198438b445bcSThierry Reding 	value &= ~SOR_PLL0_VCOCAP_MASK;
198538b445bcSThierry Reding 	value |= SOR_PLL0_ICHPMP(0x1);
198638b445bcSThierry Reding 	value |= SOR_PLL0_VCOCAP(0x3);
198738b445bcSThierry Reding 	value |= SOR_PLL0_RESISTOR_EXT;
1988880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
19896b6b6042SThierry Reding 
199030b49435SThierry Reding 	/* XXX not in TRM */
199130b49435SThierry Reding 	for (value = 0, i = 0; i < 5; i++)
19926d6c815dSThierry Reding 		value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->xbar_cfg[i]) |
199330b49435SThierry Reding 			 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
199430b49435SThierry Reding 
199530b49435SThierry Reding 	tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
199630b49435SThierry Reding 	tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
199730b49435SThierry Reding 
199861417aaaSThierry Reding 	/*
199961417aaaSThierry Reding 	 * Switch the pad clock to the DP clock. Note that we cannot actually
200061417aaaSThierry Reding 	 * do this because Tegra186 and later don't support clk_set_parent()
200161417aaaSThierry Reding 	 * on the sorX_pad_clkout clocks. We already do the equivalent above
200261417aaaSThierry Reding 	 * using the DP_CLK_SEL mux of the SOR_CLK_CNTRL register.
200361417aaaSThierry Reding 	 */
200461417aaaSThierry Reding #if 0
200561417aaaSThierry Reding 	err = clk_set_parent(sor->clk_pad, sor->clk_dp);
200661417aaaSThierry Reding 	if (err < 0) {
200761417aaaSThierry Reding 		dev_err(sor->dev, "failed to select pad parent clock: %d\n",
200861417aaaSThierry Reding 			err);
200961417aaaSThierry Reding 		return;
201061417aaaSThierry Reding 	}
201161417aaaSThierry Reding #endif
201261417aaaSThierry Reding 
201361417aaaSThierry Reding 	/* switch the SOR clock to the pad clock */
201461417aaaSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_pad);
201561417aaaSThierry Reding 	if (err < 0) {
201661417aaaSThierry Reding 		dev_err(sor->dev, "failed to select SOR parent clock: %d\n",
201761417aaaSThierry Reding 			err);
201861417aaaSThierry Reding 		return;
201961417aaaSThierry Reding 	}
202061417aaaSThierry Reding 
202161417aaaSThierry Reding 	/* switch the output clock to the parent pixel clock */
202261417aaaSThierry Reding 	err = clk_set_parent(sor->clk, sor->clk_parent);
202361417aaaSThierry Reding 	if (err < 0) {
202461417aaaSThierry Reding 		dev_err(sor->dev, "failed to select output parent clock: %d\n",
202561417aaaSThierry Reding 			err);
202661417aaaSThierry Reding 		return;
202761417aaaSThierry Reding 	}
20286b6b6042SThierry Reding 
2029c1763937SThierry Reding 	/* use DP-A protocol */
2030c1763937SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
2031c1763937SThierry Reding 	value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
2032c1763937SThierry Reding 	value |= SOR_STATE_ASY_PROTOCOL_DP_A;
2033c1763937SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
2034899451b7SThierry Reding 
2035c1763937SThierry Reding 	/* enable port */
2036a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
20376b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_ENABLE;
2038a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
20396b6b6042SThierry Reding 
2040c1763937SThierry Reding 	tegra_sor_dp_term_calibrate(sor);
2041c1763937SThierry Reding 
2042c1763937SThierry Reding 	err = drm_dp_link_train(&sor->link);
2043c1763937SThierry Reding 	if (err < 0)
2044c1763937SThierry Reding 		dev_err(sor->dev, "link training failed: %d\n", err);
2045c1763937SThierry Reding 	else
2046c1763937SThierry Reding 		dev_dbg(sor->dev, "link training succeeded\n");
2047c1763937SThierry Reding 
2048c1763937SThierry Reding 	err = drm_dp_link_power_up(sor->aux, &sor->link);
204938b445bcSThierry Reding 	if (err < 0)
205038b445bcSThierry Reding 		dev_err(sor->dev, "failed to power up eDP link: %d\n", err);
20516b6b6042SThierry Reding 
2052c1763937SThierry Reding 	/* compute configuration */
2053c1763937SThierry Reding 	memset(&config, 0, sizeof(config));
2054c1763937SThierry Reding 	config.bits_per_pixel = state->bpc * 3;
20556b6b6042SThierry Reding 
2056c1763937SThierry Reding 	err = tegra_sor_compute_config(sor, mode, &config, &sor->link);
205738b445bcSThierry Reding 	if (err < 0)
2058c1763937SThierry Reding 		dev_err(sor->dev, "failed to compute configuration: %d\n", err);
20596b6b6042SThierry Reding 
2060c1763937SThierry Reding 	tegra_sor_apply_config(sor, &config);
20616b6b6042SThierry Reding 
20626b6b6042SThierry Reding 	err = tegra_sor_power_up(sor, 250);
2063850bab44SThierry Reding 	if (err < 0)
20646b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
20656b6b6042SThierry Reding 
20666b6b6042SThierry Reding 	/* CSTM (LVDS, link A/B, upper) */
2067143b1df2SStéphane Marchesin 	value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
20686b6b6042SThierry Reding 		SOR_CSTM_UPPER;
20696b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CSTM);
20706b6b6042SThierry Reding 
20712bd1dd39SThierry Reding 	/* use DP-A protocol */
20722bd1dd39SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
20732bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
20742bd1dd39SThierry Reding 	value |= SOR_STATE_ASY_PROTOCOL_DP_A;
20752bd1dd39SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
20762bd1dd39SThierry Reding 
2077c31efa7aSThierry Reding 	tegra_sor_mode_set(sor, mode, state);
20782bd1dd39SThierry Reding 
20796b6b6042SThierry Reding 	/* PWM setup */
20806b6b6042SThierry Reding 	err = tegra_sor_setup_pwm(sor, 250);
2081850bab44SThierry Reding 	if (err < 0)
20826b6b6042SThierry Reding 		dev_err(sor->dev, "failed to setup PWM: %d\n", err);
20836b6b6042SThierry Reding 
2084666cb873SThierry Reding 	tegra_sor_update(sor);
2085666cb873SThierry Reding 
208638b445bcSThierry Reding 	err = tegra_sor_power_up(sor, 250);
208738b445bcSThierry Reding 	if (err < 0)
208838b445bcSThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
208938b445bcSThierry Reding 
209038b445bcSThierry Reding 	/* attach and wake up */
209138b445bcSThierry Reding 	err = tegra_sor_attach(sor);
209238b445bcSThierry Reding 	if (err < 0)
209338b445bcSThierry Reding 		dev_err(sor->dev, "failed to attach SOR: %d\n", err);
209438b445bcSThierry Reding 
20956b6b6042SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
2096c57997bcSThierry Reding 	value |= SOR_ENABLE(0);
20976b6b6042SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
20986b6b6042SThierry Reding 
2099666cb873SThierry Reding 	tegra_dc_commit(dc);
21006b6b6042SThierry Reding 
21016b6b6042SThierry Reding 	err = tegra_sor_wakeup(sor);
2102850bab44SThierry Reding 	if (err < 0)
210338b445bcSThierry Reding 		dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
21046b6b6042SThierry Reding 
21056fad8f66SThierry Reding 	if (output->panel)
21066fad8f66SThierry Reding 		drm_panel_enable(output->panel);
21076b6b6042SThierry Reding }
21086b6b6042SThierry Reding 
210982f1511cSThierry Reding static int
211082f1511cSThierry Reding tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
211182f1511cSThierry Reding 			       struct drm_crtc_state *crtc_state,
211282f1511cSThierry Reding 			       struct drm_connector_state *conn_state)
211382f1511cSThierry Reding {
211482f1511cSThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
2115c31efa7aSThierry Reding 	struct tegra_sor_state *state = to_sor_state(conn_state);
211682f1511cSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
211782f1511cSThierry Reding 	unsigned long pclk = crtc_state->mode.clock * 1000;
211882f1511cSThierry Reding 	struct tegra_sor *sor = to_sor(output);
2119c31efa7aSThierry Reding 	struct drm_display_info *info;
212082f1511cSThierry Reding 	int err;
212182f1511cSThierry Reding 
2122c31efa7aSThierry Reding 	info = &output->connector.display_info;
2123c31efa7aSThierry Reding 
212436e90221SThierry Reding 	/*
212536e90221SThierry Reding 	 * For HBR2 modes, the SOR brick needs to use the x20 multiplier, so
212636e90221SThierry Reding 	 * the pixel clock must be corrected accordingly.
212736e90221SThierry Reding 	 */
212836e90221SThierry Reding 	if (pclk >= 340000000) {
212936e90221SThierry Reding 		state->link_speed = 20;
213036e90221SThierry Reding 		state->pclk = pclk / 2;
213136e90221SThierry Reding 	} else {
213236e90221SThierry Reding 		state->link_speed = 10;
213336e90221SThierry Reding 		state->pclk = pclk;
213436e90221SThierry Reding 	}
213536e90221SThierry Reding 
213682f1511cSThierry Reding 	err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
213782f1511cSThierry Reding 					 pclk, 0);
213882f1511cSThierry Reding 	if (err < 0) {
213982f1511cSThierry Reding 		dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
214082f1511cSThierry Reding 		return err;
214182f1511cSThierry Reding 	}
214282f1511cSThierry Reding 
2143c31efa7aSThierry Reding 	switch (info->bpc) {
2144c31efa7aSThierry Reding 	case 8:
2145c31efa7aSThierry Reding 	case 6:
2146c31efa7aSThierry Reding 		state->bpc = info->bpc;
2147c31efa7aSThierry Reding 		break;
2148c31efa7aSThierry Reding 
2149c31efa7aSThierry Reding 	default:
2150c31efa7aSThierry Reding 		DRM_DEBUG_KMS("%u bits-per-color not supported\n", info->bpc);
2151c31efa7aSThierry Reding 		state->bpc = 8;
2152c31efa7aSThierry Reding 		break;
2153c31efa7aSThierry Reding 	}
2154c31efa7aSThierry Reding 
215582f1511cSThierry Reding 	return 0;
215682f1511cSThierry Reding }
215782f1511cSThierry Reding 
2158459cc2c6SThierry Reding static const struct drm_encoder_helper_funcs tegra_sor_edp_helpers = {
2159850bab44SThierry Reding 	.disable = tegra_sor_edp_disable,
2160850bab44SThierry Reding 	.enable = tegra_sor_edp_enable,
216182f1511cSThierry Reding 	.atomic_check = tegra_sor_encoder_atomic_check,
21626b6b6042SThierry Reding };
21636b6b6042SThierry Reding 
2164459cc2c6SThierry Reding static inline u32 tegra_sor_hdmi_subpack(const u8 *ptr, size_t size)
2165459cc2c6SThierry Reding {
2166459cc2c6SThierry Reding 	u32 value = 0;
2167459cc2c6SThierry Reding 	size_t i;
2168459cc2c6SThierry Reding 
2169459cc2c6SThierry Reding 	for (i = size; i > 0; i--)
2170459cc2c6SThierry Reding 		value = (value << 8) | ptr[i - 1];
2171459cc2c6SThierry Reding 
2172459cc2c6SThierry Reding 	return value;
2173459cc2c6SThierry Reding }
2174459cc2c6SThierry Reding 
2175459cc2c6SThierry Reding static void tegra_sor_hdmi_write_infopack(struct tegra_sor *sor,
2176459cc2c6SThierry Reding 					  const void *data, size_t size)
2177459cc2c6SThierry Reding {
2178459cc2c6SThierry Reding 	const u8 *ptr = data;
2179459cc2c6SThierry Reding 	unsigned long offset;
2180459cc2c6SThierry Reding 	size_t i, j;
2181459cc2c6SThierry Reding 	u32 value;
2182459cc2c6SThierry Reding 
2183459cc2c6SThierry Reding 	switch (ptr[0]) {
2184459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_AVI:
2185459cc2c6SThierry Reding 		offset = SOR_HDMI_AVI_INFOFRAME_HEADER;
2186459cc2c6SThierry Reding 		break;
2187459cc2c6SThierry Reding 
2188459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_AUDIO:
2189459cc2c6SThierry Reding 		offset = SOR_HDMI_AUDIO_INFOFRAME_HEADER;
2190459cc2c6SThierry Reding 		break;
2191459cc2c6SThierry Reding 
2192459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_VENDOR:
2193459cc2c6SThierry Reding 		offset = SOR_HDMI_VSI_INFOFRAME_HEADER;
2194459cc2c6SThierry Reding 		break;
2195459cc2c6SThierry Reding 
2196459cc2c6SThierry Reding 	default:
2197459cc2c6SThierry Reding 		dev_err(sor->dev, "unsupported infoframe type: %02x\n",
2198459cc2c6SThierry Reding 			ptr[0]);
2199459cc2c6SThierry Reding 		return;
2200459cc2c6SThierry Reding 	}
2201459cc2c6SThierry Reding 
2202459cc2c6SThierry Reding 	value = INFOFRAME_HEADER_TYPE(ptr[0]) |
2203459cc2c6SThierry Reding 		INFOFRAME_HEADER_VERSION(ptr[1]) |
2204459cc2c6SThierry Reding 		INFOFRAME_HEADER_LEN(ptr[2]);
2205459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, offset);
2206459cc2c6SThierry Reding 	offset++;
2207459cc2c6SThierry Reding 
2208459cc2c6SThierry Reding 	/*
2209459cc2c6SThierry Reding 	 * Each subpack contains 7 bytes, divided into:
2210459cc2c6SThierry Reding 	 * - subpack_low: bytes 0 - 3
2211459cc2c6SThierry Reding 	 * - subpack_high: bytes 4 - 6 (with byte 7 padded to 0x00)
2212459cc2c6SThierry Reding 	 */
2213459cc2c6SThierry Reding 	for (i = 3, j = 0; i < size; i += 7, j += 8) {
2214459cc2c6SThierry Reding 		size_t rem = size - i, num = min_t(size_t, rem, 4);
2215459cc2c6SThierry Reding 
2216459cc2c6SThierry Reding 		value = tegra_sor_hdmi_subpack(&ptr[i], num);
2217459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, offset++);
2218459cc2c6SThierry Reding 
2219459cc2c6SThierry Reding 		num = min_t(size_t, rem - num, 3);
2220459cc2c6SThierry Reding 
2221459cc2c6SThierry Reding 		value = tegra_sor_hdmi_subpack(&ptr[i + 4], num);
2222459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, offset++);
2223459cc2c6SThierry Reding 	}
2224459cc2c6SThierry Reding }
2225459cc2c6SThierry Reding 
2226459cc2c6SThierry Reding static int
2227459cc2c6SThierry Reding tegra_sor_hdmi_setup_avi_infoframe(struct tegra_sor *sor,
2228459cc2c6SThierry Reding 				   const struct drm_display_mode *mode)
2229459cc2c6SThierry Reding {
2230459cc2c6SThierry Reding 	u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
2231459cc2c6SThierry Reding 	struct hdmi_avi_infoframe frame;
2232459cc2c6SThierry Reding 	u32 value;
2233459cc2c6SThierry Reding 	int err;
2234459cc2c6SThierry Reding 
2235459cc2c6SThierry Reding 	/* disable AVI infoframe */
2236459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
2237459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_SINGLE;
2238459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_OTHER;
2239459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_ENABLE;
2240459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
2241459cc2c6SThierry Reding 
224213d0add3SVille Syrjälä 	err = drm_hdmi_avi_infoframe_from_display_mode(&frame,
224313d0add3SVille Syrjälä 						       &sor->output.connector, mode);
2244459cc2c6SThierry Reding 	if (err < 0) {
2245459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
2246459cc2c6SThierry Reding 		return err;
2247459cc2c6SThierry Reding 	}
2248459cc2c6SThierry Reding 
2249459cc2c6SThierry Reding 	err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
2250459cc2c6SThierry Reding 	if (err < 0) {
2251459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to pack AVI infoframe: %d\n", err);
2252459cc2c6SThierry Reding 		return err;
2253459cc2c6SThierry Reding 	}
2254459cc2c6SThierry Reding 
2255459cc2c6SThierry Reding 	tegra_sor_hdmi_write_infopack(sor, buffer, err);
2256459cc2c6SThierry Reding 
2257459cc2c6SThierry Reding 	/* enable AVI infoframe */
2258459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
2259459cc2c6SThierry Reding 	value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
2260459cc2c6SThierry Reding 	value |= INFOFRAME_CTRL_ENABLE;
2261459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
2262459cc2c6SThierry Reding 
2263459cc2c6SThierry Reding 	return 0;
2264459cc2c6SThierry Reding }
2265459cc2c6SThierry Reding 
22668e2988a7SThierry Reding static void tegra_sor_write_eld(struct tegra_sor *sor)
22678e2988a7SThierry Reding {
22688e2988a7SThierry Reding 	size_t length = drm_eld_size(sor->output.connector.eld), i;
22698e2988a7SThierry Reding 
22708e2988a7SThierry Reding 	for (i = 0; i < length; i++)
22718e2988a7SThierry Reding 		tegra_sor_writel(sor, i << 8 | sor->output.connector.eld[i],
22728e2988a7SThierry Reding 				 SOR_AUDIO_HDA_ELD_BUFWR);
22738e2988a7SThierry Reding 
22748e2988a7SThierry Reding 	/*
22758e2988a7SThierry Reding 	 * The HDA codec will always report an ELD buffer size of 96 bytes and
22768e2988a7SThierry Reding 	 * the HDA codec driver will check that each byte read from the buffer
22778e2988a7SThierry Reding 	 * is valid. Therefore every byte must be written, even if no 96 bytes
22788e2988a7SThierry Reding 	 * were parsed from EDID.
22798e2988a7SThierry Reding 	 */
22808e2988a7SThierry Reding 	for (i = length; i < 96; i++)
22818e2988a7SThierry Reding 		tegra_sor_writel(sor, i << 8 | 0, SOR_AUDIO_HDA_ELD_BUFWR);
22828e2988a7SThierry Reding }
22838e2988a7SThierry Reding 
22848e2988a7SThierry Reding static void tegra_sor_audio_prepare(struct tegra_sor *sor)
22858e2988a7SThierry Reding {
22868e2988a7SThierry Reding 	u32 value;
22878e2988a7SThierry Reding 
2288f1f20eb9SThierry Reding 	/*
2289f1f20eb9SThierry Reding 	 * Enable and unmask the HDA codec SCRATCH0 register interrupt. This
2290f1f20eb9SThierry Reding 	 * is used for interoperability between the HDA codec driver and the
2291f1f20eb9SThierry Reding 	 * HDMI/DP driver.
2292f1f20eb9SThierry Reding 	 */
2293f1f20eb9SThierry Reding 	value = SOR_INT_CODEC_SCRATCH1 | SOR_INT_CODEC_SCRATCH0;
2294f1f20eb9SThierry Reding 	tegra_sor_writel(sor, value, SOR_INT_ENABLE);
2295f1f20eb9SThierry Reding 	tegra_sor_writel(sor, value, SOR_INT_MASK);
2296f1f20eb9SThierry Reding 
22978e2988a7SThierry Reding 	tegra_sor_write_eld(sor);
22988e2988a7SThierry Reding 
22998e2988a7SThierry Reding 	value = SOR_AUDIO_HDA_PRESENSE_ELDV | SOR_AUDIO_HDA_PRESENSE_PD;
23008e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_AUDIO_HDA_PRESENSE);
23018e2988a7SThierry Reding }
23028e2988a7SThierry Reding 
23038e2988a7SThierry Reding static void tegra_sor_audio_unprepare(struct tegra_sor *sor)
23048e2988a7SThierry Reding {
23058e2988a7SThierry Reding 	tegra_sor_writel(sor, 0, SOR_AUDIO_HDA_PRESENSE);
2306f1f20eb9SThierry Reding 	tegra_sor_writel(sor, 0, SOR_INT_MASK);
2307f1f20eb9SThierry Reding 	tegra_sor_writel(sor, 0, SOR_INT_ENABLE);
23088e2988a7SThierry Reding }
23098e2988a7SThierry Reding 
23108e2988a7SThierry Reding static int tegra_sor_hdmi_enable_audio_infoframe(struct tegra_sor *sor)
23118e2988a7SThierry Reding {
23128e2988a7SThierry Reding 	u8 buffer[HDMI_INFOFRAME_SIZE(AUDIO)];
23138e2988a7SThierry Reding 	struct hdmi_audio_infoframe frame;
23148e2988a7SThierry Reding 	u32 value;
23158e2988a7SThierry Reding 	int err;
23168e2988a7SThierry Reding 
23178e2988a7SThierry Reding 	err = hdmi_audio_infoframe_init(&frame);
23188e2988a7SThierry Reding 	if (err < 0) {
23198e2988a7SThierry Reding 		dev_err(sor->dev, "failed to setup audio infoframe: %d\n", err);
23208e2988a7SThierry Reding 		return err;
23218e2988a7SThierry Reding 	}
23228e2988a7SThierry Reding 
2323fad7b806SThierry Reding 	frame.channels = sor->format.channels;
23248e2988a7SThierry Reding 
23258e2988a7SThierry Reding 	err = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
23268e2988a7SThierry Reding 	if (err < 0) {
23278e2988a7SThierry Reding 		dev_err(sor->dev, "failed to pack audio infoframe: %d\n", err);
23288e2988a7SThierry Reding 		return err;
23298e2988a7SThierry Reding 	}
23308e2988a7SThierry Reding 
23318e2988a7SThierry Reding 	tegra_sor_hdmi_write_infopack(sor, buffer, err);
23328e2988a7SThierry Reding 
23338e2988a7SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
23348e2988a7SThierry Reding 	value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
23358e2988a7SThierry Reding 	value |= INFOFRAME_CTRL_ENABLE;
23368e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
23378e2988a7SThierry Reding 
23388e2988a7SThierry Reding 	return 0;
23398e2988a7SThierry Reding }
23408e2988a7SThierry Reding 
23418e2988a7SThierry Reding static void tegra_sor_hdmi_audio_enable(struct tegra_sor *sor)
23428e2988a7SThierry Reding {
23438e2988a7SThierry Reding 	u32 value;
23448e2988a7SThierry Reding 
23458e2988a7SThierry Reding 	value = tegra_sor_readl(sor, SOR_AUDIO_CNTRL);
23468e2988a7SThierry Reding 
23478e2988a7SThierry Reding 	/* select HDA audio input */
23488e2988a7SThierry Reding 	value &= ~SOR_AUDIO_CNTRL_SOURCE_SELECT(SOURCE_SELECT_MASK);
23498e2988a7SThierry Reding 	value |= SOR_AUDIO_CNTRL_SOURCE_SELECT(SOURCE_SELECT_HDA);
23508e2988a7SThierry Reding 
23518e2988a7SThierry Reding 	/* inject null samples */
2352fad7b806SThierry Reding 	if (sor->format.channels != 2)
23538e2988a7SThierry Reding 		value &= ~SOR_AUDIO_CNTRL_INJECT_NULLSMPL;
23548e2988a7SThierry Reding 	else
23558e2988a7SThierry Reding 		value |= SOR_AUDIO_CNTRL_INJECT_NULLSMPL;
23568e2988a7SThierry Reding 
23578e2988a7SThierry Reding 	value |= SOR_AUDIO_CNTRL_AFIFO_FLUSH;
23588e2988a7SThierry Reding 
23598e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_AUDIO_CNTRL);
23608e2988a7SThierry Reding 
23618e2988a7SThierry Reding 	/* enable advertising HBR capability */
23628e2988a7SThierry Reding 	tegra_sor_writel(sor, SOR_AUDIO_SPARE_HBR_ENABLE, SOR_AUDIO_SPARE);
23638e2988a7SThierry Reding 
23648e2988a7SThierry Reding 	tegra_sor_writel(sor, 0, SOR_HDMI_ACR_CTRL);
23658e2988a7SThierry Reding 
23668e2988a7SThierry Reding 	value = SOR_HDMI_SPARE_ACR_PRIORITY_HIGH |
23678e2988a7SThierry Reding 		SOR_HDMI_SPARE_CTS_RESET(1) |
23688e2988a7SThierry Reding 		SOR_HDMI_SPARE_HW_CTS_ENABLE;
23698e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_SPARE);
23708e2988a7SThierry Reding 
23718e2988a7SThierry Reding 	/* enable HW CTS */
23728e2988a7SThierry Reding 	value = SOR_HDMI_ACR_SUBPACK_LOW_SB1(0);
23738e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_ACR_0441_SUBPACK_LOW);
23748e2988a7SThierry Reding 
23758e2988a7SThierry Reding 	/* allow packet to be sent */
23768e2988a7SThierry Reding 	value = SOR_HDMI_ACR_SUBPACK_HIGH_ENABLE;
23778e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_ACR_0441_SUBPACK_HIGH);
23788e2988a7SThierry Reding 
23798e2988a7SThierry Reding 	/* reset N counter and enable lookup */
23808e2988a7SThierry Reding 	value = SOR_HDMI_AUDIO_N_RESET | SOR_HDMI_AUDIO_N_LOOKUP;
23818e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_N);
23828e2988a7SThierry Reding 
2383fad7b806SThierry Reding 	value = (24000 * 4096) / (128 * sor->format.sample_rate / 1000);
23848e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_0320);
23858e2988a7SThierry Reding 	tegra_sor_writel(sor, 4096, SOR_AUDIO_NVAL_0320);
23868e2988a7SThierry Reding 
23878e2988a7SThierry Reding 	tegra_sor_writel(sor, 20000, SOR_AUDIO_AVAL_0441);
23888e2988a7SThierry Reding 	tegra_sor_writel(sor, 4704, SOR_AUDIO_NVAL_0441);
23898e2988a7SThierry Reding 
23908e2988a7SThierry Reding 	tegra_sor_writel(sor, 20000, SOR_AUDIO_AVAL_0882);
23918e2988a7SThierry Reding 	tegra_sor_writel(sor, 9408, SOR_AUDIO_NVAL_0882);
23928e2988a7SThierry Reding 
23938e2988a7SThierry Reding 	tegra_sor_writel(sor, 20000, SOR_AUDIO_AVAL_1764);
23948e2988a7SThierry Reding 	tegra_sor_writel(sor, 18816, SOR_AUDIO_NVAL_1764);
23958e2988a7SThierry Reding 
2396fad7b806SThierry Reding 	value = (24000 * 6144) / (128 * sor->format.sample_rate / 1000);
23978e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_0480);
23988e2988a7SThierry Reding 	tegra_sor_writel(sor, 6144, SOR_AUDIO_NVAL_0480);
23998e2988a7SThierry Reding 
2400fad7b806SThierry Reding 	value = (24000 * 12288) / (128 * sor->format.sample_rate / 1000);
24018e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_0960);
24028e2988a7SThierry Reding 	tegra_sor_writel(sor, 12288, SOR_AUDIO_NVAL_0960);
24038e2988a7SThierry Reding 
2404fad7b806SThierry Reding 	value = (24000 * 24576) / (128 * sor->format.sample_rate / 1000);
24058e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_1920);
24068e2988a7SThierry Reding 	tegra_sor_writel(sor, 24576, SOR_AUDIO_NVAL_1920);
24078e2988a7SThierry Reding 
24088e2988a7SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_N);
24098e2988a7SThierry Reding 	value &= ~SOR_HDMI_AUDIO_N_RESET;
24108e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_N);
24118e2988a7SThierry Reding 
24128e2988a7SThierry Reding 	tegra_sor_hdmi_enable_audio_infoframe(sor);
24138e2988a7SThierry Reding }
24148e2988a7SThierry Reding 
2415459cc2c6SThierry Reding static void tegra_sor_hdmi_disable_audio_infoframe(struct tegra_sor *sor)
2416459cc2c6SThierry Reding {
2417459cc2c6SThierry Reding 	u32 value;
2418459cc2c6SThierry Reding 
2419459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2420459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_ENABLE;
2421459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2422459cc2c6SThierry Reding }
2423459cc2c6SThierry Reding 
24248e2988a7SThierry Reding static void tegra_sor_hdmi_audio_disable(struct tegra_sor *sor)
24258e2988a7SThierry Reding {
24268e2988a7SThierry Reding 	tegra_sor_hdmi_disable_audio_infoframe(sor);
24278e2988a7SThierry Reding }
24288e2988a7SThierry Reding 
2429459cc2c6SThierry Reding static struct tegra_sor_hdmi_settings *
2430459cc2c6SThierry Reding tegra_sor_hdmi_find_settings(struct tegra_sor *sor, unsigned long frequency)
2431459cc2c6SThierry Reding {
2432459cc2c6SThierry Reding 	unsigned int i;
2433459cc2c6SThierry Reding 
2434459cc2c6SThierry Reding 	for (i = 0; i < sor->num_settings; i++)
2435459cc2c6SThierry Reding 		if (frequency <= sor->settings[i].frequency)
2436459cc2c6SThierry Reding 			return &sor->settings[i];
2437459cc2c6SThierry Reding 
2438459cc2c6SThierry Reding 	return NULL;
2439459cc2c6SThierry Reding }
2440459cc2c6SThierry Reding 
244136e90221SThierry Reding static void tegra_sor_hdmi_disable_scrambling(struct tegra_sor *sor)
244236e90221SThierry Reding {
244336e90221SThierry Reding 	u32 value;
244436e90221SThierry Reding 
244536e90221SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI2_CTRL);
244636e90221SThierry Reding 	value &= ~SOR_HDMI2_CTRL_CLOCK_MODE_DIV_BY_4;
244736e90221SThierry Reding 	value &= ~SOR_HDMI2_CTRL_SCRAMBLE;
244836e90221SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI2_CTRL);
244936e90221SThierry Reding }
245036e90221SThierry Reding 
245136e90221SThierry Reding static void tegra_sor_hdmi_scdc_disable(struct tegra_sor *sor)
245236e90221SThierry Reding {
245336e90221SThierry Reding 	struct i2c_adapter *ddc = sor->output.ddc;
245436e90221SThierry Reding 
245536e90221SThierry Reding 	drm_scdc_set_high_tmds_clock_ratio(ddc, false);
245636e90221SThierry Reding 	drm_scdc_set_scrambling(ddc, false);
245736e90221SThierry Reding 
245836e90221SThierry Reding 	tegra_sor_hdmi_disable_scrambling(sor);
245936e90221SThierry Reding }
246036e90221SThierry Reding 
246136e90221SThierry Reding static void tegra_sor_hdmi_scdc_stop(struct tegra_sor *sor)
246236e90221SThierry Reding {
246336e90221SThierry Reding 	if (sor->scdc_enabled) {
246436e90221SThierry Reding 		cancel_delayed_work_sync(&sor->scdc);
246536e90221SThierry Reding 		tegra_sor_hdmi_scdc_disable(sor);
246636e90221SThierry Reding 	}
246736e90221SThierry Reding }
246836e90221SThierry Reding 
246936e90221SThierry Reding static void tegra_sor_hdmi_enable_scrambling(struct tegra_sor *sor)
247036e90221SThierry Reding {
247136e90221SThierry Reding 	u32 value;
247236e90221SThierry Reding 
247336e90221SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI2_CTRL);
247436e90221SThierry Reding 	value |= SOR_HDMI2_CTRL_CLOCK_MODE_DIV_BY_4;
247536e90221SThierry Reding 	value |= SOR_HDMI2_CTRL_SCRAMBLE;
247636e90221SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI2_CTRL);
247736e90221SThierry Reding }
247836e90221SThierry Reding 
247936e90221SThierry Reding static void tegra_sor_hdmi_scdc_enable(struct tegra_sor *sor)
248036e90221SThierry Reding {
248136e90221SThierry Reding 	struct i2c_adapter *ddc = sor->output.ddc;
248236e90221SThierry Reding 
248336e90221SThierry Reding 	drm_scdc_set_high_tmds_clock_ratio(ddc, true);
248436e90221SThierry Reding 	drm_scdc_set_scrambling(ddc, true);
248536e90221SThierry Reding 
248636e90221SThierry Reding 	tegra_sor_hdmi_enable_scrambling(sor);
248736e90221SThierry Reding }
248836e90221SThierry Reding 
248936e90221SThierry Reding static void tegra_sor_hdmi_scdc_work(struct work_struct *work)
249036e90221SThierry Reding {
249136e90221SThierry Reding 	struct tegra_sor *sor = container_of(work, struct tegra_sor, scdc.work);
249236e90221SThierry Reding 	struct i2c_adapter *ddc = sor->output.ddc;
249336e90221SThierry Reding 
249436e90221SThierry Reding 	if (!drm_scdc_get_scrambling_status(ddc)) {
249536e90221SThierry Reding 		DRM_DEBUG_KMS("SCDC not scrambled\n");
249636e90221SThierry Reding 		tegra_sor_hdmi_scdc_enable(sor);
249736e90221SThierry Reding 	}
249836e90221SThierry Reding 
249936e90221SThierry Reding 	schedule_delayed_work(&sor->scdc, msecs_to_jiffies(5000));
250036e90221SThierry Reding }
250136e90221SThierry Reding 
250236e90221SThierry Reding static void tegra_sor_hdmi_scdc_start(struct tegra_sor *sor)
250336e90221SThierry Reding {
250436e90221SThierry Reding 	struct drm_scdc *scdc = &sor->output.connector.display_info.hdmi.scdc;
250536e90221SThierry Reding 	struct drm_display_mode *mode;
250636e90221SThierry Reding 
250736e90221SThierry Reding 	mode = &sor->output.encoder.crtc->state->adjusted_mode;
250836e90221SThierry Reding 
250936e90221SThierry Reding 	if (mode->clock >= 340000 && scdc->supported) {
251036e90221SThierry Reding 		schedule_delayed_work(&sor->scdc, msecs_to_jiffies(5000));
251136e90221SThierry Reding 		tegra_sor_hdmi_scdc_enable(sor);
251236e90221SThierry Reding 		sor->scdc_enabled = true;
251336e90221SThierry Reding 	}
251436e90221SThierry Reding }
251536e90221SThierry Reding 
2516459cc2c6SThierry Reding static void tegra_sor_hdmi_disable(struct drm_encoder *encoder)
2517459cc2c6SThierry Reding {
2518459cc2c6SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
2519459cc2c6SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
2520459cc2c6SThierry Reding 	struct tegra_sor *sor = to_sor(output);
2521459cc2c6SThierry Reding 	u32 value;
2522459cc2c6SThierry Reding 	int err;
2523459cc2c6SThierry Reding 
25248e2988a7SThierry Reding 	tegra_sor_audio_unprepare(sor);
252536e90221SThierry Reding 	tegra_sor_hdmi_scdc_stop(sor);
252636e90221SThierry Reding 
2527459cc2c6SThierry Reding 	err = tegra_sor_detach(sor);
2528459cc2c6SThierry Reding 	if (err < 0)
2529459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to detach SOR: %d\n", err);
2530459cc2c6SThierry Reding 
2531459cc2c6SThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE1);
2532459cc2c6SThierry Reding 	tegra_sor_update(sor);
2533459cc2c6SThierry Reding 
2534459cc2c6SThierry Reding 	/* disable display to SOR clock */
2535459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
2536c57997bcSThierry Reding 
2537c57997bcSThierry Reding 	if (!sor->soc->has_nvdisplay)
2538c57997bcSThierry Reding 		value &= ~(SOR1_TIMING_CYA | SOR_ENABLE(1));
2539c57997bcSThierry Reding 	else
2540c57997bcSThierry Reding 		value &= ~SOR_ENABLE(sor->index);
2541c57997bcSThierry Reding 
2542459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2543459cc2c6SThierry Reding 
2544459cc2c6SThierry Reding 	tegra_dc_commit(dc);
2545459cc2c6SThierry Reding 
2546459cc2c6SThierry Reding 	err = tegra_sor_power_down(sor);
2547459cc2c6SThierry Reding 	if (err < 0)
2548459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power down SOR: %d\n", err);
2549459cc2c6SThierry Reding 
2550c57997bcSThierry Reding 	err = tegra_io_pad_power_disable(sor->pad);
2551459cc2c6SThierry Reding 	if (err < 0)
2552c57997bcSThierry Reding 		dev_err(sor->dev, "failed to power off I/O pad: %d\n", err);
2553459cc2c6SThierry Reding 
2554aaff8bd2SThierry Reding 	pm_runtime_put(sor->dev);
2555459cc2c6SThierry Reding }
2556459cc2c6SThierry Reding 
2557459cc2c6SThierry Reding static void tegra_sor_hdmi_enable(struct drm_encoder *encoder)
2558459cc2c6SThierry Reding {
2559459cc2c6SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
2560459cc2c6SThierry Reding 	unsigned int h_ref_to_sync = 1, pulse_start, max_ac;
2561459cc2c6SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
2562459cc2c6SThierry Reding 	struct tegra_sor_hdmi_settings *settings;
2563459cc2c6SThierry Reding 	struct tegra_sor *sor = to_sor(output);
2564c31efa7aSThierry Reding 	struct tegra_sor_state *state;
2565459cc2c6SThierry Reding 	struct drm_display_mode *mode;
256636e90221SThierry Reding 	unsigned long rate, pclk;
256730b49435SThierry Reding 	unsigned int div, i;
2568459cc2c6SThierry Reding 	u32 value;
2569459cc2c6SThierry Reding 	int err;
2570459cc2c6SThierry Reding 
2571c31efa7aSThierry Reding 	state = to_sor_state(output->connector.state);
2572459cc2c6SThierry Reding 	mode = &encoder->crtc->state->adjusted_mode;
257336e90221SThierry Reding 	pclk = mode->clock * 1000;
2574459cc2c6SThierry Reding 
2575aaff8bd2SThierry Reding 	pm_runtime_get_sync(sor->dev);
2576459cc2c6SThierry Reding 
257725bb2cecSThierry Reding 	/* switch to safe parent clock */
257825bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
2579e1335e2fSThierry Reding 	if (err < 0) {
2580459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
2581e1335e2fSThierry Reding 		return;
2582e1335e2fSThierry Reding 	}
2583459cc2c6SThierry Reding 
2584459cc2c6SThierry Reding 	div = clk_get_rate(sor->clk) / 1000000 * 4;
2585459cc2c6SThierry Reding 
2586c57997bcSThierry Reding 	err = tegra_io_pad_power_enable(sor->pad);
2587459cc2c6SThierry Reding 	if (err < 0)
2588c57997bcSThierry Reding 		dev_err(sor->dev, "failed to power on I/O pad: %d\n", err);
2589459cc2c6SThierry Reding 
2590459cc2c6SThierry Reding 	usleep_range(20, 100);
2591459cc2c6SThierry Reding 
2592880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
2593459cc2c6SThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
2594880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
2595459cc2c6SThierry Reding 
2596459cc2c6SThierry Reding 	usleep_range(20, 100);
2597459cc2c6SThierry Reding 
2598880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll3);
2599459cc2c6SThierry Reding 	value &= ~SOR_PLL3_PLL_VDD_MODE_3V3;
2600880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll3);
2601459cc2c6SThierry Reding 
2602880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
2603459cc2c6SThierry Reding 	value &= ~SOR_PLL0_VCOPD;
2604459cc2c6SThierry Reding 	value &= ~SOR_PLL0_PWR;
2605880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
2606459cc2c6SThierry Reding 
2607880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
2608459cc2c6SThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
2609880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
2610459cc2c6SThierry Reding 
2611459cc2c6SThierry Reding 	usleep_range(200, 400);
2612459cc2c6SThierry Reding 
2613880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
2614459cc2c6SThierry Reding 	value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
2615459cc2c6SThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
2616880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
2617459cc2c6SThierry Reding 
2618459cc2c6SThierry Reding 	usleep_range(20, 100);
2619459cc2c6SThierry Reding 
2620880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2621459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
2622459cc2c6SThierry Reding 		 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2;
2623880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2624459cc2c6SThierry Reding 
2625459cc2c6SThierry Reding 	while (true) {
2626459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2627459cc2c6SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_STATE_BUSY) == 0)
2628459cc2c6SThierry Reding 			break;
2629459cc2c6SThierry Reding 
2630459cc2c6SThierry Reding 		usleep_range(250, 1000);
2631459cc2c6SThierry Reding 	}
2632459cc2c6SThierry Reding 
2633459cc2c6SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
2634459cc2c6SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_UP | SOR_LANE_SEQ_CTL_DELAY(5);
2635459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
2636459cc2c6SThierry Reding 
2637459cc2c6SThierry Reding 	while (true) {
2638459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2639459cc2c6SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
2640459cc2c6SThierry Reding 			break;
2641459cc2c6SThierry Reding 
2642459cc2c6SThierry Reding 		usleep_range(250, 1000);
2643459cc2c6SThierry Reding 	}
2644459cc2c6SThierry Reding 
2645459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
2646459cc2c6SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
2647459cc2c6SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
2648459cc2c6SThierry Reding 
264936e90221SThierry Reding 	if (mode->clock < 340000) {
265036e90221SThierry Reding 		DRM_DEBUG_KMS("setting 2.7 GHz link speed\n");
2651459cc2c6SThierry Reding 		value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G2_70;
265236e90221SThierry Reding 	} else {
265336e90221SThierry Reding 		DRM_DEBUG_KMS("setting 5.4 GHz link speed\n");
2654459cc2c6SThierry Reding 		value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G5_40;
265536e90221SThierry Reding 	}
2656459cc2c6SThierry Reding 
2657459cc2c6SThierry Reding 	value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
2658459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
2659459cc2c6SThierry Reding 
2660c57997bcSThierry Reding 	/* SOR pad PLL stabilization time */
2661c57997bcSThierry Reding 	usleep_range(250, 1000);
2662c57997bcSThierry Reding 
2663c57997bcSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
2664c57997bcSThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
2665c57997bcSThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(4);
2666c57997bcSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
2667c57997bcSThierry Reding 
2668459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
2669c57997bcSThierry Reding 	value &= ~SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
2670459cc2c6SThierry Reding 	value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
2671c57997bcSThierry Reding 	value &= ~SOR_DP_SPARE_SEQ_ENABLE;
2672c57997bcSThierry Reding 	value &= ~SOR_DP_SPARE_MACRO_SOR_CLK;
2673459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
2674459cc2c6SThierry Reding 
2675459cc2c6SThierry Reding 	value = SOR_SEQ_CTL_PU_PC(0) | SOR_SEQ_CTL_PU_PC_ALT(0) |
2676459cc2c6SThierry Reding 		SOR_SEQ_CTL_PD_PC(8) | SOR_SEQ_CTL_PD_PC_ALT(8);
2677459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_CTL);
2678459cc2c6SThierry Reding 
2679459cc2c6SThierry Reding 	value = SOR_SEQ_INST_DRIVE_PWM_OUT_LO | SOR_SEQ_INST_HALT |
2680459cc2c6SThierry Reding 		SOR_SEQ_INST_WAIT_VSYNC | SOR_SEQ_INST_WAIT(1);
2681459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_INST(0));
2682459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_INST(8));
2683459cc2c6SThierry Reding 
2684c57997bcSThierry Reding 	if (!sor->soc->has_nvdisplay) {
2685459cc2c6SThierry Reding 		/* program the reference clock */
2686459cc2c6SThierry Reding 		value = SOR_REFCLK_DIV_INT(div) | SOR_REFCLK_DIV_FRAC(div);
2687459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, SOR_REFCLK);
2688c57997bcSThierry Reding 	}
2689459cc2c6SThierry Reding 
269030b49435SThierry Reding 	/* XXX not in TRM */
269130b49435SThierry Reding 	for (value = 0, i = 0; i < 5; i++)
26926d6c815dSThierry Reding 		value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->xbar_cfg[i]) |
269330b49435SThierry Reding 			 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
2694459cc2c6SThierry Reding 
2695459cc2c6SThierry Reding 	tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
269630b49435SThierry Reding 	tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
2697459cc2c6SThierry Reding 
269861417aaaSThierry Reding 	/*
269961417aaaSThierry Reding 	 * Switch the pad clock to the DP clock. Note that we cannot actually
270061417aaaSThierry Reding 	 * do this because Tegra186 and later don't support clk_set_parent()
270161417aaaSThierry Reding 	 * on the sorX_pad_clkout clocks. We already do the equivalent above
270261417aaaSThierry Reding 	 * using the DP_CLK_SEL mux of the SOR_CLK_CNTRL register.
270361417aaaSThierry Reding 	 */
270461417aaaSThierry Reding #if 0
270561417aaaSThierry Reding 	err = clk_set_parent(sor->clk_pad, sor->clk_dp);
2706e1335e2fSThierry Reding 	if (err < 0) {
270761417aaaSThierry Reding 		dev_err(sor->dev, "failed to select pad parent clock: %d\n",
270861417aaaSThierry Reding 			err);
270961417aaaSThierry Reding 		return;
271061417aaaSThierry Reding 	}
271161417aaaSThierry Reding #endif
271261417aaaSThierry Reding 
271361417aaaSThierry Reding 	/* switch the SOR clock to the pad clock */
271461417aaaSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_pad);
271561417aaaSThierry Reding 	if (err < 0) {
271661417aaaSThierry Reding 		dev_err(sor->dev, "failed to select SOR parent clock: %d\n",
271761417aaaSThierry Reding 			err);
2718e1335e2fSThierry Reding 		return;
2719e1335e2fSThierry Reding 	}
2720e1335e2fSThierry Reding 
272161417aaaSThierry Reding 	/* switch the output clock to the parent pixel clock */
272261417aaaSThierry Reding 	err = clk_set_parent(sor->clk, sor->clk_parent);
2723e1335e2fSThierry Reding 	if (err < 0) {
272461417aaaSThierry Reding 		dev_err(sor->dev, "failed to select output parent clock: %d\n",
272561417aaaSThierry Reding 			err);
2726e1335e2fSThierry Reding 		return;
2727e1335e2fSThierry Reding 	}
2728459cc2c6SThierry Reding 
272936e90221SThierry Reding 	/* adjust clock rate for HDMI 2.0 modes */
273036e90221SThierry Reding 	rate = clk_get_rate(sor->clk_parent);
273136e90221SThierry Reding 
273236e90221SThierry Reding 	if (mode->clock >= 340000)
273336e90221SThierry Reding 		rate /= 2;
273436e90221SThierry Reding 
273536e90221SThierry Reding 	DRM_DEBUG_KMS("setting clock to %lu Hz, mode: %lu Hz\n", rate, pclk);
273636e90221SThierry Reding 
273736e90221SThierry Reding 	clk_set_rate(sor->clk, rate);
2738c57997bcSThierry Reding 
2739c57997bcSThierry Reding 	if (!sor->soc->has_nvdisplay) {
2740459cc2c6SThierry Reding 		value = SOR_INPUT_CONTROL_HDMI_SRC_SELECT(dc->pipe);
2741459cc2c6SThierry Reding 
2742459cc2c6SThierry Reding 		/* XXX is this the proper check? */
2743459cc2c6SThierry Reding 		if (mode->clock < 75000)
2744459cc2c6SThierry Reding 			value |= SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_LIMITED;
2745459cc2c6SThierry Reding 
2746459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, SOR_INPUT_CONTROL);
2747c57997bcSThierry Reding 	}
2748459cc2c6SThierry Reding 
2749459cc2c6SThierry Reding 	max_ac = ((mode->htotal - mode->hdisplay) - SOR_REKEY - 18) / 32;
2750459cc2c6SThierry Reding 
2751459cc2c6SThierry Reding 	value = SOR_HDMI_CTRL_ENABLE | SOR_HDMI_CTRL_MAX_AC_PACKET(max_ac) |
2752459cc2c6SThierry Reding 		SOR_HDMI_CTRL_AUDIO_LAYOUT | SOR_HDMI_CTRL_REKEY(SOR_REKEY);
2753459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_CTRL);
2754459cc2c6SThierry Reding 
2755c57997bcSThierry Reding 	if (!dc->soc->has_nvdisplay) {
2756459cc2c6SThierry Reding 		/* H_PULSE2 setup */
2757c57997bcSThierry Reding 		pulse_start = h_ref_to_sync +
2758c57997bcSThierry Reding 			      (mode->hsync_end - mode->hsync_start) +
2759459cc2c6SThierry Reding 			      (mode->htotal - mode->hsync_end) - 10;
2760459cc2c6SThierry Reding 
2761459cc2c6SThierry Reding 		value = PULSE_LAST_END_A | PULSE_QUAL_VACTIVE |
2762459cc2c6SThierry Reding 			PULSE_POLARITY_HIGH | PULSE_MODE_NORMAL;
2763459cc2c6SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_CONTROL);
2764459cc2c6SThierry Reding 
2765459cc2c6SThierry Reding 		value = PULSE_END(pulse_start + 8) | PULSE_START(pulse_start);
2766459cc2c6SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_POSITION_A);
2767459cc2c6SThierry Reding 
2768459cc2c6SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_DISP_SIGNAL_OPTIONS0);
2769459cc2c6SThierry Reding 		value |= H_PULSE2_ENABLE;
2770459cc2c6SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_SIGNAL_OPTIONS0);
2771c57997bcSThierry Reding 	}
2772459cc2c6SThierry Reding 
2773459cc2c6SThierry Reding 	/* infoframe setup */
2774459cc2c6SThierry Reding 	err = tegra_sor_hdmi_setup_avi_infoframe(sor, mode);
2775459cc2c6SThierry Reding 	if (err < 0)
2776459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
2777459cc2c6SThierry Reding 
2778459cc2c6SThierry Reding 	/* XXX HDMI audio support not implemented yet */
2779459cc2c6SThierry Reding 	tegra_sor_hdmi_disable_audio_infoframe(sor);
2780459cc2c6SThierry Reding 
2781459cc2c6SThierry Reding 	/* use single TMDS protocol */
2782459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
2783459cc2c6SThierry Reding 	value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
2784459cc2c6SThierry Reding 	value |= SOR_STATE_ASY_PROTOCOL_SINGLE_TMDS_A;
2785459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
2786459cc2c6SThierry Reding 
2787459cc2c6SThierry Reding 	/* power up pad calibration */
2788880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2789459cc2c6SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
2790880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2791459cc2c6SThierry Reding 
2792459cc2c6SThierry Reding 	/* production settings */
2793459cc2c6SThierry Reding 	settings = tegra_sor_hdmi_find_settings(sor, mode->clock * 1000);
2794db8b42fbSDan Carpenter 	if (!settings) {
2795db8b42fbSDan Carpenter 		dev_err(sor->dev, "no settings for pixel clock %d Hz\n",
2796db8b42fbSDan Carpenter 			mode->clock * 1000);
2797459cc2c6SThierry Reding 		return;
2798459cc2c6SThierry Reding 	}
2799459cc2c6SThierry Reding 
2800880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
2801459cc2c6SThierry Reding 	value &= ~SOR_PLL0_ICHPMP_MASK;
2802c57997bcSThierry Reding 	value &= ~SOR_PLL0_FILTER_MASK;
2803459cc2c6SThierry Reding 	value &= ~SOR_PLL0_VCOCAP_MASK;
2804459cc2c6SThierry Reding 	value |= SOR_PLL0_ICHPMP(settings->ichpmp);
2805c57997bcSThierry Reding 	value |= SOR_PLL0_FILTER(settings->filter);
2806459cc2c6SThierry Reding 	value |= SOR_PLL0_VCOCAP(settings->vcocap);
2807880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
2808459cc2c6SThierry Reding 
2809c57997bcSThierry Reding 	/* XXX not in TRM */
2810880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll1);
2811459cc2c6SThierry Reding 	value &= ~SOR_PLL1_LOADADJ_MASK;
2812c57997bcSThierry Reding 	value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
2813459cc2c6SThierry Reding 	value |= SOR_PLL1_LOADADJ(settings->loadadj);
2814c57997bcSThierry Reding 	value |= SOR_PLL1_TMDS_TERMADJ(settings->tmds_termadj);
2815c57997bcSThierry Reding 	value |= SOR_PLL1_TMDS_TERM;
2816880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll1);
2817459cc2c6SThierry Reding 
2818880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll3);
2819c57997bcSThierry Reding 	value &= ~SOR_PLL3_BG_TEMP_COEF_MASK;
2820459cc2c6SThierry Reding 	value &= ~SOR_PLL3_BG_VREF_LEVEL_MASK;
2821c57997bcSThierry Reding 	value &= ~SOR_PLL3_AVDD10_LEVEL_MASK;
2822c57997bcSThierry Reding 	value &= ~SOR_PLL3_AVDD14_LEVEL_MASK;
2823c57997bcSThierry Reding 	value |= SOR_PLL3_BG_TEMP_COEF(settings->bg_temp_coef);
2824c57997bcSThierry Reding 	value |= SOR_PLL3_BG_VREF_LEVEL(settings->bg_vref_level);
2825c57997bcSThierry Reding 	value |= SOR_PLL3_AVDD10_LEVEL(settings->avdd10_level);
2826c57997bcSThierry Reding 	value |= SOR_PLL3_AVDD14_LEVEL(settings->avdd14_level);
2827880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll3);
2828459cc2c6SThierry Reding 
2829c57997bcSThierry Reding 	value = settings->drive_current[3] << 24 |
2830c57997bcSThierry Reding 		settings->drive_current[2] << 16 |
2831c57997bcSThierry Reding 		settings->drive_current[1] <<  8 |
2832c57997bcSThierry Reding 		settings->drive_current[0] <<  0;
2833459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
2834459cc2c6SThierry Reding 
2835c57997bcSThierry Reding 	value = settings->preemphasis[3] << 24 |
2836c57997bcSThierry Reding 		settings->preemphasis[2] << 16 |
2837c57997bcSThierry Reding 		settings->preemphasis[1] <<  8 |
2838c57997bcSThierry Reding 		settings->preemphasis[0] <<  0;
2839459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
2840459cc2c6SThierry Reding 
2841880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2842459cc2c6SThierry Reding 	value &= ~SOR_DP_PADCTL_TX_PU_MASK;
2843459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU_ENABLE;
2844c57997bcSThierry Reding 	value |= SOR_DP_PADCTL_TX_PU(settings->tx_pu_value);
2845880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2846459cc2c6SThierry Reding 
2847c57997bcSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl2);
2848c57997bcSThierry Reding 	value &= ~SOR_DP_PADCTL_SPAREPLL_MASK;
2849c57997bcSThierry Reding 	value |= SOR_DP_PADCTL_SPAREPLL(settings->sparepll);
2850c57997bcSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl2);
2851c57997bcSThierry Reding 
2852459cc2c6SThierry Reding 	/* power down pad calibration */
2853880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2854459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
2855880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2856459cc2c6SThierry Reding 
2857c57997bcSThierry Reding 	if (!dc->soc->has_nvdisplay) {
2858459cc2c6SThierry Reding 		/* miscellaneous display controller settings */
2859459cc2c6SThierry Reding 		value = VSYNC_H_POSITION(1);
2860459cc2c6SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_TIMING_OPTIONS);
2861c57997bcSThierry Reding 	}
2862459cc2c6SThierry Reding 
2863459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_COLOR_CONTROL);
2864459cc2c6SThierry Reding 	value &= ~DITHER_CONTROL_MASK;
2865459cc2c6SThierry Reding 	value &= ~BASE_COLOR_SIZE_MASK;
2866459cc2c6SThierry Reding 
2867c31efa7aSThierry Reding 	switch (state->bpc) {
2868459cc2c6SThierry Reding 	case 6:
2869459cc2c6SThierry Reding 		value |= BASE_COLOR_SIZE_666;
2870459cc2c6SThierry Reding 		break;
2871459cc2c6SThierry Reding 
2872459cc2c6SThierry Reding 	case 8:
2873459cc2c6SThierry Reding 		value |= BASE_COLOR_SIZE_888;
2874459cc2c6SThierry Reding 		break;
2875459cc2c6SThierry Reding 
2876c57997bcSThierry Reding 	case 10:
2877c57997bcSThierry Reding 		value |= BASE_COLOR_SIZE_101010;
2878c57997bcSThierry Reding 		break;
2879c57997bcSThierry Reding 
2880c57997bcSThierry Reding 	case 12:
2881c57997bcSThierry Reding 		value |= BASE_COLOR_SIZE_121212;
2882c57997bcSThierry Reding 		break;
2883c57997bcSThierry Reding 
2884459cc2c6SThierry Reding 	default:
2885c31efa7aSThierry Reding 		WARN(1, "%u bits-per-color not supported\n", state->bpc);
2886c31efa7aSThierry Reding 		value |= BASE_COLOR_SIZE_888;
2887459cc2c6SThierry Reding 		break;
2888459cc2c6SThierry Reding 	}
2889459cc2c6SThierry Reding 
2890459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_COLOR_CONTROL);
2891459cc2c6SThierry Reding 
2892c57997bcSThierry Reding 	/* XXX set display head owner */
2893c57997bcSThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
2894c57997bcSThierry Reding 	value &= ~SOR_STATE_ASY_OWNER_MASK;
2895c57997bcSThierry Reding 	value |= SOR_STATE_ASY_OWNER(1 + dc->pipe);
2896c57997bcSThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
2897c57997bcSThierry Reding 
2898459cc2c6SThierry Reding 	err = tegra_sor_power_up(sor, 250);
2899459cc2c6SThierry Reding 	if (err < 0)
2900459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
2901459cc2c6SThierry Reding 
29022bd1dd39SThierry Reding 	/* configure dynamic range of output */
2903880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
2904459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_RANGECOMPRESS_MASK;
2905459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_DYNRANGE_MASK;
2906880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
2907459cc2c6SThierry Reding 
29082bd1dd39SThierry Reding 	/* configure colorspace */
2909880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
2910459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_COLORSPACE_MASK;
2911459cc2c6SThierry Reding 	value |= SOR_HEAD_STATE_COLORSPACE_RGB;
2912880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
2913459cc2c6SThierry Reding 
2914c31efa7aSThierry Reding 	tegra_sor_mode_set(sor, mode, state);
2915459cc2c6SThierry Reding 
2916459cc2c6SThierry Reding 	tegra_sor_update(sor);
2917459cc2c6SThierry Reding 
2918c57997bcSThierry Reding 	/* program preamble timing in SOR (XXX) */
2919c57997bcSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
2920c57997bcSThierry Reding 	value &= ~SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
2921c57997bcSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
2922c57997bcSThierry Reding 
2923459cc2c6SThierry Reding 	err = tegra_sor_attach(sor);
2924459cc2c6SThierry Reding 	if (err < 0)
2925459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to attach SOR: %d\n", err);
2926459cc2c6SThierry Reding 
2927459cc2c6SThierry Reding 	/* enable display to SOR clock and generate HDMI preamble */
2928459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
2929c57997bcSThierry Reding 
2930c57997bcSThierry Reding 	if (!sor->soc->has_nvdisplay)
2931c57997bcSThierry Reding 		value |= SOR_ENABLE(1) | SOR1_TIMING_CYA;
2932c57997bcSThierry Reding 	else
2933c57997bcSThierry Reding 		value |= SOR_ENABLE(sor->index);
2934c57997bcSThierry Reding 
2935459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2936459cc2c6SThierry Reding 
2937c57997bcSThierry Reding 	if (dc->soc->has_nvdisplay) {
2938c57997bcSThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_CORE_SOR_SET_CONTROL(sor->index));
2939c57997bcSThierry Reding 		value &= ~PROTOCOL_MASK;
2940c57997bcSThierry Reding 		value |= PROTOCOL_SINGLE_TMDS_A;
2941c57997bcSThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_CORE_SOR_SET_CONTROL(sor->index));
2942c57997bcSThierry Reding 	}
2943c57997bcSThierry Reding 
2944459cc2c6SThierry Reding 	tegra_dc_commit(dc);
2945459cc2c6SThierry Reding 
2946459cc2c6SThierry Reding 	err = tegra_sor_wakeup(sor);
2947459cc2c6SThierry Reding 	if (err < 0)
2948459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
294936e90221SThierry Reding 
295036e90221SThierry Reding 	tegra_sor_hdmi_scdc_start(sor);
29518e2988a7SThierry Reding 	tegra_sor_audio_prepare(sor);
2952459cc2c6SThierry Reding }
2953459cc2c6SThierry Reding 
2954459cc2c6SThierry Reding static const struct drm_encoder_helper_funcs tegra_sor_hdmi_helpers = {
2955459cc2c6SThierry Reding 	.disable = tegra_sor_hdmi_disable,
2956459cc2c6SThierry Reding 	.enable = tegra_sor_hdmi_enable,
2957459cc2c6SThierry Reding 	.atomic_check = tegra_sor_encoder_atomic_check,
2958459cc2c6SThierry Reding };
2959459cc2c6SThierry Reding 
29600472c21bSThierry Reding static void tegra_sor_dp_disable(struct drm_encoder *encoder)
29610472c21bSThierry Reding {
29620472c21bSThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
29630472c21bSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
29640472c21bSThierry Reding 	struct tegra_sor *sor = to_sor(output);
29650472c21bSThierry Reding 	u32 value;
29660472c21bSThierry Reding 	int err;
29670472c21bSThierry Reding 
29680472c21bSThierry Reding 	err = drm_dp_link_power_down(sor->aux, &sor->link);
29690472c21bSThierry Reding 	if (err < 0)
29700472c21bSThierry Reding 		dev_err(sor->dev, "failed to power down link: %d\n", err);
29710472c21bSThierry Reding 
29720472c21bSThierry Reding 	err = tegra_sor_detach(sor);
29730472c21bSThierry Reding 	if (err < 0)
29740472c21bSThierry Reding 		dev_err(sor->dev, "failed to detach SOR: %d\n", err);
29750472c21bSThierry Reding 
29760472c21bSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE1);
29770472c21bSThierry Reding 	tegra_sor_update(sor);
29780472c21bSThierry Reding 
29790472c21bSThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
29800472c21bSThierry Reding 
29810472c21bSThierry Reding 	if (!sor->soc->has_nvdisplay)
29820472c21bSThierry Reding 		value &= ~(SOR1_TIMING_CYA | SOR_ENABLE(1));
29830472c21bSThierry Reding 	else
29840472c21bSThierry Reding 		value &= ~SOR_ENABLE(sor->index);
29850472c21bSThierry Reding 
29860472c21bSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
29870472c21bSThierry Reding 	tegra_dc_commit(dc);
29880472c21bSThierry Reding 
29890472c21bSThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
29900472c21bSThierry Reding 	value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
29910472c21bSThierry Reding 	value &= ~SOR_STATE_ASY_SUBOWNER_MASK;
29920472c21bSThierry Reding 	value &= ~SOR_STATE_ASY_OWNER_MASK;
29930472c21bSThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
29940472c21bSThierry Reding 	tegra_sor_update(sor);
29950472c21bSThierry Reding 
29960472c21bSThierry Reding 	/* switch to safe parent clock */
29970472c21bSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
29980472c21bSThierry Reding 	if (err < 0)
29990472c21bSThierry Reding 		dev_err(sor->dev, "failed to set safe clock: %d\n", err);
30000472c21bSThierry Reding 
30010472c21bSThierry Reding 	err = tegra_sor_power_down(sor);
30020472c21bSThierry Reding 	if (err < 0)
30030472c21bSThierry Reding 		dev_err(sor->dev, "failed to power down SOR: %d\n", err);
30040472c21bSThierry Reding 
30050472c21bSThierry Reding 	err = tegra_io_pad_power_disable(sor->pad);
30060472c21bSThierry Reding 	if (err < 0)
30070472c21bSThierry Reding 		dev_err(sor->dev, "failed to power off I/O pad: %d\n", err);
30080472c21bSThierry Reding 
30090472c21bSThierry Reding 	err = drm_dp_aux_disable(sor->aux);
30100472c21bSThierry Reding 	if (err < 0)
30110472c21bSThierry Reding 		dev_err(sor->dev, "failed disable DPAUX: %d\n", err);
30120472c21bSThierry Reding 
30130472c21bSThierry Reding 	pm_runtime_put(sor->dev);
30140472c21bSThierry Reding }
30150472c21bSThierry Reding 
30160472c21bSThierry Reding static void tegra_sor_dp_enable(struct drm_encoder *encoder)
30170472c21bSThierry Reding {
30180472c21bSThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
30190472c21bSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
30200472c21bSThierry Reding 	struct tegra_sor *sor = to_sor(output);
30210472c21bSThierry Reding 	struct tegra_sor_config config;
30220472c21bSThierry Reding 	struct tegra_sor_state *state;
30230472c21bSThierry Reding 	struct drm_display_mode *mode;
30240472c21bSThierry Reding 	struct drm_display_info *info;
30250472c21bSThierry Reding 	unsigned int i;
30260472c21bSThierry Reding 	u32 value;
30270472c21bSThierry Reding 	int err;
30280472c21bSThierry Reding 
30290472c21bSThierry Reding 	state = to_sor_state(output->connector.state);
30300472c21bSThierry Reding 	mode = &encoder->crtc->state->adjusted_mode;
30310472c21bSThierry Reding 	info = &output->connector.display_info;
30320472c21bSThierry Reding 
30330472c21bSThierry Reding 	pm_runtime_get_sync(sor->dev);
30340472c21bSThierry Reding 
30350472c21bSThierry Reding 	/* switch to safe parent clock */
30360472c21bSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
30370472c21bSThierry Reding 	if (err < 0)
30380472c21bSThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
30390472c21bSThierry Reding 
30400472c21bSThierry Reding 	err = tegra_io_pad_power_enable(sor->pad);
30410472c21bSThierry Reding 	if (err < 0)
30420472c21bSThierry Reding 		dev_err(sor->dev, "failed to power on LVDS rail: %d\n", err);
30430472c21bSThierry Reding 
30440472c21bSThierry Reding 	usleep_range(20, 100);
30450472c21bSThierry Reding 
30460472c21bSThierry Reding 	err = drm_dp_aux_enable(sor->aux);
30470472c21bSThierry Reding 	if (err < 0)
30480472c21bSThierry Reding 		dev_err(sor->dev, "failed to enable DPAUX: %d\n", err);
30490472c21bSThierry Reding 
30500472c21bSThierry Reding 	err = drm_dp_link_probe(sor->aux, &sor->link);
30510472c21bSThierry Reding 	if (err < 0)
30520472c21bSThierry Reding 		dev_err(sor->dev, "failed to probe DP link: %d\n", err);
30530472c21bSThierry Reding 
30540472c21bSThierry Reding 	err = drm_dp_link_choose(&sor->link, mode, info);
30550472c21bSThierry Reding 	if (err < 0)
30560472c21bSThierry Reding 		dev_err(sor->dev, "failed to choose link: %d\n", err);
30570472c21bSThierry Reding 
30580472c21bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
30590472c21bSThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
30600472c21bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
30610472c21bSThierry Reding 
30620472c21bSThierry Reding 	usleep_range(20, 40);
30630472c21bSThierry Reding 
30640472c21bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll3);
30650472c21bSThierry Reding 	value |= SOR_PLL3_PLL_VDD_MODE_3V3;
30660472c21bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll3);
30670472c21bSThierry Reding 
30680472c21bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
30690472c21bSThierry Reding 	value &= ~(SOR_PLL0_VCOPD | SOR_PLL0_PWR);
30700472c21bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
30710472c21bSThierry Reding 
30720472c21bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
30730472c21bSThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
30740472c21bSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD;
30750472c21bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
30760472c21bSThierry Reding 
30770472c21bSThierry Reding 	usleep_range(200, 400);
30780472c21bSThierry Reding 
30790472c21bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
30800472c21bSThierry Reding 	value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
30810472c21bSThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
30820472c21bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
30830472c21bSThierry Reding 
30840472c21bSThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
30850472c21bSThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
30860472c21bSThierry Reding 	value |= SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_DPCLK;
30870472c21bSThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
30880472c21bSThierry Reding 
30890472c21bSThierry Reding 	usleep_range(200, 400);
30900472c21bSThierry Reding 
30910472c21bSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
30920472c21bSThierry Reding 	/* XXX not in TRM */
30930472c21bSThierry Reding 	value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
30940472c21bSThierry Reding 	value |= SOR_DP_SPARE_SEQ_ENABLE;
30950472c21bSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
30960472c21bSThierry Reding 
30970472c21bSThierry Reding 	/* XXX not in TRM */
30980472c21bSThierry Reding 	tegra_sor_writel(sor, 0, SOR_LVDS);
30990472c21bSThierry Reding 
31000472c21bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
31010472c21bSThierry Reding 	value &= ~SOR_PLL0_ICHPMP_MASK;
31020472c21bSThierry Reding 	value &= ~SOR_PLL0_VCOCAP_MASK;
31030472c21bSThierry Reding 	value |= SOR_PLL0_ICHPMP(0x1);
31040472c21bSThierry Reding 	value |= SOR_PLL0_VCOCAP(0x3);
31050472c21bSThierry Reding 	value |= SOR_PLL0_RESISTOR_EXT;
31060472c21bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
31070472c21bSThierry Reding 
31080472c21bSThierry Reding 	/* XXX not in TRM */
31090472c21bSThierry Reding 	for (value = 0, i = 0; i < 5; i++)
31100472c21bSThierry Reding 		value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->soc->xbar_cfg[i]) |
31110472c21bSThierry Reding 			 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
31120472c21bSThierry Reding 
31130472c21bSThierry Reding 	tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
31140472c21bSThierry Reding 	tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
31150472c21bSThierry Reding 
311661417aaaSThierry Reding 	/*
311761417aaaSThierry Reding 	 * Switch the pad clock to the DP clock. Note that we cannot actually
311861417aaaSThierry Reding 	 * do this because Tegra186 and later don't support clk_set_parent()
311961417aaaSThierry Reding 	 * on the sorX_pad_clkout clocks. We already do the equivalent above
312061417aaaSThierry Reding 	 * using the DP_CLK_SEL mux of the SOR_CLK_CNTRL register.
312161417aaaSThierry Reding 	 */
312261417aaaSThierry Reding #if 0
312361417aaaSThierry Reding 	err = clk_set_parent(sor->clk_pad, sor->clk_parent);
312461417aaaSThierry Reding 	if (err < 0) {
312561417aaaSThierry Reding 		dev_err(sor->dev, "failed to select pad parent clock: %d\n",
312661417aaaSThierry Reding 			err);
312761417aaaSThierry Reding 		return;
312861417aaaSThierry Reding 	}
312961417aaaSThierry Reding #endif
313061417aaaSThierry Reding 
313161417aaaSThierry Reding 	/* switch the SOR clock to the pad clock */
31320472c21bSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_pad);
31330472c21bSThierry Reding 	if (err < 0) {
313461417aaaSThierry Reding 		dev_err(sor->dev, "failed to select SOR parent clock: %d\n",
313561417aaaSThierry Reding 			err);
31360472c21bSThierry Reding 		return;
31370472c21bSThierry Reding 	}
31380472c21bSThierry Reding 
313961417aaaSThierry Reding 	/* switch the output clock to the parent pixel clock */
31400472c21bSThierry Reding 	err = clk_set_parent(sor->clk, sor->clk_parent);
31410472c21bSThierry Reding 	if (err < 0) {
314261417aaaSThierry Reding 		dev_err(sor->dev, "failed to select output parent clock: %d\n",
314361417aaaSThierry Reding 			err);
31440472c21bSThierry Reding 		return;
31450472c21bSThierry Reding 	}
31460472c21bSThierry Reding 
31470472c21bSThierry Reding 	/* use DP-A protocol */
31480472c21bSThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
31490472c21bSThierry Reding 	value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
31500472c21bSThierry Reding 	value |= SOR_STATE_ASY_PROTOCOL_DP_A;
31510472c21bSThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
31520472c21bSThierry Reding 
31530472c21bSThierry Reding 	/* enable port */
31540472c21bSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
31550472c21bSThierry Reding 	value |= SOR_DP_LINKCTL_ENABLE;
31560472c21bSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
31570472c21bSThierry Reding 
31580472c21bSThierry Reding 	tegra_sor_dp_term_calibrate(sor);
31590472c21bSThierry Reding 
31600472c21bSThierry Reding 	err = drm_dp_link_train(&sor->link);
31610472c21bSThierry Reding 	if (err < 0)
31620472c21bSThierry Reding 		dev_err(sor->dev, "link training failed: %d\n", err);
31630472c21bSThierry Reding 	else
31640472c21bSThierry Reding 		dev_dbg(sor->dev, "link training succeeded\n");
31650472c21bSThierry Reding 
31660472c21bSThierry Reding 	err = drm_dp_link_power_up(sor->aux, &sor->link);
31670472c21bSThierry Reding 	if (err < 0)
31680472c21bSThierry Reding 		dev_err(sor->dev, "failed to power up DP link: %d\n", err);
31690472c21bSThierry Reding 
31700472c21bSThierry Reding 	/* compute configuration */
31710472c21bSThierry Reding 	memset(&config, 0, sizeof(config));
31720472c21bSThierry Reding 	config.bits_per_pixel = state->bpc * 3;
31730472c21bSThierry Reding 
31740472c21bSThierry Reding 	err = tegra_sor_compute_config(sor, mode, &config, &sor->link);
31750472c21bSThierry Reding 	if (err < 0)
31760472c21bSThierry Reding 		dev_err(sor->dev, "failed to compute configuration: %d\n", err);
31770472c21bSThierry Reding 
31780472c21bSThierry Reding 	tegra_sor_apply_config(sor, &config);
31790472c21bSThierry Reding 	tegra_sor_mode_set(sor, mode, state);
31800472c21bSThierry Reding 	tegra_sor_update(sor);
31810472c21bSThierry Reding 
31820472c21bSThierry Reding 	err = tegra_sor_power_up(sor, 250);
31830472c21bSThierry Reding 	if (err < 0)
31840472c21bSThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
31850472c21bSThierry Reding 
31860472c21bSThierry Reding 	/* attach and wake up */
31870472c21bSThierry Reding 	err = tegra_sor_attach(sor);
31880472c21bSThierry Reding 	if (err < 0)
31890472c21bSThierry Reding 		dev_err(sor->dev, "failed to attach SOR: %d\n", err);
31900472c21bSThierry Reding 
31910472c21bSThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
31920472c21bSThierry Reding 	value |= SOR_ENABLE(sor->index);
31930472c21bSThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
31940472c21bSThierry Reding 
31950472c21bSThierry Reding 	tegra_dc_commit(dc);
31960472c21bSThierry Reding 
31970472c21bSThierry Reding 	err = tegra_sor_wakeup(sor);
31980472c21bSThierry Reding 	if (err < 0)
31990472c21bSThierry Reding 		dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
32000472c21bSThierry Reding }
32010472c21bSThierry Reding 
32020472c21bSThierry Reding static const struct drm_encoder_helper_funcs tegra_sor_dp_helpers = {
32030472c21bSThierry Reding 	.disable = tegra_sor_dp_disable,
32040472c21bSThierry Reding 	.enable = tegra_sor_dp_enable,
32050472c21bSThierry Reding 	.atomic_check = tegra_sor_encoder_atomic_check,
32060472c21bSThierry Reding };
32070472c21bSThierry Reding 
32081c3cc0dfSThierry Reding static const struct tegra_sor_ops tegra_sor_edp_ops = {
32091c3cc0dfSThierry Reding 	.name = "eDP",
32101c3cc0dfSThierry Reding };
32111c3cc0dfSThierry Reding 
32121c3cc0dfSThierry Reding static int tegra_sor_hdmi_probe(struct tegra_sor *sor)
32131c3cc0dfSThierry Reding {
32141c3cc0dfSThierry Reding 	int err;
32151c3cc0dfSThierry Reding 
32161c3cc0dfSThierry Reding 	sor->avdd_io_supply = devm_regulator_get(sor->dev, "avdd-io");
32171c3cc0dfSThierry Reding 	if (IS_ERR(sor->avdd_io_supply)) {
32181c3cc0dfSThierry Reding 		dev_err(sor->dev, "cannot get AVDD I/O supply: %ld\n",
32191c3cc0dfSThierry Reding 			PTR_ERR(sor->avdd_io_supply));
32201c3cc0dfSThierry Reding 		return PTR_ERR(sor->avdd_io_supply);
32211c3cc0dfSThierry Reding 	}
32221c3cc0dfSThierry Reding 
32231c3cc0dfSThierry Reding 	err = regulator_enable(sor->avdd_io_supply);
32241c3cc0dfSThierry Reding 	if (err < 0) {
32251c3cc0dfSThierry Reding 		dev_err(sor->dev, "failed to enable AVDD I/O supply: %d\n",
32261c3cc0dfSThierry Reding 			err);
32271c3cc0dfSThierry Reding 		return err;
32281c3cc0dfSThierry Reding 	}
32291c3cc0dfSThierry Reding 
32301c3cc0dfSThierry Reding 	sor->vdd_pll_supply = devm_regulator_get(sor->dev, "vdd-pll");
32311c3cc0dfSThierry Reding 	if (IS_ERR(sor->vdd_pll_supply)) {
32321c3cc0dfSThierry Reding 		dev_err(sor->dev, "cannot get VDD PLL supply: %ld\n",
32331c3cc0dfSThierry Reding 			PTR_ERR(sor->vdd_pll_supply));
32341c3cc0dfSThierry Reding 		return PTR_ERR(sor->vdd_pll_supply);
32351c3cc0dfSThierry Reding 	}
32361c3cc0dfSThierry Reding 
32371c3cc0dfSThierry Reding 	err = regulator_enable(sor->vdd_pll_supply);
32381c3cc0dfSThierry Reding 	if (err < 0) {
32391c3cc0dfSThierry Reding 		dev_err(sor->dev, "failed to enable VDD PLL supply: %d\n",
32401c3cc0dfSThierry Reding 			err);
32411c3cc0dfSThierry Reding 		return err;
32421c3cc0dfSThierry Reding 	}
32431c3cc0dfSThierry Reding 
32441c3cc0dfSThierry Reding 	sor->hdmi_supply = devm_regulator_get(sor->dev, "hdmi");
32451c3cc0dfSThierry Reding 	if (IS_ERR(sor->hdmi_supply)) {
32461c3cc0dfSThierry Reding 		dev_err(sor->dev, "cannot get HDMI supply: %ld\n",
32471c3cc0dfSThierry Reding 			PTR_ERR(sor->hdmi_supply));
32481c3cc0dfSThierry Reding 		return PTR_ERR(sor->hdmi_supply);
32491c3cc0dfSThierry Reding 	}
32501c3cc0dfSThierry Reding 
32511c3cc0dfSThierry Reding 	err = regulator_enable(sor->hdmi_supply);
32521c3cc0dfSThierry Reding 	if (err < 0) {
32531c3cc0dfSThierry Reding 		dev_err(sor->dev, "failed to enable HDMI supply: %d\n", err);
32541c3cc0dfSThierry Reding 		return err;
32551c3cc0dfSThierry Reding 	}
32561c3cc0dfSThierry Reding 
32571c3cc0dfSThierry Reding 	INIT_DELAYED_WORK(&sor->scdc, tegra_sor_hdmi_scdc_work);
32581c3cc0dfSThierry Reding 
32591c3cc0dfSThierry Reding 	return 0;
32601c3cc0dfSThierry Reding }
32611c3cc0dfSThierry Reding 
32621c3cc0dfSThierry Reding static int tegra_sor_hdmi_remove(struct tegra_sor *sor)
32631c3cc0dfSThierry Reding {
32641c3cc0dfSThierry Reding 	regulator_disable(sor->hdmi_supply);
32651c3cc0dfSThierry Reding 	regulator_disable(sor->vdd_pll_supply);
32661c3cc0dfSThierry Reding 	regulator_disable(sor->avdd_io_supply);
32671c3cc0dfSThierry Reding 
32681c3cc0dfSThierry Reding 	return 0;
32691c3cc0dfSThierry Reding }
32701c3cc0dfSThierry Reding 
32711c3cc0dfSThierry Reding static const struct tegra_sor_ops tegra_sor_hdmi_ops = {
32721c3cc0dfSThierry Reding 	.name = "HDMI",
32731c3cc0dfSThierry Reding 	.probe = tegra_sor_hdmi_probe,
32741c3cc0dfSThierry Reding 	.remove = tegra_sor_hdmi_remove,
32751c3cc0dfSThierry Reding };
32761c3cc0dfSThierry Reding 
32771c3cc0dfSThierry Reding static int tegra_sor_dp_probe(struct tegra_sor *sor)
32781c3cc0dfSThierry Reding {
32791c3cc0dfSThierry Reding 	int err;
32801c3cc0dfSThierry Reding 
32811c3cc0dfSThierry Reding 	sor->avdd_io_supply = devm_regulator_get(sor->dev, "avdd-io-hdmi-dp");
32821c3cc0dfSThierry Reding 	if (IS_ERR(sor->avdd_io_supply))
32831c3cc0dfSThierry Reding 		return PTR_ERR(sor->avdd_io_supply);
32841c3cc0dfSThierry Reding 
32851c3cc0dfSThierry Reding 	err = regulator_enable(sor->avdd_io_supply);
32861c3cc0dfSThierry Reding 	if (err < 0)
32871c3cc0dfSThierry Reding 		return err;
32881c3cc0dfSThierry Reding 
32891c3cc0dfSThierry Reding 	sor->vdd_pll_supply = devm_regulator_get(sor->dev, "vdd-hdmi-dp-pll");
32901c3cc0dfSThierry Reding 	if (IS_ERR(sor->vdd_pll_supply))
32911c3cc0dfSThierry Reding 		return PTR_ERR(sor->vdd_pll_supply);
32921c3cc0dfSThierry Reding 
32931c3cc0dfSThierry Reding 	err = regulator_enable(sor->vdd_pll_supply);
32941c3cc0dfSThierry Reding 	if (err < 0)
32951c3cc0dfSThierry Reding 		return err;
32961c3cc0dfSThierry Reding 
32971c3cc0dfSThierry Reding 	return 0;
32981c3cc0dfSThierry Reding }
32991c3cc0dfSThierry Reding 
33001c3cc0dfSThierry Reding static int tegra_sor_dp_remove(struct tegra_sor *sor)
33011c3cc0dfSThierry Reding {
33021c3cc0dfSThierry Reding 	regulator_disable(sor->vdd_pll_supply);
33031c3cc0dfSThierry Reding 	regulator_disable(sor->avdd_io_supply);
33041c3cc0dfSThierry Reding 
33051c3cc0dfSThierry Reding 	return 0;
33061c3cc0dfSThierry Reding }
33071c3cc0dfSThierry Reding 
33081c3cc0dfSThierry Reding static const struct tegra_sor_ops tegra_sor_dp_ops = {
33091c3cc0dfSThierry Reding 	.name = "DP",
33101c3cc0dfSThierry Reding 	.probe = tegra_sor_dp_probe,
33111c3cc0dfSThierry Reding 	.remove = tegra_sor_dp_remove,
33121c3cc0dfSThierry Reding };
33131c3cc0dfSThierry Reding 
33146b6b6042SThierry Reding static int tegra_sor_init(struct host1x_client *client)
33156b6b6042SThierry Reding {
33169910f5c4SThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->parent);
3317459cc2c6SThierry Reding 	const struct drm_encoder_helper_funcs *helpers = NULL;
33186b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
3319459cc2c6SThierry Reding 	int connector = DRM_MODE_CONNECTOR_Unknown;
3320459cc2c6SThierry Reding 	int encoder = DRM_MODE_ENCODER_NONE;
33216b6b6042SThierry Reding 	int err;
33226b6b6042SThierry Reding 
33239542c237SThierry Reding 	if (!sor->aux) {
33241c3cc0dfSThierry Reding 		if (sor->ops == &tegra_sor_hdmi_ops) {
3325459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_HDMIA;
3326459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
3327459cc2c6SThierry Reding 			helpers = &tegra_sor_hdmi_helpers;
3328459cc2c6SThierry Reding 		} else if (sor->soc->supports_lvds) {
3329459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_LVDS;
3330459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_LVDS;
3331459cc2c6SThierry Reding 		}
3332459cc2c6SThierry Reding 	} else {
33331c3cc0dfSThierry Reding 		if (sor->ops == &tegra_sor_edp_ops) {
3334459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_eDP;
3335459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
3336459cc2c6SThierry Reding 			helpers = &tegra_sor_edp_helpers;
33371c3cc0dfSThierry Reding 		} else {
3338459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_DisplayPort;
3339459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
33400472c21bSThierry Reding 			helpers = &tegra_sor_dp_helpers;
3341459cc2c6SThierry Reding 		}
3342c1763937SThierry Reding 
3343c1763937SThierry Reding 		sor->link.ops = &tegra_sor_dp_link_ops;
3344c1763937SThierry Reding 		sor->link.aux = sor->aux;
3345459cc2c6SThierry Reding 	}
33466b6b6042SThierry Reding 
33476b6b6042SThierry Reding 	sor->output.dev = sor->dev;
33486b6b6042SThierry Reding 
33496fad8f66SThierry Reding 	drm_connector_init(drm, &sor->output.connector,
33506fad8f66SThierry Reding 			   &tegra_sor_connector_funcs,
3351459cc2c6SThierry Reding 			   connector);
33526fad8f66SThierry Reding 	drm_connector_helper_add(&sor->output.connector,
33536fad8f66SThierry Reding 				 &tegra_sor_connector_helper_funcs);
33546fad8f66SThierry Reding 	sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
33556fad8f66SThierry Reding 
33566fad8f66SThierry Reding 	drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
335713a3d91fSVille Syrjälä 			 encoder, NULL);
3358459cc2c6SThierry Reding 	drm_encoder_helper_add(&sor->output.encoder, helpers);
33596fad8f66SThierry Reding 
3360cde4c44dSDaniel Vetter 	drm_connector_attach_encoder(&sor->output.connector,
33616fad8f66SThierry Reding 					  &sor->output.encoder);
33626fad8f66SThierry Reding 	drm_connector_register(&sor->output.connector);
33636fad8f66SThierry Reding 
3364ea130b24SThierry Reding 	err = tegra_output_init(drm, &sor->output);
3365ea130b24SThierry Reding 	if (err < 0) {
3366ea130b24SThierry Reding 		dev_err(client->dev, "failed to initialize output: %d\n", err);
3367ea130b24SThierry Reding 		return err;
3368ea130b24SThierry Reding 	}
33696fad8f66SThierry Reding 
3370c57997bcSThierry Reding 	tegra_output_find_possible_crtcs(&sor->output, drm);
33716b6b6042SThierry Reding 
33729542c237SThierry Reding 	if (sor->aux) {
33739542c237SThierry Reding 		err = drm_dp_aux_attach(sor->aux, &sor->output);
33746b6b6042SThierry Reding 		if (err < 0) {
33756b6b6042SThierry Reding 			dev_err(sor->dev, "failed to attach DP: %d\n", err);
33766b6b6042SThierry Reding 			return err;
33776b6b6042SThierry Reding 		}
33786b6b6042SThierry Reding 	}
33796b6b6042SThierry Reding 
3380535a65dbSTomeu Vizoso 	/*
3381535a65dbSTomeu Vizoso 	 * XXX: Remove this reset once proper hand-over from firmware to
3382535a65dbSTomeu Vizoso 	 * kernel is possible.
3383535a65dbSTomeu Vizoso 	 */
3384f8c79120SJon Hunter 	if (sor->rst) {
338511c632e1SThierry Reding 		err = reset_control_acquire(sor->rst);
338611c632e1SThierry Reding 		if (err < 0) {
338711c632e1SThierry Reding 			dev_err(sor->dev, "failed to acquire SOR reset: %d\n",
338811c632e1SThierry Reding 				err);
338911c632e1SThierry Reding 			return err;
339011c632e1SThierry Reding 		}
339111c632e1SThierry Reding 
3392535a65dbSTomeu Vizoso 		err = reset_control_assert(sor->rst);
3393535a65dbSTomeu Vizoso 		if (err < 0) {
3394f8c79120SJon Hunter 			dev_err(sor->dev, "failed to assert SOR reset: %d\n",
3395f8c79120SJon Hunter 				err);
3396535a65dbSTomeu Vizoso 			return err;
3397535a65dbSTomeu Vizoso 		}
3398f8c79120SJon Hunter 	}
3399535a65dbSTomeu Vizoso 
34006fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk);
34016fad8f66SThierry Reding 	if (err < 0) {
34026fad8f66SThierry Reding 		dev_err(sor->dev, "failed to enable clock: %d\n", err);
34036fad8f66SThierry Reding 		return err;
34046fad8f66SThierry Reding 	}
34056fad8f66SThierry Reding 
3406535a65dbSTomeu Vizoso 	usleep_range(1000, 3000);
3407535a65dbSTomeu Vizoso 
3408f8c79120SJon Hunter 	if (sor->rst) {
3409535a65dbSTomeu Vizoso 		err = reset_control_deassert(sor->rst);
3410535a65dbSTomeu Vizoso 		if (err < 0) {
3411f8c79120SJon Hunter 			dev_err(sor->dev, "failed to deassert SOR reset: %d\n",
3412f8c79120SJon Hunter 				err);
3413535a65dbSTomeu Vizoso 			return err;
3414535a65dbSTomeu Vizoso 		}
341511c632e1SThierry Reding 
341611c632e1SThierry Reding 		reset_control_release(sor->rst);
3417f8c79120SJon Hunter 	}
3418535a65dbSTomeu Vizoso 
34196fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk_safe);
34206fad8f66SThierry Reding 	if (err < 0)
34216fad8f66SThierry Reding 		return err;
34226fad8f66SThierry Reding 
34236fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk_dp);
34246fad8f66SThierry Reding 	if (err < 0)
34256fad8f66SThierry Reding 		return err;
34266fad8f66SThierry Reding 
34276b6b6042SThierry Reding 	return 0;
34286b6b6042SThierry Reding }
34296b6b6042SThierry Reding 
34306b6b6042SThierry Reding static int tegra_sor_exit(struct host1x_client *client)
34316b6b6042SThierry Reding {
34326b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
34336b6b6042SThierry Reding 	int err;
34346b6b6042SThierry Reding 
3435328ec69eSThierry Reding 	tegra_output_exit(&sor->output);
3436328ec69eSThierry Reding 
34379542c237SThierry Reding 	if (sor->aux) {
34389542c237SThierry Reding 		err = drm_dp_aux_detach(sor->aux);
34396b6b6042SThierry Reding 		if (err < 0) {
34406b6b6042SThierry Reding 			dev_err(sor->dev, "failed to detach DP: %d\n", err);
34416b6b6042SThierry Reding 			return err;
34426b6b6042SThierry Reding 		}
34436b6b6042SThierry Reding 	}
34446b6b6042SThierry Reding 
34456fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk_safe);
34466fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk_dp);
34476fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk);
34486fad8f66SThierry Reding 
34496b6b6042SThierry Reding 	return 0;
34506b6b6042SThierry Reding }
34516b6b6042SThierry Reding 
34526b6b6042SThierry Reding static const struct host1x_client_ops sor_client_ops = {
34536b6b6042SThierry Reding 	.init = tegra_sor_init,
34546b6b6042SThierry Reding 	.exit = tegra_sor_exit,
34556b6b6042SThierry Reding };
34566b6b6042SThierry Reding 
345730b49435SThierry Reding static const u8 tegra124_sor_xbar_cfg[5] = {
345830b49435SThierry Reding 	0, 1, 2, 3, 4
345930b49435SThierry Reding };
346030b49435SThierry Reding 
3461880cee0bSThierry Reding static const struct tegra_sor_regs tegra124_sor_regs = {
3462880cee0bSThierry Reding 	.head_state0 = 0x05,
3463880cee0bSThierry Reding 	.head_state1 = 0x07,
3464880cee0bSThierry Reding 	.head_state2 = 0x09,
3465880cee0bSThierry Reding 	.head_state3 = 0x0b,
3466880cee0bSThierry Reding 	.head_state4 = 0x0d,
3467880cee0bSThierry Reding 	.head_state5 = 0x0f,
3468880cee0bSThierry Reding 	.pll0 = 0x17,
3469880cee0bSThierry Reding 	.pll1 = 0x18,
3470880cee0bSThierry Reding 	.pll2 = 0x19,
3471880cee0bSThierry Reding 	.pll3 = 0x1a,
3472880cee0bSThierry Reding 	.dp_padctl0 = 0x5c,
3473880cee0bSThierry Reding 	.dp_padctl2 = 0x73,
3474880cee0bSThierry Reding };
3475880cee0bSThierry Reding 
3476c1763937SThierry Reding /* Tegra124 and Tegra132 have lanes 0 and 2 swapped. */
3477c1763937SThierry Reding static const u8 tegra124_sor_lane_map[4] = {
3478c1763937SThierry Reding 	2, 1, 0, 3,
3479c1763937SThierry Reding };
3480c1763937SThierry Reding 
3481c1763937SThierry Reding static const u8 tegra124_sor_voltage_swing[4][4][4] = {
3482c1763937SThierry Reding 	{
3483c1763937SThierry Reding 		{ 0x13, 0x19, 0x1e, 0x28 },
3484c1763937SThierry Reding 		{ 0x1e, 0x25, 0x2d, },
3485c1763937SThierry Reding 		{ 0x28, 0x32, },
3486c1763937SThierry Reding 		{ 0x3c, },
3487c1763937SThierry Reding 	}, {
3488c1763937SThierry Reding 		{ 0x12, 0x17, 0x1b, 0x25 },
3489c1763937SThierry Reding 		{ 0x1c, 0x23, 0x2a, },
3490c1763937SThierry Reding 		{ 0x25, 0x2f, },
3491c1763937SThierry Reding 		{ 0x39, }
3492c1763937SThierry Reding 	}, {
3493c1763937SThierry Reding 		{ 0x12, 0x16, 0x1a, 0x22 },
3494c1763937SThierry Reding 		{ 0x1b, 0x20, 0x27, },
3495c1763937SThierry Reding 		{ 0x24, 0x2d, },
3496c1763937SThierry Reding 		{ 0x36, },
3497c1763937SThierry Reding 	}, {
3498c1763937SThierry Reding 		{ 0x11, 0x14, 0x17, 0x1f },
3499c1763937SThierry Reding 		{ 0x19, 0x1e, 0x24, },
3500c1763937SThierry Reding 		{ 0x22, 0x2a, },
3501c1763937SThierry Reding 		{ 0x32, },
3502c1763937SThierry Reding 	},
3503c1763937SThierry Reding };
3504c1763937SThierry Reding 
3505c1763937SThierry Reding static const u8 tegra124_sor_pre_emphasis[4][4][4] = {
3506c1763937SThierry Reding 	{
3507c1763937SThierry Reding 		{ 0x00, 0x09, 0x13, 0x25 },
3508c1763937SThierry Reding 		{ 0x00, 0x0f, 0x1e, },
3509c1763937SThierry Reding 		{ 0x00, 0x14, },
3510c1763937SThierry Reding 		{ 0x00, },
3511c1763937SThierry Reding 	}, {
3512c1763937SThierry Reding 		{ 0x00, 0x0a, 0x14, 0x28 },
3513c1763937SThierry Reding 		{ 0x00, 0x0f, 0x1e, },
3514c1763937SThierry Reding 		{ 0x00, 0x14, },
3515c1763937SThierry Reding 		{ 0x00 },
3516c1763937SThierry Reding 	}, {
3517c1763937SThierry Reding 		{ 0x00, 0x0a, 0x14, 0x28 },
3518c1763937SThierry Reding 		{ 0x00, 0x0f, 0x1e, },
3519c1763937SThierry Reding 		{ 0x00, 0x14, },
3520c1763937SThierry Reding 		{ 0x00, },
3521c1763937SThierry Reding 	}, {
3522c1763937SThierry Reding 		{ 0x00, 0x0a, 0x14, 0x28 },
3523c1763937SThierry Reding 		{ 0x00, 0x0f, 0x1e, },
3524c1763937SThierry Reding 		{ 0x00, 0x14, },
3525c1763937SThierry Reding 		{ 0x00, },
3526c1763937SThierry Reding 	},
3527c1763937SThierry Reding };
3528c1763937SThierry Reding 
3529c1763937SThierry Reding static const u8 tegra124_sor_post_cursor[4][4][4] = {
3530c1763937SThierry Reding 	{
3531c1763937SThierry Reding 		{ 0x00, 0x00, 0x00, 0x00 },
3532c1763937SThierry Reding 		{ 0x00, 0x00, 0x00, },
3533c1763937SThierry Reding 		{ 0x00, 0x00, },
3534c1763937SThierry Reding 		{ 0x00, },
3535c1763937SThierry Reding 	}, {
3536c1763937SThierry Reding 		{ 0x02, 0x02, 0x04, 0x05 },
3537c1763937SThierry Reding 		{ 0x02, 0x04, 0x05, },
3538c1763937SThierry Reding 		{ 0x04, 0x05, },
3539c1763937SThierry Reding 		{ 0x05, },
3540c1763937SThierry Reding 	}, {
3541c1763937SThierry Reding 		{ 0x04, 0x05, 0x08, 0x0b },
3542c1763937SThierry Reding 		{ 0x05, 0x09, 0x0b, },
3543c1763937SThierry Reding 		{ 0x08, 0x0a, },
3544c1763937SThierry Reding 		{ 0x0b, },
3545c1763937SThierry Reding 	}, {
3546c1763937SThierry Reding 		{ 0x05, 0x09, 0x0b, 0x12 },
3547c1763937SThierry Reding 		{ 0x09, 0x0d, 0x12, },
3548c1763937SThierry Reding 		{ 0x0b, 0x0f, },
3549c1763937SThierry Reding 		{ 0x12, },
3550c1763937SThierry Reding 	},
3551c1763937SThierry Reding };
3552c1763937SThierry Reding 
3553c1763937SThierry Reding static const u8 tegra124_sor_tx_pu[4][4][4] = {
3554c1763937SThierry Reding 	{
3555c1763937SThierry Reding 		{ 0x20, 0x30, 0x40, 0x60 },
3556c1763937SThierry Reding 		{ 0x30, 0x40, 0x60, },
3557c1763937SThierry Reding 		{ 0x40, 0x60, },
3558c1763937SThierry Reding 		{ 0x60, },
3559c1763937SThierry Reding 	}, {
3560c1763937SThierry Reding 		{ 0x20, 0x20, 0x30, 0x50 },
3561c1763937SThierry Reding 		{ 0x30, 0x40, 0x50, },
3562c1763937SThierry Reding 		{ 0x40, 0x50, },
3563c1763937SThierry Reding 		{ 0x60, },
3564c1763937SThierry Reding 	}, {
3565c1763937SThierry Reding 		{ 0x20, 0x20, 0x30, 0x40, },
3566c1763937SThierry Reding 		{ 0x30, 0x30, 0x40, },
3567c1763937SThierry Reding 		{ 0x40, 0x50, },
3568c1763937SThierry Reding 		{ 0x60, },
3569c1763937SThierry Reding 	}, {
3570c1763937SThierry Reding 		{ 0x20, 0x20, 0x20, 0x40, },
3571c1763937SThierry Reding 		{ 0x30, 0x30, 0x40, },
3572c1763937SThierry Reding 		{ 0x40, 0x40, },
3573c1763937SThierry Reding 		{ 0x60, },
3574c1763937SThierry Reding 	},
3575c1763937SThierry Reding };
3576c1763937SThierry Reding 
3577459cc2c6SThierry Reding static const struct tegra_sor_soc tegra124_sor = {
3578459cc2c6SThierry Reding 	.supports_edp = true,
3579459cc2c6SThierry Reding 	.supports_lvds = true,
3580459cc2c6SThierry Reding 	.supports_hdmi = false,
3581459cc2c6SThierry Reding 	.supports_dp = false,
3582880cee0bSThierry Reding 	.regs = &tegra124_sor_regs,
3583c57997bcSThierry Reding 	.has_nvdisplay = false,
358430b49435SThierry Reding 	.xbar_cfg = tegra124_sor_xbar_cfg,
3585c1763937SThierry Reding 	.lane_map = tegra124_sor_lane_map,
3586c1763937SThierry Reding 	.voltage_swing = tegra124_sor_voltage_swing,
3587c1763937SThierry Reding 	.pre_emphasis = tegra124_sor_pre_emphasis,
3588c1763937SThierry Reding 	.post_cursor = tegra124_sor_post_cursor,
3589c1763937SThierry Reding 	.tx_pu = tegra124_sor_tx_pu,
3590c1763937SThierry Reding };
3591c1763937SThierry Reding 
3592c1763937SThierry Reding static const u8 tegra132_sor_pre_emphasis[4][4][4] = {
3593c1763937SThierry Reding 	{
3594c1763937SThierry Reding 		{ 0x00, 0x08, 0x12, 0x24 },
3595c1763937SThierry Reding 		{ 0x01, 0x0e, 0x1d, },
3596c1763937SThierry Reding 		{ 0x01, 0x13, },
3597c1763937SThierry Reding 		{ 0x00, },
3598c1763937SThierry Reding 	}, {
3599c1763937SThierry Reding 		{ 0x00, 0x08, 0x12, 0x24 },
3600c1763937SThierry Reding 		{ 0x00, 0x0e, 0x1d, },
3601c1763937SThierry Reding 		{ 0x00, 0x13, },
3602c1763937SThierry Reding 		{ 0x00 },
3603c1763937SThierry Reding 	}, {
3604c1763937SThierry Reding 		{ 0x00, 0x08, 0x12, 0x24 },
3605c1763937SThierry Reding 		{ 0x00, 0x0e, 0x1d, },
3606c1763937SThierry Reding 		{ 0x00, 0x13, },
3607c1763937SThierry Reding 		{ 0x00, },
3608c1763937SThierry Reding 	}, {
3609c1763937SThierry Reding 		{ 0x00, 0x08, 0x12, 0x24 },
3610c1763937SThierry Reding 		{ 0x00, 0x0e, 0x1d, },
3611c1763937SThierry Reding 		{ 0x00, 0x13, },
3612c1763937SThierry Reding 		{ 0x00, },
3613c1763937SThierry Reding 	},
3614c1763937SThierry Reding };
3615c1763937SThierry Reding 
3616c1763937SThierry Reding static const struct tegra_sor_soc tegra132_sor = {
3617c1763937SThierry Reding 	.supports_edp = true,
3618c1763937SThierry Reding 	.supports_lvds = true,
3619c1763937SThierry Reding 	.supports_hdmi = false,
3620c1763937SThierry Reding 	.supports_dp = false,
3621c1763937SThierry Reding 	.regs = &tegra124_sor_regs,
3622c1763937SThierry Reding 	.has_nvdisplay = false,
3623c1763937SThierry Reding 	.xbar_cfg = tegra124_sor_xbar_cfg,
3624c1763937SThierry Reding 	.lane_map = tegra124_sor_lane_map,
3625c1763937SThierry Reding 	.voltage_swing = tegra124_sor_voltage_swing,
3626c1763937SThierry Reding 	.pre_emphasis = tegra132_sor_pre_emphasis,
3627c1763937SThierry Reding 	.post_cursor = tegra124_sor_post_cursor,
3628c1763937SThierry Reding 	.tx_pu = tegra124_sor_tx_pu,
3629459cc2c6SThierry Reding };
3630459cc2c6SThierry Reding 
3631880cee0bSThierry Reding static const struct tegra_sor_regs tegra210_sor_regs = {
3632880cee0bSThierry Reding 	.head_state0 = 0x05,
3633880cee0bSThierry Reding 	.head_state1 = 0x07,
3634880cee0bSThierry Reding 	.head_state2 = 0x09,
3635880cee0bSThierry Reding 	.head_state3 = 0x0b,
3636880cee0bSThierry Reding 	.head_state4 = 0x0d,
3637880cee0bSThierry Reding 	.head_state5 = 0x0f,
3638880cee0bSThierry Reding 	.pll0 = 0x17,
3639880cee0bSThierry Reding 	.pll1 = 0x18,
3640880cee0bSThierry Reding 	.pll2 = 0x19,
3641880cee0bSThierry Reding 	.pll3 = 0x1a,
3642880cee0bSThierry Reding 	.dp_padctl0 = 0x5c,
3643880cee0bSThierry Reding 	.dp_padctl2 = 0x73,
3644880cee0bSThierry Reding };
3645880cee0bSThierry Reding 
3646c1763937SThierry Reding static const u8 tegra210_sor_xbar_cfg[5] = {
3647c1763937SThierry Reding 	2, 1, 0, 3, 4
3648c1763937SThierry Reding };
3649c1763937SThierry Reding 
36500472c21bSThierry Reding static const u8 tegra210_sor_lane_map[4] = {
36510472c21bSThierry Reding 	0, 1, 2, 3,
36520472c21bSThierry Reding };
36530472c21bSThierry Reding 
3654459cc2c6SThierry Reding static const struct tegra_sor_soc tegra210_sor = {
3655459cc2c6SThierry Reding 	.supports_edp = true,
3656459cc2c6SThierry Reding 	.supports_lvds = false,
3657459cc2c6SThierry Reding 	.supports_hdmi = false,
3658459cc2c6SThierry Reding 	.supports_dp = false,
3659c1763937SThierry Reding 
3660880cee0bSThierry Reding 	.regs = &tegra210_sor_regs,
3661c57997bcSThierry Reding 	.has_nvdisplay = false,
366230b49435SThierry Reding 
3663c1763937SThierry Reding 	.xbar_cfg = tegra210_sor_xbar_cfg,
36640472c21bSThierry Reding 	.lane_map = tegra210_sor_lane_map,
36650472c21bSThierry Reding 	.voltage_swing = tegra124_sor_voltage_swing,
36660472c21bSThierry Reding 	.pre_emphasis = tegra124_sor_pre_emphasis,
36670472c21bSThierry Reding 	.post_cursor = tegra124_sor_post_cursor,
36680472c21bSThierry Reding 	.tx_pu = tegra124_sor_tx_pu,
3669459cc2c6SThierry Reding };
3670459cc2c6SThierry Reding 
3671459cc2c6SThierry Reding static const struct tegra_sor_soc tegra210_sor1 = {
3672459cc2c6SThierry Reding 	.supports_edp = false,
3673459cc2c6SThierry Reding 	.supports_lvds = false,
3674459cc2c6SThierry Reding 	.supports_hdmi = true,
3675459cc2c6SThierry Reding 	.supports_dp = true,
3676459cc2c6SThierry Reding 
3677880cee0bSThierry Reding 	.regs = &tegra210_sor_regs,
3678c57997bcSThierry Reding 	.has_nvdisplay = false,
3679880cee0bSThierry Reding 
3680459cc2c6SThierry Reding 	.num_settings = ARRAY_SIZE(tegra210_sor_hdmi_defaults),
3681459cc2c6SThierry Reding 	.settings = tegra210_sor_hdmi_defaults,
368230b49435SThierry Reding 	.xbar_cfg = tegra210_sor_xbar_cfg,
36830472c21bSThierry Reding 	.lane_map = tegra210_sor_lane_map,
36840472c21bSThierry Reding 	.voltage_swing = tegra124_sor_voltage_swing,
36850472c21bSThierry Reding 	.pre_emphasis = tegra124_sor_pre_emphasis,
36860472c21bSThierry Reding 	.post_cursor = tegra124_sor_post_cursor,
36870472c21bSThierry Reding 	.tx_pu = tegra124_sor_tx_pu,
3688459cc2c6SThierry Reding };
3689459cc2c6SThierry Reding 
3690c57997bcSThierry Reding static const struct tegra_sor_regs tegra186_sor_regs = {
3691c57997bcSThierry Reding 	.head_state0 = 0x151,
3692c57997bcSThierry Reding 	.head_state1 = 0x154,
3693c57997bcSThierry Reding 	.head_state2 = 0x157,
3694c57997bcSThierry Reding 	.head_state3 = 0x15a,
3695c57997bcSThierry Reding 	.head_state4 = 0x15d,
3696c57997bcSThierry Reding 	.head_state5 = 0x160,
3697c57997bcSThierry Reding 	.pll0 = 0x163,
3698c57997bcSThierry Reding 	.pll1 = 0x164,
3699c57997bcSThierry Reding 	.pll2 = 0x165,
3700c57997bcSThierry Reding 	.pll3 = 0x166,
3701c57997bcSThierry Reding 	.dp_padctl0 = 0x168,
3702c57997bcSThierry Reding 	.dp_padctl2 = 0x16a,
3703c57997bcSThierry Reding };
3704c57997bcSThierry Reding 
37050472c21bSThierry Reding static const u8 tegra186_sor_voltage_swing[4][4][4] = {
37060472c21bSThierry Reding 	{
37070472c21bSThierry Reding 		{ 0x13, 0x19, 0x1e, 0x28 },
37080472c21bSThierry Reding 		{ 0x1e, 0x25, 0x2d, },
37090472c21bSThierry Reding 		{ 0x28, 0x32, },
37100472c21bSThierry Reding 		{ 0x39, },
37110472c21bSThierry Reding 	}, {
37120472c21bSThierry Reding 		{ 0x12, 0x16, 0x1b, 0x25 },
37130472c21bSThierry Reding 		{ 0x1c, 0x23, 0x2a, },
37140472c21bSThierry Reding 		{ 0x25, 0x2f, },
37150472c21bSThierry Reding 		{ 0x37, }
37160472c21bSThierry Reding 	}, {
37170472c21bSThierry Reding 		{ 0x12, 0x16, 0x1a, 0x22 },
37180472c21bSThierry Reding 		{ 0x1b, 0x20, 0x27, },
37190472c21bSThierry Reding 		{ 0x24, 0x2d, },
37200472c21bSThierry Reding 		{ 0x35, },
37210472c21bSThierry Reding 	}, {
37220472c21bSThierry Reding 		{ 0x11, 0x14, 0x17, 0x1f },
37230472c21bSThierry Reding 		{ 0x19, 0x1e, 0x24, },
37240472c21bSThierry Reding 		{ 0x22, 0x2a, },
37250472c21bSThierry Reding 		{ 0x32, },
37260472c21bSThierry Reding 	},
37270472c21bSThierry Reding };
37280472c21bSThierry Reding 
37290472c21bSThierry Reding static const u8 tegra186_sor_pre_emphasis[4][4][4] = {
37300472c21bSThierry Reding 	{
37310472c21bSThierry Reding 		{ 0x00, 0x08, 0x12, 0x24 },
37320472c21bSThierry Reding 		{ 0x01, 0x0e, 0x1d, },
37330472c21bSThierry Reding 		{ 0x01, 0x13, },
37340472c21bSThierry Reding 		{ 0x00, },
37350472c21bSThierry Reding 	}, {
37360472c21bSThierry Reding 		{ 0x00, 0x08, 0x12, 0x24 },
37370472c21bSThierry Reding 		{ 0x00, 0x0e, 0x1d, },
37380472c21bSThierry Reding 		{ 0x00, 0x13, },
37390472c21bSThierry Reding 		{ 0x00 },
37400472c21bSThierry Reding 	}, {
37410472c21bSThierry Reding 		{ 0x00, 0x08, 0x14, 0x24 },
37420472c21bSThierry Reding 		{ 0x00, 0x0e, 0x1d, },
37430472c21bSThierry Reding 		{ 0x00, 0x13, },
37440472c21bSThierry Reding 		{ 0x00, },
37450472c21bSThierry Reding 	}, {
37460472c21bSThierry Reding 		{ 0x00, 0x08, 0x12, 0x24 },
37470472c21bSThierry Reding 		{ 0x00, 0x0e, 0x1d, },
37480472c21bSThierry Reding 		{ 0x00, 0x13, },
37490472c21bSThierry Reding 		{ 0x00, },
37500472c21bSThierry Reding 	},
37510472c21bSThierry Reding };
37520472c21bSThierry Reding 
3753c57997bcSThierry Reding static const struct tegra_sor_soc tegra186_sor = {
3754c57997bcSThierry Reding 	.supports_edp = false,
3755c57997bcSThierry Reding 	.supports_lvds = false,
3756c57997bcSThierry Reding 	.supports_hdmi = true,
3757c57997bcSThierry Reding 	.supports_dp = true,
3758c57997bcSThierry Reding 
3759c57997bcSThierry Reding 	.regs = &tegra186_sor_regs,
3760c57997bcSThierry Reding 	.has_nvdisplay = true,
3761c57997bcSThierry Reding 
3762c57997bcSThierry Reding 	.num_settings = ARRAY_SIZE(tegra186_sor_hdmi_defaults),
3763c57997bcSThierry Reding 	.settings = tegra186_sor_hdmi_defaults,
3764c57997bcSThierry Reding 	.xbar_cfg = tegra124_sor_xbar_cfg,
37650472c21bSThierry Reding 	.lane_map = tegra124_sor_lane_map,
37660472c21bSThierry Reding 	.voltage_swing = tegra186_sor_voltage_swing,
37670472c21bSThierry Reding 	.pre_emphasis = tegra186_sor_pre_emphasis,
37680472c21bSThierry Reding 	.post_cursor = tegra124_sor_post_cursor,
37690472c21bSThierry Reding 	.tx_pu = tegra124_sor_tx_pu,
3770c57997bcSThierry Reding };
3771c57997bcSThierry Reding 
37729b6c14b8SThierry Reding static const struct tegra_sor_regs tegra194_sor_regs = {
37739b6c14b8SThierry Reding 	.head_state0 = 0x151,
37749b6c14b8SThierry Reding 	.head_state1 = 0x155,
37759b6c14b8SThierry Reding 	.head_state2 = 0x159,
37769b6c14b8SThierry Reding 	.head_state3 = 0x15d,
37779b6c14b8SThierry Reding 	.head_state4 = 0x161,
37789b6c14b8SThierry Reding 	.head_state5 = 0x165,
37799b6c14b8SThierry Reding 	.pll0 = 0x169,
37809b6c14b8SThierry Reding 	.pll1 = 0x16a,
37819b6c14b8SThierry Reding 	.pll2 = 0x16b,
37829b6c14b8SThierry Reding 	.pll3 = 0x16c,
37839b6c14b8SThierry Reding 	.dp_padctl0 = 0x16e,
37849b6c14b8SThierry Reding 	.dp_padctl2 = 0x16f,
37859b6c14b8SThierry Reding };
37869b6c14b8SThierry Reding 
37879b6c14b8SThierry Reding static const struct tegra_sor_soc tegra194_sor = {
37889b6c14b8SThierry Reding 	.supports_edp = true,
37899b6c14b8SThierry Reding 	.supports_lvds = false,
37909b6c14b8SThierry Reding 	.supports_hdmi = true,
37919b6c14b8SThierry Reding 	.supports_dp = true,
37929b6c14b8SThierry Reding 
37939b6c14b8SThierry Reding 	.regs = &tegra194_sor_regs,
37949b6c14b8SThierry Reding 	.has_nvdisplay = true,
37959b6c14b8SThierry Reding 
37969b6c14b8SThierry Reding 	.num_settings = ARRAY_SIZE(tegra194_sor_hdmi_defaults),
37979b6c14b8SThierry Reding 	.settings = tegra194_sor_hdmi_defaults,
37989b6c14b8SThierry Reding 
37999b6c14b8SThierry Reding 	.xbar_cfg = tegra210_sor_xbar_cfg,
3800bae88815SThierry Reding 	.lane_map = tegra124_sor_lane_map,
3801bae88815SThierry Reding 	.voltage_swing = tegra186_sor_voltage_swing,
3802bae88815SThierry Reding 	.pre_emphasis = tegra186_sor_pre_emphasis,
3803bae88815SThierry Reding 	.post_cursor = tegra124_sor_post_cursor,
3804bae88815SThierry Reding 	.tx_pu = tegra124_sor_tx_pu,
38059b6c14b8SThierry Reding };
38069b6c14b8SThierry Reding 
3807459cc2c6SThierry Reding static const struct of_device_id tegra_sor_of_match[] = {
38089b6c14b8SThierry Reding 	{ .compatible = "nvidia,tegra194-sor", .data = &tegra194_sor },
3809c57997bcSThierry Reding 	{ .compatible = "nvidia,tegra186-sor", .data = &tegra186_sor },
3810459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra210-sor1", .data = &tegra210_sor1 },
3811459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra210-sor", .data = &tegra210_sor },
3812c1763937SThierry Reding 	{ .compatible = "nvidia,tegra132-sor", .data = &tegra132_sor },
3813459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra124-sor", .data = &tegra124_sor },
3814459cc2c6SThierry Reding 	{ },
3815459cc2c6SThierry Reding };
3816459cc2c6SThierry Reding MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
3817459cc2c6SThierry Reding 
3818c57997bcSThierry Reding static int tegra_sor_parse_dt(struct tegra_sor *sor)
3819c57997bcSThierry Reding {
3820c57997bcSThierry Reding 	struct device_node *np = sor->dev->of_node;
38216d6c815dSThierry Reding 	u32 xbar_cfg[5];
38226d6c815dSThierry Reding 	unsigned int i;
3823c57997bcSThierry Reding 	u32 value;
3824c57997bcSThierry Reding 	int err;
3825c57997bcSThierry Reding 
3826c57997bcSThierry Reding 	if (sor->soc->has_nvdisplay) {
3827c57997bcSThierry Reding 		err = of_property_read_u32(np, "nvidia,interface", &value);
3828c57997bcSThierry Reding 		if (err < 0)
3829c57997bcSThierry Reding 			return err;
3830c57997bcSThierry Reding 
3831c57997bcSThierry Reding 		sor->index = value;
3832c57997bcSThierry Reding 
3833c57997bcSThierry Reding 		/*
3834c57997bcSThierry Reding 		 * override the default that we already set for Tegra210 and
3835c57997bcSThierry Reding 		 * earlier
3836c57997bcSThierry Reding 		 */
3837c57997bcSThierry Reding 		sor->pad = TEGRA_IO_PAD_HDMI_DP0 + sor->index;
383824e64f86SThierry Reding 	} else {
383924e64f86SThierry Reding 		if (sor->soc->supports_edp)
384024e64f86SThierry Reding 			sor->index = 0;
384124e64f86SThierry Reding 		else
384224e64f86SThierry Reding 			sor->index = 1;
3843c57997bcSThierry Reding 	}
3844c57997bcSThierry Reding 
38456d6c815dSThierry Reding 	err = of_property_read_u32_array(np, "nvidia,xbar-cfg", xbar_cfg, 5);
38466d6c815dSThierry Reding 	if (err < 0) {
38476d6c815dSThierry Reding 		/* fall back to default per-SoC XBAR configuration */
38486d6c815dSThierry Reding 		for (i = 0; i < 5; i++)
38496d6c815dSThierry Reding 			sor->xbar_cfg[i] = sor->soc->xbar_cfg[i];
38506d6c815dSThierry Reding 	} else {
38516d6c815dSThierry Reding 		/* copy cells to SOR XBAR configuration */
38526d6c815dSThierry Reding 		for (i = 0; i < 5; i++)
38536d6c815dSThierry Reding 			sor->xbar_cfg[i] = xbar_cfg[i];
3854c57997bcSThierry Reding 	}
3855c57997bcSThierry Reding 
38566b6b6042SThierry Reding 	return 0;
38578e2988a7SThierry Reding }
38588e2988a7SThierry Reding 
38598e2988a7SThierry Reding static irqreturn_t tegra_sor_irq(int irq, void *data)
38608e2988a7SThierry Reding {
38618e2988a7SThierry Reding 	struct tegra_sor *sor = data;
38628e2988a7SThierry Reding 	u32 value;
38638e2988a7SThierry Reding 
38648e2988a7SThierry Reding 	value = tegra_sor_readl(sor, SOR_INT_STATUS);
38658e2988a7SThierry Reding 	tegra_sor_writel(sor, value, SOR_INT_STATUS);
38668e2988a7SThierry Reding 
38678e2988a7SThierry Reding 	if (value & SOR_INT_CODEC_SCRATCH0) {
38688e2988a7SThierry Reding 		value = tegra_sor_readl(sor, SOR_AUDIO_HDA_CODEC_SCRATCH0);
38698e2988a7SThierry Reding 
38708e2988a7SThierry Reding 		if (value & SOR_AUDIO_HDA_CODEC_SCRATCH0_VALID) {
3871cd54fb96SThierry Reding 			unsigned int format;
38728e2988a7SThierry Reding 
38738e2988a7SThierry Reding 			format = value & SOR_AUDIO_HDA_CODEC_SCRATCH0_FMT_MASK;
38748e2988a7SThierry Reding 
3875fad7b806SThierry Reding 			tegra_hda_parse_format(format, &sor->format);
38768e2988a7SThierry Reding 
38778e2988a7SThierry Reding 			tegra_sor_hdmi_audio_enable(sor);
38788e2988a7SThierry Reding 		} else {
38798e2988a7SThierry Reding 			tegra_sor_hdmi_audio_disable(sor);
38808e2988a7SThierry Reding 		}
38818e2988a7SThierry Reding 	}
38828e2988a7SThierry Reding 
38838e2988a7SThierry Reding 	return IRQ_HANDLED;
38848e2988a7SThierry Reding }
38858e2988a7SThierry Reding 
38866b6b6042SThierry Reding static int tegra_sor_probe(struct platform_device *pdev)
38876b6b6042SThierry Reding {
38886b6b6042SThierry Reding 	struct device_node *np;
38896b6b6042SThierry Reding 	struct tegra_sor *sor;
38906b6b6042SThierry Reding 	struct resource *regs;
38916b6b6042SThierry Reding 	int err;
38926b6b6042SThierry Reding 
38936b6b6042SThierry Reding 	sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
38946b6b6042SThierry Reding 	if (!sor)
38956b6b6042SThierry Reding 		return -ENOMEM;
38966b6b6042SThierry Reding 
38975faea3d0SThierry Reding 	sor->soc = of_device_get_match_data(&pdev->dev);
38986b6b6042SThierry Reding 	sor->output.dev = sor->dev = &pdev->dev;
3899459cc2c6SThierry Reding 
3900459cc2c6SThierry Reding 	sor->settings = devm_kmemdup(&pdev->dev, sor->soc->settings,
3901459cc2c6SThierry Reding 				     sor->soc->num_settings *
3902459cc2c6SThierry Reding 					sizeof(*sor->settings),
3903459cc2c6SThierry Reding 				     GFP_KERNEL);
3904459cc2c6SThierry Reding 	if (!sor->settings)
3905459cc2c6SThierry Reding 		return -ENOMEM;
3906459cc2c6SThierry Reding 
3907459cc2c6SThierry Reding 	sor->num_settings = sor->soc->num_settings;
39086b6b6042SThierry Reding 
39096b6b6042SThierry Reding 	np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
39106b6b6042SThierry Reding 	if (np) {
39119542c237SThierry Reding 		sor->aux = drm_dp_aux_find_by_of_node(np);
39126b6b6042SThierry Reding 		of_node_put(np);
39136b6b6042SThierry Reding 
39149542c237SThierry Reding 		if (!sor->aux)
39156b6b6042SThierry Reding 			return -EPROBE_DEFER;
39166f684de5SThierry Reding 
39176f684de5SThierry Reding 		sor->output.ddc = &sor->aux->ddc;
39186b6b6042SThierry Reding 	}
39196b6b6042SThierry Reding 
39209542c237SThierry Reding 	if (!sor->aux) {
3921459cc2c6SThierry Reding 		if (sor->soc->supports_hdmi) {
3922459cc2c6SThierry Reding 			sor->ops = &tegra_sor_hdmi_ops;
3923c57997bcSThierry Reding 			sor->pad = TEGRA_IO_PAD_HDMI;
3924459cc2c6SThierry Reding 		} else if (sor->soc->supports_lvds) {
3925459cc2c6SThierry Reding 			dev_err(&pdev->dev, "LVDS not supported yet\n");
3926459cc2c6SThierry Reding 			return -ENODEV;
3927459cc2c6SThierry Reding 		} else {
3928459cc2c6SThierry Reding 			dev_err(&pdev->dev, "unknown (non-DP) support\n");
3929459cc2c6SThierry Reding 			return -ENODEV;
3930459cc2c6SThierry Reding 		}
3931459cc2c6SThierry Reding 	} else {
3932459cc2c6SThierry Reding 		if (sor->soc->supports_edp) {
3933459cc2c6SThierry Reding 			sor->ops = &tegra_sor_edp_ops;
3934c57997bcSThierry Reding 			sor->pad = TEGRA_IO_PAD_LVDS;
3935459cc2c6SThierry Reding 		} else if (sor->soc->supports_dp) {
39360472c21bSThierry Reding 			sor->ops = &tegra_sor_dp_ops;
3937*d23691f6SThierry Reding 			sor->pad = TEGRA_IO_PAD_LVDS;
3938459cc2c6SThierry Reding 		} else {
3939459cc2c6SThierry Reding 			dev_err(&pdev->dev, "unknown (DP) support\n");
3940459cc2c6SThierry Reding 			return -ENODEV;
3941459cc2c6SThierry Reding 		}
3942459cc2c6SThierry Reding 	}
3943459cc2c6SThierry Reding 
3944c57997bcSThierry Reding 	err = tegra_sor_parse_dt(sor);
3945c57997bcSThierry Reding 	if (err < 0)
3946c57997bcSThierry Reding 		return err;
3947c57997bcSThierry Reding 
39486b6b6042SThierry Reding 	err = tegra_output_probe(&sor->output);
39494dbdc740SThierry Reding 	if (err < 0) {
39504dbdc740SThierry Reding 		dev_err(&pdev->dev, "failed to probe output: %d\n", err);
39516b6b6042SThierry Reding 		return err;
39524dbdc740SThierry Reding 	}
39536b6b6042SThierry Reding 
3954459cc2c6SThierry Reding 	if (sor->ops && sor->ops->probe) {
3955459cc2c6SThierry Reding 		err = sor->ops->probe(sor);
3956459cc2c6SThierry Reding 		if (err < 0) {
3957459cc2c6SThierry Reding 			dev_err(&pdev->dev, "failed to probe %s: %d\n",
3958459cc2c6SThierry Reding 				sor->ops->name, err);
3959459cc2c6SThierry Reding 			goto output;
3960459cc2c6SThierry Reding 		}
3961459cc2c6SThierry Reding 	}
3962459cc2c6SThierry Reding 
39636b6b6042SThierry Reding 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
39646b6b6042SThierry Reding 	sor->regs = devm_ioremap_resource(&pdev->dev, regs);
3965459cc2c6SThierry Reding 	if (IS_ERR(sor->regs)) {
3966459cc2c6SThierry Reding 		err = PTR_ERR(sor->regs);
3967459cc2c6SThierry Reding 		goto remove;
3968459cc2c6SThierry Reding 	}
39696b6b6042SThierry Reding 
39708e2988a7SThierry Reding 	err = platform_get_irq(pdev, 0);
39718e2988a7SThierry Reding 	if (err < 0) {
39728e2988a7SThierry Reding 		dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
39738e2988a7SThierry Reding 		goto remove;
39748e2988a7SThierry Reding 	}
39758e2988a7SThierry Reding 
39768e2988a7SThierry Reding 	sor->irq = err;
39778e2988a7SThierry Reding 
39788e2988a7SThierry Reding 	err = devm_request_irq(sor->dev, sor->irq, tegra_sor_irq, 0,
39798e2988a7SThierry Reding 			       dev_name(sor->dev), sor);
39808e2988a7SThierry Reding 	if (err < 0) {
39818e2988a7SThierry Reding 		dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
39828e2988a7SThierry Reding 		goto remove;
39838e2988a7SThierry Reding 	}
39848e2988a7SThierry Reding 
398511c632e1SThierry Reding 	sor->rst = devm_reset_control_get_exclusive_released(&pdev->dev, "sor");
39864dbdc740SThierry Reding 	if (IS_ERR(sor->rst)) {
3987459cc2c6SThierry Reding 		err = PTR_ERR(sor->rst);
3988180b46ecSThierry Reding 
3989180b46ecSThierry Reding 		if (err != -EBUSY || WARN_ON(!pdev->dev.pm_domain)) {
3990f8c79120SJon Hunter 			dev_err(&pdev->dev, "failed to get reset control: %d\n",
3991f8c79120SJon Hunter 				err);
3992459cc2c6SThierry Reding 			goto remove;
39934dbdc740SThierry Reding 		}
3994180b46ecSThierry Reding 
3995180b46ecSThierry Reding 		/*
3996180b46ecSThierry Reding 		 * At this point, the reset control is most likely being used
3997180b46ecSThierry Reding 		 * by the generic power domain implementation. With any luck
3998180b46ecSThierry Reding 		 * the power domain will have taken care of resetting the SOR
3999180b46ecSThierry Reding 		 * and we don't have to do anything.
4000180b46ecSThierry Reding 		 */
4001180b46ecSThierry Reding 		sor->rst = NULL;
4002f8c79120SJon Hunter 	}
40036b6b6042SThierry Reding 
40046b6b6042SThierry Reding 	sor->clk = devm_clk_get(&pdev->dev, NULL);
40054dbdc740SThierry Reding 	if (IS_ERR(sor->clk)) {
4006459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk);
4007459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get module clock: %d\n", err);
4008459cc2c6SThierry Reding 		goto remove;
40094dbdc740SThierry Reding 	}
40106b6b6042SThierry Reding 
4011618dee39SThierry Reding 	if (sor->soc->supports_hdmi || sor->soc->supports_dp) {
4012e1335e2fSThierry Reding 		struct device_node *np = pdev->dev.of_node;
4013e1335e2fSThierry Reding 		const char *name;
4014e1335e2fSThierry Reding 
4015e1335e2fSThierry Reding 		/*
4016e1335e2fSThierry Reding 		 * For backwards compatibility with Tegra210 device trees,
4017e1335e2fSThierry Reding 		 * fall back to the old clock name "source" if the new "out"
4018e1335e2fSThierry Reding 		 * clock is not available.
4019e1335e2fSThierry Reding 		 */
4020e1335e2fSThierry Reding 		if (of_property_match_string(np, "clock-names", "out") < 0)
4021e1335e2fSThierry Reding 			name = "source";
4022e1335e2fSThierry Reding 		else
4023e1335e2fSThierry Reding 			name = "out";
4024e1335e2fSThierry Reding 
4025e1335e2fSThierry Reding 		sor->clk_out = devm_clk_get(&pdev->dev, name);
4026e1335e2fSThierry Reding 		if (IS_ERR(sor->clk_out)) {
4027e1335e2fSThierry Reding 			err = PTR_ERR(sor->clk_out);
4028e1335e2fSThierry Reding 			dev_err(sor->dev, "failed to get %s clock: %d\n",
4029e1335e2fSThierry Reding 				name, err);
4030618dee39SThierry Reding 			goto remove;
4031618dee39SThierry Reding 		}
40321087fac1SThierry Reding 	} else {
4033d780537fSThierry Reding 		/* fall back to the module clock on SOR0 (eDP/LVDS only) */
40341087fac1SThierry Reding 		sor->clk_out = sor->clk;
4035618dee39SThierry Reding 	}
4036618dee39SThierry Reding 
40376b6b6042SThierry Reding 	sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
40384dbdc740SThierry Reding 	if (IS_ERR(sor->clk_parent)) {
4039459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_parent);
4040459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get parent clock: %d\n", err);
4041459cc2c6SThierry Reding 		goto remove;
40424dbdc740SThierry Reding 	}
40436b6b6042SThierry Reding 
40446b6b6042SThierry Reding 	sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
40454dbdc740SThierry Reding 	if (IS_ERR(sor->clk_safe)) {
4046459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_safe);
4047459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get safe clock: %d\n", err);
4048459cc2c6SThierry Reding 		goto remove;
40494dbdc740SThierry Reding 	}
40506b6b6042SThierry Reding 
40516b6b6042SThierry Reding 	sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
40524dbdc740SThierry Reding 	if (IS_ERR(sor->clk_dp)) {
4053459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_dp);
4054459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get DP clock: %d\n", err);
4055459cc2c6SThierry Reding 		goto remove;
40564dbdc740SThierry Reding 	}
40576b6b6042SThierry Reding 
4058e1335e2fSThierry Reding 	/*
4059e1335e2fSThierry Reding 	 * Starting with Tegra186, the BPMP provides an implementation for
4060e1335e2fSThierry Reding 	 * the pad output clock, so we have to look it up from device tree.
4061e1335e2fSThierry Reding 	 */
4062e1335e2fSThierry Reding 	sor->clk_pad = devm_clk_get(&pdev->dev, "pad");
4063e1335e2fSThierry Reding 	if (IS_ERR(sor->clk_pad)) {
4064e1335e2fSThierry Reding 		if (sor->clk_pad != ERR_PTR(-ENOENT)) {
4065e1335e2fSThierry Reding 			err = PTR_ERR(sor->clk_pad);
4066e1335e2fSThierry Reding 			goto remove;
4067e1335e2fSThierry Reding 		}
4068e1335e2fSThierry Reding 
4069e1335e2fSThierry Reding 		/*
4070e1335e2fSThierry Reding 		 * If the pad output clock is not available, then we assume
4071e1335e2fSThierry Reding 		 * we're on Tegra210 or earlier and have to provide our own
4072e1335e2fSThierry Reding 		 * implementation.
4073e1335e2fSThierry Reding 		 */
4074e1335e2fSThierry Reding 		sor->clk_pad = NULL;
4075e1335e2fSThierry Reding 	}
4076e1335e2fSThierry Reding 
4077e1335e2fSThierry Reding 	/*
4078e1335e2fSThierry Reding 	 * The bootloader may have set up the SOR such that it's module clock
4079e1335e2fSThierry Reding 	 * is sourced by one of the display PLLs. However, that doesn't work
4080e1335e2fSThierry Reding 	 * without properly having set up other bits of the SOR.
4081e1335e2fSThierry Reding 	 */
4082e1335e2fSThierry Reding 	err = clk_set_parent(sor->clk_out, sor->clk_safe);
4083e1335e2fSThierry Reding 	if (err < 0) {
4084e1335e2fSThierry Reding 		dev_err(&pdev->dev, "failed to use safe clock: %d\n", err);
4085e1335e2fSThierry Reding 		goto remove;
4086e1335e2fSThierry Reding 	}
4087e1335e2fSThierry Reding 
4088aaff8bd2SThierry Reding 	platform_set_drvdata(pdev, sor);
4089aaff8bd2SThierry Reding 	pm_runtime_enable(&pdev->dev);
4090aaff8bd2SThierry Reding 
4091e1335e2fSThierry Reding 	/*
4092e1335e2fSThierry Reding 	 * On Tegra210 and earlier, provide our own implementation for the
4093e1335e2fSThierry Reding 	 * pad output clock.
4094e1335e2fSThierry Reding 	 */
4095e1335e2fSThierry Reding 	if (!sor->clk_pad) {
40964bdf4710SThierry Reding 		char *name;
40974bdf4710SThierry Reding 
4098e1335e2fSThierry Reding 		err = pm_runtime_get_sync(&pdev->dev);
4099e1335e2fSThierry Reding 		if (err < 0) {
4100e1335e2fSThierry Reding 			dev_err(&pdev->dev, "failed to get runtime PM: %d\n",
4101e1335e2fSThierry Reding 				err);
4102e1335e2fSThierry Reding 			goto remove;
4103e1335e2fSThierry Reding 		}
4104b299221cSThierry Reding 
41054bdf4710SThierry Reding 		name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "sor%u_pad_clkout", sor->index);
41064bdf4710SThierry Reding 		if (!name) {
41074bdf4710SThierry Reding 			err = -ENOMEM;
41084bdf4710SThierry Reding 			goto remove;
41094bdf4710SThierry Reding 		}
41104bdf4710SThierry Reding 
41114bdf4710SThierry Reding 		sor->clk_pad = tegra_clk_sor_pad_register(sor, name);
4112e1335e2fSThierry Reding 		pm_runtime_put(&pdev->dev);
4113e1335e2fSThierry Reding 	}
4114e1335e2fSThierry Reding 
4115e1335e2fSThierry Reding 	if (IS_ERR(sor->clk_pad)) {
4116e1335e2fSThierry Reding 		err = PTR_ERR(sor->clk_pad);
4117e1335e2fSThierry Reding 		dev_err(&pdev->dev, "failed to register SOR pad clock: %d\n",
4118e1335e2fSThierry Reding 			err);
4119b299221cSThierry Reding 		goto remove;
4120b299221cSThierry Reding 	}
4121b299221cSThierry Reding 
41226b6b6042SThierry Reding 	INIT_LIST_HEAD(&sor->client.list);
41236b6b6042SThierry Reding 	sor->client.ops = &sor_client_ops;
41246b6b6042SThierry Reding 	sor->client.dev = &pdev->dev;
41256b6b6042SThierry Reding 
41266b6b6042SThierry Reding 	err = host1x_client_register(&sor->client);
41276b6b6042SThierry Reding 	if (err < 0) {
41286b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
41296b6b6042SThierry Reding 			err);
4130459cc2c6SThierry Reding 		goto remove;
41316b6b6042SThierry Reding 	}
41326b6b6042SThierry Reding 
41336b6b6042SThierry Reding 	return 0;
4134459cc2c6SThierry Reding 
4135459cc2c6SThierry Reding remove:
4136459cc2c6SThierry Reding 	if (sor->ops && sor->ops->remove)
4137459cc2c6SThierry Reding 		sor->ops->remove(sor);
4138459cc2c6SThierry Reding output:
4139459cc2c6SThierry Reding 	tegra_output_remove(&sor->output);
4140459cc2c6SThierry Reding 	return err;
41416b6b6042SThierry Reding }
41426b6b6042SThierry Reding 
41436b6b6042SThierry Reding static int tegra_sor_remove(struct platform_device *pdev)
41446b6b6042SThierry Reding {
41456b6b6042SThierry Reding 	struct tegra_sor *sor = platform_get_drvdata(pdev);
41466b6b6042SThierry Reding 	int err;
41476b6b6042SThierry Reding 
4148aaff8bd2SThierry Reding 	pm_runtime_disable(&pdev->dev);
4149aaff8bd2SThierry Reding 
41506b6b6042SThierry Reding 	err = host1x_client_unregister(&sor->client);
41516b6b6042SThierry Reding 	if (err < 0) {
41526b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
41536b6b6042SThierry Reding 			err);
41546b6b6042SThierry Reding 		return err;
41556b6b6042SThierry Reding 	}
41566b6b6042SThierry Reding 
4157459cc2c6SThierry Reding 	if (sor->ops && sor->ops->remove) {
4158459cc2c6SThierry Reding 		err = sor->ops->remove(sor);
4159459cc2c6SThierry Reding 		if (err < 0)
4160459cc2c6SThierry Reding 			dev_err(&pdev->dev, "failed to remove SOR: %d\n", err);
4161459cc2c6SThierry Reding 	}
4162459cc2c6SThierry Reding 
4163328ec69eSThierry Reding 	tegra_output_remove(&sor->output);
41646b6b6042SThierry Reding 
41656b6b6042SThierry Reding 	return 0;
41666b6b6042SThierry Reding }
41676b6b6042SThierry Reding 
4168aaff8bd2SThierry Reding #ifdef CONFIG_PM
4169aaff8bd2SThierry Reding static int tegra_sor_suspend(struct device *dev)
4170aaff8bd2SThierry Reding {
4171aaff8bd2SThierry Reding 	struct tegra_sor *sor = dev_get_drvdata(dev);
4172aaff8bd2SThierry Reding 	int err;
4173aaff8bd2SThierry Reding 
4174f8c79120SJon Hunter 	if (sor->rst) {
4175aaff8bd2SThierry Reding 		err = reset_control_assert(sor->rst);
4176aaff8bd2SThierry Reding 		if (err < 0) {
4177aaff8bd2SThierry Reding 			dev_err(dev, "failed to assert reset: %d\n", err);
4178aaff8bd2SThierry Reding 			return err;
4179aaff8bd2SThierry Reding 		}
418011c632e1SThierry Reding 
418111c632e1SThierry Reding 		reset_control_release(sor->rst);
4182f8c79120SJon Hunter 	}
4183aaff8bd2SThierry Reding 
4184aaff8bd2SThierry Reding 	usleep_range(1000, 2000);
4185aaff8bd2SThierry Reding 
4186aaff8bd2SThierry Reding 	clk_disable_unprepare(sor->clk);
4187aaff8bd2SThierry Reding 
4188aaff8bd2SThierry Reding 	return 0;
4189aaff8bd2SThierry Reding }
4190aaff8bd2SThierry Reding 
4191aaff8bd2SThierry Reding static int tegra_sor_resume(struct device *dev)
4192aaff8bd2SThierry Reding {
4193aaff8bd2SThierry Reding 	struct tegra_sor *sor = dev_get_drvdata(dev);
4194aaff8bd2SThierry Reding 	int err;
4195aaff8bd2SThierry Reding 
4196aaff8bd2SThierry Reding 	err = clk_prepare_enable(sor->clk);
4197aaff8bd2SThierry Reding 	if (err < 0) {
4198aaff8bd2SThierry Reding 		dev_err(dev, "failed to enable clock: %d\n", err);
4199aaff8bd2SThierry Reding 		return err;
4200aaff8bd2SThierry Reding 	}
4201aaff8bd2SThierry Reding 
4202aaff8bd2SThierry Reding 	usleep_range(1000, 2000);
4203aaff8bd2SThierry Reding 
4204f8c79120SJon Hunter 	if (sor->rst) {
420511c632e1SThierry Reding 		err = reset_control_acquire(sor->rst);
420611c632e1SThierry Reding 		if (err < 0) {
420711c632e1SThierry Reding 			dev_err(dev, "failed to acquire reset: %d\n", err);
420811c632e1SThierry Reding 			clk_disable_unprepare(sor->clk);
420911c632e1SThierry Reding 			return err;
421011c632e1SThierry Reding 		}
421111c632e1SThierry Reding 
4212aaff8bd2SThierry Reding 		err = reset_control_deassert(sor->rst);
4213aaff8bd2SThierry Reding 		if (err < 0) {
4214aaff8bd2SThierry Reding 			dev_err(dev, "failed to deassert reset: %d\n", err);
421511c632e1SThierry Reding 			reset_control_release(sor->rst);
4216aaff8bd2SThierry Reding 			clk_disable_unprepare(sor->clk);
4217aaff8bd2SThierry Reding 			return err;
4218aaff8bd2SThierry Reding 		}
4219f8c79120SJon Hunter 	}
4220aaff8bd2SThierry Reding 
4221aaff8bd2SThierry Reding 	return 0;
4222aaff8bd2SThierry Reding }
4223aaff8bd2SThierry Reding #endif
4224aaff8bd2SThierry Reding 
4225aaff8bd2SThierry Reding static const struct dev_pm_ops tegra_sor_pm_ops = {
4226aaff8bd2SThierry Reding 	SET_RUNTIME_PM_OPS(tegra_sor_suspend, tegra_sor_resume, NULL)
4227aaff8bd2SThierry Reding };
4228aaff8bd2SThierry Reding 
42296b6b6042SThierry Reding struct platform_driver tegra_sor_driver = {
42306b6b6042SThierry Reding 	.driver = {
42316b6b6042SThierry Reding 		.name = "tegra-sor",
42326b6b6042SThierry Reding 		.of_match_table = tegra_sor_of_match,
4233aaff8bd2SThierry Reding 		.pm = &tegra_sor_pm_ops,
42346b6b6042SThierry Reding 	},
42356b6b6042SThierry Reding 	.probe = tegra_sor_probe,
42366b6b6042SThierry Reding 	.remove = tegra_sor_remove,
42376b6b6042SThierry Reding };
4238