blob: 529768d977b8c257cedf9d9398c1b0bab6057a7a [file] [log] [blame]
Thierry Redingedec4af2012-11-15 21:28:23 +00001/*
2 * Copyright (C) 2012 Avionic Design GmbH
3 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
10#include <linux/clk.h>
Thierry Reding9eb9b222013-09-24 16:32:47 +020011#include <linux/debugfs.h>
Thierry Reding59682712014-11-28 16:50:59 +010012#include <linux/gpio.h>
Thierry Reding9eb9b222013-09-24 16:32:47 +020013#include <linux/hdmi.h>
14#include <linux/regulator/consumer.h>
Stephen Warrenca480802013-11-06 16:20:54 -070015#include <linux/reset.h>
Thierry Redingac24c222012-11-23 15:14:00 +010016
Thierry Reding4aa3df72014-11-24 16:27:13 +010017#include <drm/drm_atomic_helper.h>
Thierry Reding59682712014-11-28 16:50:59 +010018#include <drm/drm_crtc.h>
19#include <drm/drm_crtc_helper.h>
20
Thierry Reding2ccb3962015-01-15 13:43:18 +010021#include <sound/hda_verbs.h>
22
Thierry Redingedec4af2012-11-15 21:28:23 +000023#include "hdmi.h"
24#include "drm.h"
25#include "dc.h"
26
Thierry Reding2ccb3962015-01-15 13:43:18 +010027#define HDMI_ELD_BUFFER_SIZE 96
28
Thierry Reding59af0592013-10-14 09:43:05 +020029struct tmds_config {
30 unsigned int pclk;
31 u32 pll0;
32 u32 pll1;
33 u32 pe_current;
34 u32 drive_current;
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +020035 u32 peak_current;
Thierry Reding59af0592013-10-14 09:43:05 +020036};
37
38struct tegra_hdmi_config {
39 const struct tmds_config *tmds;
40 unsigned int num_tmds;
41
42 unsigned long fuse_override_offset;
Thierry Reding4ee8cee2014-12-08 16:25:14 +010043 u32 fuse_override_value;
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +020044
45 bool has_sor_io_peak_current;
Thierry Reding2ccb3962015-01-15 13:43:18 +010046 bool has_hda;
47 bool has_hbr;
Thierry Reding59af0592013-10-14 09:43:05 +020048};
49
Thierry Redingedec4af2012-11-15 21:28:23 +000050struct tegra_hdmi {
Thierry Reding776dc382013-10-14 14:43:22 +020051 struct host1x_client client;
Thierry Redingedec4af2012-11-15 21:28:23 +000052 struct tegra_output output;
53 struct device *dev;
54
Thierry Redingfb50a112014-02-28 16:57:34 +010055 struct regulator *hdmi;
Thierry Redingedec4af2012-11-15 21:28:23 +000056 struct regulator *pll;
Thierry Reding88685682014-04-16 10:24:12 +020057 struct regulator *vdd;
Thierry Redingedec4af2012-11-15 21:28:23 +000058
59 void __iomem *regs;
60 unsigned int irq;
61
62 struct clk *clk_parent;
63 struct clk *clk;
Stephen Warrenca480802013-11-06 16:20:54 -070064 struct reset_control *rst;
Thierry Redingedec4af2012-11-15 21:28:23 +000065
Thierry Reding59af0592013-10-14 09:43:05 +020066 const struct tegra_hdmi_config *config;
67
Thierry Redingedec4af2012-11-15 21:28:23 +000068 unsigned int audio_source;
Thierry Reding2ccb3962015-01-15 13:43:18 +010069 unsigned int audio_sample_rate;
70 unsigned int audio_channels;
71
72 unsigned int pixel_clock;
Thierry Redingedec4af2012-11-15 21:28:23 +000073 bool stereo;
74 bool dvi;
75
76 struct drm_info_list *debugfs_files;
77 struct drm_minor *minor;
78 struct dentry *debugfs;
79};
80
81static inline struct tegra_hdmi *
Thierry Reding776dc382013-10-14 14:43:22 +020082host1x_client_to_hdmi(struct host1x_client *client)
Thierry Redingedec4af2012-11-15 21:28:23 +000083{
84 return container_of(client, struct tegra_hdmi, client);
85}
86
87static inline struct tegra_hdmi *to_hdmi(struct tegra_output *output)
88{
89 return container_of(output, struct tegra_hdmi, output);
90}
91
92#define HDMI_AUDIOCLK_FREQ 216000000
93#define HDMI_REKEY_DEFAULT 56
94
95enum {
96 AUTO = 0,
97 SPDIF,
98 HDA,
99};
100
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100101static inline u32 tegra_hdmi_readl(struct tegra_hdmi *hdmi,
102 unsigned long offset)
Thierry Redingedec4af2012-11-15 21:28:23 +0000103{
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100104 return readl(hdmi->regs + (offset << 2));
Thierry Redingedec4af2012-11-15 21:28:23 +0000105}
106
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100107static inline void tegra_hdmi_writel(struct tegra_hdmi *hdmi, u32 value,
108 unsigned long offset)
Thierry Redingedec4af2012-11-15 21:28:23 +0000109{
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100110 writel(value, hdmi->regs + (offset << 2));
Thierry Redingedec4af2012-11-15 21:28:23 +0000111}
112
113struct tegra_hdmi_audio_config {
114 unsigned int pclk;
115 unsigned int n;
116 unsigned int cts;
117 unsigned int aval;
118};
119
120static const struct tegra_hdmi_audio_config tegra_hdmi_audio_32k[] = {
121 { 25200000, 4096, 25200, 24000 },
122 { 27000000, 4096, 27000, 24000 },
123 { 74250000, 4096, 74250, 24000 },
124 { 148500000, 4096, 148500, 24000 },
125 { 0, 0, 0, 0 },
126};
127
128static const struct tegra_hdmi_audio_config tegra_hdmi_audio_44_1k[] = {
129 { 25200000, 5880, 26250, 25000 },
130 { 27000000, 5880, 28125, 25000 },
131 { 74250000, 4704, 61875, 20000 },
132 { 148500000, 4704, 123750, 20000 },
133 { 0, 0, 0, 0 },
134};
135
136static const struct tegra_hdmi_audio_config tegra_hdmi_audio_48k[] = {
137 { 25200000, 6144, 25200, 24000 },
138 { 27000000, 6144, 27000, 24000 },
139 { 74250000, 6144, 74250, 24000 },
140 { 148500000, 6144, 148500, 24000 },
141 { 0, 0, 0, 0 },
142};
143
144static const struct tegra_hdmi_audio_config tegra_hdmi_audio_88_2k[] = {
145 { 25200000, 11760, 26250, 25000 },
146 { 27000000, 11760, 28125, 25000 },
147 { 74250000, 9408, 61875, 20000 },
148 { 148500000, 9408, 123750, 20000 },
149 { 0, 0, 0, 0 },
150};
151
152static const struct tegra_hdmi_audio_config tegra_hdmi_audio_96k[] = {
153 { 25200000, 12288, 25200, 24000 },
154 { 27000000, 12288, 27000, 24000 },
155 { 74250000, 12288, 74250, 24000 },
156 { 148500000, 12288, 148500, 24000 },
157 { 0, 0, 0, 0 },
158};
159
160static const struct tegra_hdmi_audio_config tegra_hdmi_audio_176_4k[] = {
161 { 25200000, 23520, 26250, 25000 },
162 { 27000000, 23520, 28125, 25000 },
163 { 74250000, 18816, 61875, 20000 },
164 { 148500000, 18816, 123750, 20000 },
165 { 0, 0, 0, 0 },
166};
167
168static const struct tegra_hdmi_audio_config tegra_hdmi_audio_192k[] = {
169 { 25200000, 24576, 25200, 24000 },
170 { 27000000, 24576, 27000, 24000 },
171 { 74250000, 24576, 74250, 24000 },
172 { 148500000, 24576, 148500, 24000 },
173 { 0, 0, 0, 0 },
174};
175
Thierry Redingf27db962013-09-30 15:14:41 +0200176static const struct tmds_config tegra20_tmds_config[] = {
Lucas Stachfa416dd2012-12-19 21:38:55 +0000177 { /* slow pixel clock modes */
Thierry Redingedec4af2012-11-15 21:28:23 +0000178 .pclk = 27000000,
179 .pll0 = SOR_PLL_BG_V17_S(3) | SOR_PLL_ICHPMP(1) |
180 SOR_PLL_RESISTORSEL | SOR_PLL_VCOCAP(0) |
181 SOR_PLL_TX_REG_LOAD(3),
182 .pll1 = SOR_PLL_TMDS_TERM_ENABLE,
183 .pe_current = PE_CURRENT0(PE_CURRENT_0_0_mA) |
184 PE_CURRENT1(PE_CURRENT_0_0_mA) |
185 PE_CURRENT2(PE_CURRENT_0_0_mA) |
186 PE_CURRENT3(PE_CURRENT_0_0_mA),
187 .drive_current = DRIVE_CURRENT_LANE0(DRIVE_CURRENT_7_125_mA) |
188 DRIVE_CURRENT_LANE1(DRIVE_CURRENT_7_125_mA) |
189 DRIVE_CURRENT_LANE2(DRIVE_CURRENT_7_125_mA) |
190 DRIVE_CURRENT_LANE3(DRIVE_CURRENT_7_125_mA),
Lucas Stachfa416dd2012-12-19 21:38:55 +0000191 },
192 { /* high pixel clock modes */
Thierry Redingedec4af2012-11-15 21:28:23 +0000193 .pclk = UINT_MAX,
194 .pll0 = SOR_PLL_BG_V17_S(3) | SOR_PLL_ICHPMP(1) |
195 SOR_PLL_RESISTORSEL | SOR_PLL_VCOCAP(1) |
196 SOR_PLL_TX_REG_LOAD(3),
197 .pll1 = SOR_PLL_TMDS_TERM_ENABLE | SOR_PLL_PE_EN,
198 .pe_current = PE_CURRENT0(PE_CURRENT_6_0_mA) |
199 PE_CURRENT1(PE_CURRENT_6_0_mA) |
200 PE_CURRENT2(PE_CURRENT_6_0_mA) |
201 PE_CURRENT3(PE_CURRENT_6_0_mA),
202 .drive_current = DRIVE_CURRENT_LANE0(DRIVE_CURRENT_7_125_mA) |
203 DRIVE_CURRENT_LANE1(DRIVE_CURRENT_7_125_mA) |
204 DRIVE_CURRENT_LANE2(DRIVE_CURRENT_7_125_mA) |
205 DRIVE_CURRENT_LANE3(DRIVE_CURRENT_7_125_mA),
206 },
207};
208
Thierry Redingf27db962013-09-30 15:14:41 +0200209static const struct tmds_config tegra30_tmds_config[] = {
Thierry Redingedec4af2012-11-15 21:28:23 +0000210 { /* 480p modes */
211 .pclk = 27000000,
212 .pll0 = SOR_PLL_BG_V17_S(3) | SOR_PLL_ICHPMP(1) |
213 SOR_PLL_RESISTORSEL | SOR_PLL_VCOCAP(0) |
214 SOR_PLL_TX_REG_LOAD(0),
215 .pll1 = SOR_PLL_TMDS_TERM_ENABLE,
216 .pe_current = PE_CURRENT0(PE_CURRENT_0_0_mA) |
217 PE_CURRENT1(PE_CURRENT_0_0_mA) |
218 PE_CURRENT2(PE_CURRENT_0_0_mA) |
219 PE_CURRENT3(PE_CURRENT_0_0_mA),
220 .drive_current = DRIVE_CURRENT_LANE0(DRIVE_CURRENT_5_250_mA) |
221 DRIVE_CURRENT_LANE1(DRIVE_CURRENT_5_250_mA) |
222 DRIVE_CURRENT_LANE2(DRIVE_CURRENT_5_250_mA) |
223 DRIVE_CURRENT_LANE3(DRIVE_CURRENT_5_250_mA),
224 }, { /* 720p modes */
225 .pclk = 74250000,
226 .pll0 = SOR_PLL_BG_V17_S(3) | SOR_PLL_ICHPMP(1) |
227 SOR_PLL_RESISTORSEL | SOR_PLL_VCOCAP(1) |
228 SOR_PLL_TX_REG_LOAD(0),
229 .pll1 = SOR_PLL_TMDS_TERM_ENABLE | SOR_PLL_PE_EN,
230 .pe_current = PE_CURRENT0(PE_CURRENT_5_0_mA) |
231 PE_CURRENT1(PE_CURRENT_5_0_mA) |
232 PE_CURRENT2(PE_CURRENT_5_0_mA) |
233 PE_CURRENT3(PE_CURRENT_5_0_mA),
234 .drive_current = DRIVE_CURRENT_LANE0(DRIVE_CURRENT_5_250_mA) |
235 DRIVE_CURRENT_LANE1(DRIVE_CURRENT_5_250_mA) |
236 DRIVE_CURRENT_LANE2(DRIVE_CURRENT_5_250_mA) |
237 DRIVE_CURRENT_LANE3(DRIVE_CURRENT_5_250_mA),
238 }, { /* 1080p modes */
239 .pclk = UINT_MAX,
240 .pll0 = SOR_PLL_BG_V17_S(3) | SOR_PLL_ICHPMP(1) |
241 SOR_PLL_RESISTORSEL | SOR_PLL_VCOCAP(3) |
242 SOR_PLL_TX_REG_LOAD(0),
243 .pll1 = SOR_PLL_TMDS_TERM_ENABLE | SOR_PLL_PE_EN,
244 .pe_current = PE_CURRENT0(PE_CURRENT_5_0_mA) |
245 PE_CURRENT1(PE_CURRENT_5_0_mA) |
246 PE_CURRENT2(PE_CURRENT_5_0_mA) |
247 PE_CURRENT3(PE_CURRENT_5_0_mA),
248 .drive_current = DRIVE_CURRENT_LANE0(DRIVE_CURRENT_5_250_mA) |
249 DRIVE_CURRENT_LANE1(DRIVE_CURRENT_5_250_mA) |
250 DRIVE_CURRENT_LANE2(DRIVE_CURRENT_5_250_mA) |
251 DRIVE_CURRENT_LANE3(DRIVE_CURRENT_5_250_mA),
252 },
253};
254
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +0200255static const struct tmds_config tegra114_tmds_config[] = {
256 { /* 480p/576p / 25.2MHz/27MHz modes */
257 .pclk = 27000000,
258 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
259 SOR_PLL_VCOCAP(0) | SOR_PLL_RESISTORSEL,
260 .pll1 = SOR_PLL_LOADADJ(3) | SOR_PLL_TMDS_TERMADJ(0),
261 .pe_current = PE_CURRENT0(PE_CURRENT_0_mA_T114) |
262 PE_CURRENT1(PE_CURRENT_0_mA_T114) |
263 PE_CURRENT2(PE_CURRENT_0_mA_T114) |
264 PE_CURRENT3(PE_CURRENT_0_mA_T114),
265 .drive_current =
266 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_10_400_mA_T114) |
267 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_10_400_mA_T114) |
268 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_10_400_mA_T114) |
269 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_10_400_mA_T114),
270 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_0_000_mA) |
271 PEAK_CURRENT_LANE1(PEAK_CURRENT_0_000_mA) |
272 PEAK_CURRENT_LANE2(PEAK_CURRENT_0_000_mA) |
273 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_000_mA),
274 }, { /* 720p / 74.25MHz modes */
275 .pclk = 74250000,
276 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
277 SOR_PLL_VCOCAP(1) | SOR_PLL_RESISTORSEL,
278 .pll1 = SOR_PLL_PE_EN | SOR_PLL_LOADADJ(3) |
279 SOR_PLL_TMDS_TERMADJ(0),
280 .pe_current = PE_CURRENT0(PE_CURRENT_15_mA_T114) |
281 PE_CURRENT1(PE_CURRENT_15_mA_T114) |
282 PE_CURRENT2(PE_CURRENT_15_mA_T114) |
283 PE_CURRENT3(PE_CURRENT_15_mA_T114),
284 .drive_current =
285 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_10_400_mA_T114) |
286 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_10_400_mA_T114) |
287 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_10_400_mA_T114) |
288 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_10_400_mA_T114),
289 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_0_000_mA) |
290 PEAK_CURRENT_LANE1(PEAK_CURRENT_0_000_mA) |
291 PEAK_CURRENT_LANE2(PEAK_CURRENT_0_000_mA) |
292 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_000_mA),
293 }, { /* 1080p / 148.5MHz modes */
294 .pclk = 148500000,
295 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
296 SOR_PLL_VCOCAP(3) | SOR_PLL_RESISTORSEL,
297 .pll1 = SOR_PLL_PE_EN | SOR_PLL_LOADADJ(3) |
298 SOR_PLL_TMDS_TERMADJ(0),
299 .pe_current = PE_CURRENT0(PE_CURRENT_10_mA_T114) |
300 PE_CURRENT1(PE_CURRENT_10_mA_T114) |
301 PE_CURRENT2(PE_CURRENT_10_mA_T114) |
302 PE_CURRENT3(PE_CURRENT_10_mA_T114),
303 .drive_current =
304 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_12_400_mA_T114) |
305 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_12_400_mA_T114) |
306 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_12_400_mA_T114) |
307 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_12_400_mA_T114),
308 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_0_000_mA) |
309 PEAK_CURRENT_LANE1(PEAK_CURRENT_0_000_mA) |
310 PEAK_CURRENT_LANE2(PEAK_CURRENT_0_000_mA) |
311 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_000_mA),
312 }, { /* 225/297MHz modes */
313 .pclk = UINT_MAX,
314 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
315 SOR_PLL_VCOCAP(0xf) | SOR_PLL_RESISTORSEL,
316 .pll1 = SOR_PLL_LOADADJ(3) | SOR_PLL_TMDS_TERMADJ(7)
317 | SOR_PLL_TMDS_TERM_ENABLE,
318 .pe_current = PE_CURRENT0(PE_CURRENT_0_mA_T114) |
319 PE_CURRENT1(PE_CURRENT_0_mA_T114) |
320 PE_CURRENT2(PE_CURRENT_0_mA_T114) |
321 PE_CURRENT3(PE_CURRENT_0_mA_T114),
322 .drive_current =
323 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_25_200_mA_T114) |
324 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_25_200_mA_T114) |
325 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_25_200_mA_T114) |
326 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_19_200_mA_T114),
327 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_3_000_mA) |
328 PEAK_CURRENT_LANE1(PEAK_CURRENT_3_000_mA) |
329 PEAK_CURRENT_LANE2(PEAK_CURRENT_3_000_mA) |
330 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_800_mA),
331 },
332};
333
Thierry Redingfb7be702013-11-15 16:07:32 +0100334static const struct tmds_config tegra124_tmds_config[] = {
335 { /* 480p/576p / 25.2MHz/27MHz modes */
336 .pclk = 27000000,
337 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
338 SOR_PLL_VCOCAP(0) | SOR_PLL_RESISTORSEL,
339 .pll1 = SOR_PLL_LOADADJ(3) | SOR_PLL_TMDS_TERMADJ(0),
340 .pe_current = PE_CURRENT0(PE_CURRENT_0_mA_T114) |
341 PE_CURRENT1(PE_CURRENT_0_mA_T114) |
342 PE_CURRENT2(PE_CURRENT_0_mA_T114) |
343 PE_CURRENT3(PE_CURRENT_0_mA_T114),
344 .drive_current =
345 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_10_400_mA_T114) |
346 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_10_400_mA_T114) |
347 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_10_400_mA_T114) |
348 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_10_400_mA_T114),
349 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_0_000_mA) |
350 PEAK_CURRENT_LANE1(PEAK_CURRENT_0_000_mA) |
351 PEAK_CURRENT_LANE2(PEAK_CURRENT_0_000_mA) |
352 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_000_mA),
353 }, { /* 720p / 74.25MHz modes */
354 .pclk = 74250000,
355 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
356 SOR_PLL_VCOCAP(1) | SOR_PLL_RESISTORSEL,
357 .pll1 = SOR_PLL_PE_EN | SOR_PLL_LOADADJ(3) |
358 SOR_PLL_TMDS_TERMADJ(0),
359 .pe_current = PE_CURRENT0(PE_CURRENT_15_mA_T114) |
360 PE_CURRENT1(PE_CURRENT_15_mA_T114) |
361 PE_CURRENT2(PE_CURRENT_15_mA_T114) |
362 PE_CURRENT3(PE_CURRENT_15_mA_T114),
363 .drive_current =
364 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_10_400_mA_T114) |
365 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_10_400_mA_T114) |
366 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_10_400_mA_T114) |
367 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_10_400_mA_T114),
368 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_0_000_mA) |
369 PEAK_CURRENT_LANE1(PEAK_CURRENT_0_000_mA) |
370 PEAK_CURRENT_LANE2(PEAK_CURRENT_0_000_mA) |
371 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_000_mA),
372 }, { /* 1080p / 148.5MHz modes */
373 .pclk = 148500000,
374 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
375 SOR_PLL_VCOCAP(3) | SOR_PLL_RESISTORSEL,
376 .pll1 = SOR_PLL_PE_EN | SOR_PLL_LOADADJ(3) |
377 SOR_PLL_TMDS_TERMADJ(0),
378 .pe_current = PE_CURRENT0(PE_CURRENT_10_mA_T114) |
379 PE_CURRENT1(PE_CURRENT_10_mA_T114) |
380 PE_CURRENT2(PE_CURRENT_10_mA_T114) |
381 PE_CURRENT3(PE_CURRENT_10_mA_T114),
382 .drive_current =
383 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_12_400_mA_T114) |
384 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_12_400_mA_T114) |
385 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_12_400_mA_T114) |
386 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_12_400_mA_T114),
387 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_0_000_mA) |
388 PEAK_CURRENT_LANE1(PEAK_CURRENT_0_000_mA) |
389 PEAK_CURRENT_LANE2(PEAK_CURRENT_0_000_mA) |
390 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_000_mA),
391 }, { /* 225/297MHz modes */
392 .pclk = UINT_MAX,
393 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
394 SOR_PLL_VCOCAP(0xf) | SOR_PLL_RESISTORSEL,
395 .pll1 = SOR_PLL_LOADADJ(3) | SOR_PLL_TMDS_TERMADJ(7)
396 | SOR_PLL_TMDS_TERM_ENABLE,
397 .pe_current = PE_CURRENT0(PE_CURRENT_0_mA_T114) |
398 PE_CURRENT1(PE_CURRENT_0_mA_T114) |
399 PE_CURRENT2(PE_CURRENT_0_mA_T114) |
400 PE_CURRENT3(PE_CURRENT_0_mA_T114),
401 .drive_current =
402 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_25_200_mA_T114) |
403 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_25_200_mA_T114) |
404 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_25_200_mA_T114) |
405 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_19_200_mA_T114),
406 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_3_000_mA) |
407 PEAK_CURRENT_LANE1(PEAK_CURRENT_3_000_mA) |
408 PEAK_CURRENT_LANE2(PEAK_CURRENT_3_000_mA) |
409 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_800_mA),
410 },
411};
412
Thierry Redingedec4af2012-11-15 21:28:23 +0000413static const struct tegra_hdmi_audio_config *
Thierry Reding2ccb3962015-01-15 13:43:18 +0100414tegra_hdmi_get_audio_config(unsigned int sample_rate, unsigned int pclk)
Thierry Redingedec4af2012-11-15 21:28:23 +0000415{
416 const struct tegra_hdmi_audio_config *table;
417
Thierry Reding2ccb3962015-01-15 13:43:18 +0100418 switch (sample_rate) {
Thierry Redingedec4af2012-11-15 21:28:23 +0000419 case 32000:
420 table = tegra_hdmi_audio_32k;
421 break;
422
423 case 44100:
424 table = tegra_hdmi_audio_44_1k;
425 break;
426
427 case 48000:
428 table = tegra_hdmi_audio_48k;
429 break;
430
431 case 88200:
432 table = tegra_hdmi_audio_88_2k;
433 break;
434
435 case 96000:
436 table = tegra_hdmi_audio_96k;
437 break;
438
439 case 176400:
440 table = tegra_hdmi_audio_176_4k;
441 break;
442
443 case 192000:
444 table = tegra_hdmi_audio_192k;
445 break;
446
447 default:
448 return NULL;
449 }
450
451 while (table->pclk) {
452 if (table->pclk == pclk)
453 return table;
454
455 table++;
456 }
457
458 return NULL;
459}
460
461static void tegra_hdmi_setup_audio_fs_tables(struct tegra_hdmi *hdmi)
462{
463 const unsigned int freqs[] = {
464 32000, 44100, 48000, 88200, 96000, 176400, 192000
465 };
466 unsigned int i;
467
468 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
469 unsigned int f = freqs[i];
470 unsigned int eight_half;
Thierry Redingedec4af2012-11-15 21:28:23 +0000471 unsigned int delta;
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100472 u32 value;
Thierry Redingedec4af2012-11-15 21:28:23 +0000473
474 if (f > 96000)
475 delta = 2;
Thierry Reding17a8b6b2013-12-16 10:01:24 +0100476 else if (f > 48000)
Thierry Redingedec4af2012-11-15 21:28:23 +0000477 delta = 6;
478 else
479 delta = 9;
480
481 eight_half = (8 * HDMI_AUDIOCLK_FREQ) / (f * 128);
482 value = AUDIO_FS_LOW(eight_half - delta) |
483 AUDIO_FS_HIGH(eight_half + delta);
484 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_AUDIO_FS(i));
485 }
486}
487
Thierry Reding2ccb3962015-01-15 13:43:18 +0100488static void tegra_hdmi_write_aval(struct tegra_hdmi *hdmi, u32 value)
Thierry Redingedec4af2012-11-15 21:28:23 +0000489{
Thierry Reding2ccb3962015-01-15 13:43:18 +0100490 static const struct {
491 unsigned int sample_rate;
492 unsigned int offset;
493 } regs[] = {
494 { 32000, HDMI_NV_PDISP_SOR_AUDIO_AVAL_0320 },
495 { 44100, HDMI_NV_PDISP_SOR_AUDIO_AVAL_0441 },
496 { 48000, HDMI_NV_PDISP_SOR_AUDIO_AVAL_0480 },
497 { 88200, HDMI_NV_PDISP_SOR_AUDIO_AVAL_0882 },
498 { 96000, HDMI_NV_PDISP_SOR_AUDIO_AVAL_0960 },
499 { 176400, HDMI_NV_PDISP_SOR_AUDIO_AVAL_1764 },
500 { 192000, HDMI_NV_PDISP_SOR_AUDIO_AVAL_1920 },
501 };
502 unsigned int i;
503
504 for (i = 0; i < ARRAY_SIZE(regs); i++) {
505 if (regs[i].sample_rate == hdmi->audio_sample_rate) {
506 tegra_hdmi_writel(hdmi, value, regs[i].offset);
507 break;
508 }
509 }
510}
511
512static int tegra_hdmi_setup_audio(struct tegra_hdmi *hdmi)
513{
Thierry Redingedec4af2012-11-15 21:28:23 +0000514 const struct tegra_hdmi_audio_config *config;
Thierry Reding2ccb3962015-01-15 13:43:18 +0100515 u32 source, value;
Thierry Redingedec4af2012-11-15 21:28:23 +0000516
517 switch (hdmi->audio_source) {
518 case HDA:
Thierry Reding2ccb3962015-01-15 13:43:18 +0100519 if (hdmi->config->has_hda)
520 source = SOR_AUDIO_CNTRL0_SOURCE_SELECT_HDAL;
521 else
522 return -EINVAL;
523
Thierry Redingedec4af2012-11-15 21:28:23 +0000524 break;
525
526 case SPDIF:
Thierry Reding2ccb3962015-01-15 13:43:18 +0100527 if (hdmi->config->has_hda)
528 source = SOR_AUDIO_CNTRL0_SOURCE_SELECT_SPDIF;
529 else
530 source = AUDIO_CNTRL0_SOURCE_SELECT_SPDIF;
Thierry Redingedec4af2012-11-15 21:28:23 +0000531 break;
532
533 default:
Thierry Reding2ccb3962015-01-15 13:43:18 +0100534 if (hdmi->config->has_hda)
535 source = SOR_AUDIO_CNTRL0_SOURCE_SELECT_AUTO;
536 else
537 source = AUDIO_CNTRL0_SOURCE_SELECT_AUTO;
Thierry Redingedec4af2012-11-15 21:28:23 +0000538 break;
539 }
540
Thierry Reding2ccb3962015-01-15 13:43:18 +0100541 /*
542 * Tegra30 and later use a slightly modified version of the register
543 * layout to accomodate for changes related to supporting HDA as the
544 * audio input source for HDMI. The source select field has moved to
545 * the SOR_AUDIO_CNTRL0 register, but the error tolerance and frames
546 * per block fields remain in the AUDIO_CNTRL0 register.
547 */
548 if (hdmi->config->has_hda) {
549 /*
550 * Inject null samples into the audio FIFO for every frame in
551 * which the codec did not receive any samples. This applies
552 * to stereo LPCM only.
553 *
554 * XXX: This seems to be a remnant of MCP days when this was
555 * used to work around issues with monitors not being able to
556 * play back system startup sounds early. It is possibly not
557 * needed on Linux at all.
558 */
559 if (hdmi->audio_channels == 2)
560 value = SOR_AUDIO_CNTRL0_INJECT_NULLSMPL;
561 else
562 value = 0;
Thierry Redingedec4af2012-11-15 21:28:23 +0000563
Thierry Reding2ccb3962015-01-15 13:43:18 +0100564 value |= source;
565
566 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_AUDIO_CNTRL0);
Thierry Redingedec4af2012-11-15 21:28:23 +0000567 }
568
Thierry Reding2ccb3962015-01-15 13:43:18 +0100569 /*
570 * On Tegra20, HDA is not a supported audio source and the source
571 * select field is part of the AUDIO_CNTRL0 register.
572 */
573 value = AUDIO_CNTRL0_FRAMES_PER_BLOCK(0xc0) |
574 AUDIO_CNTRL0_ERROR_TOLERANCE(6);
575
576 if (!hdmi->config->has_hda)
577 value |= source;
578
579 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_AUDIO_CNTRL0);
580
581 /*
582 * Advertise support for High Bit-Rate on Tegra114 and later.
583 */
584 if (hdmi->config->has_hbr) {
585 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_SOR_AUDIO_SPARE0);
586 value |= SOR_AUDIO_SPARE0_HBR_ENABLE;
587 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_AUDIO_SPARE0);
588 }
589
590 config = tegra_hdmi_get_audio_config(hdmi->audio_sample_rate,
591 hdmi->pixel_clock);
Thierry Redingedec4af2012-11-15 21:28:23 +0000592 if (!config) {
Thierry Reding2ccb3962015-01-15 13:43:18 +0100593 dev_err(hdmi->dev,
594 "cannot set audio to %u Hz at %u Hz pixel clock\n",
595 hdmi->audio_sample_rate, hdmi->pixel_clock);
Thierry Redingedec4af2012-11-15 21:28:23 +0000596 return -EINVAL;
597 }
598
599 tegra_hdmi_writel(hdmi, 0, HDMI_NV_PDISP_HDMI_ACR_CTRL);
600
601 value = AUDIO_N_RESETF | AUDIO_N_GENERATE_ALTERNATE |
602 AUDIO_N_VALUE(config->n - 1);
603 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_AUDIO_N);
604
605 tegra_hdmi_writel(hdmi, ACR_SUBPACK_N(config->n) | ACR_ENABLE,
606 HDMI_NV_PDISP_HDMI_ACR_0441_SUBPACK_HIGH);
607
Thierry Reding2ccb3962015-01-15 13:43:18 +0100608 tegra_hdmi_writel(hdmi, ACR_SUBPACK_CTS(config->cts),
609 HDMI_NV_PDISP_HDMI_ACR_0441_SUBPACK_LOW);
Thierry Redingedec4af2012-11-15 21:28:23 +0000610
611 value = SPARE_HW_CTS | SPARE_FORCE_SW_CTS | SPARE_CTS_RESET_VAL(1);
612 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_SPARE);
613
614 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_AUDIO_N);
615 value &= ~AUDIO_N_RESETF;
616 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_AUDIO_N);
617
Thierry Reding2ccb3962015-01-15 13:43:18 +0100618 if (hdmi->config->has_hda)
619 tegra_hdmi_write_aval(hdmi, config->aval);
Thierry Redingedec4af2012-11-15 21:28:23 +0000620
621 tegra_hdmi_setup_audio_fs_tables(hdmi);
622
623 return 0;
624}
625
Thierry Reding2ccb3962015-01-15 13:43:18 +0100626static void tegra_hdmi_disable_audio(struct tegra_hdmi *hdmi)
627{
628 u32 value;
629
630 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
631 value &= ~GENERIC_CTRL_AUDIO;
632 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
633}
634
635static void tegra_hdmi_enable_audio(struct tegra_hdmi *hdmi)
636{
637 u32 value;
638
639 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
640 value |= GENERIC_CTRL_AUDIO;
641 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
642}
643
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100644static inline u32 tegra_hdmi_subpack(const u8 *ptr, size_t size)
Thierry Redingedec4af2012-11-15 21:28:23 +0000645{
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100646 u32 value = 0;
Thierry Redingedec4af2012-11-15 21:28:23 +0000647 size_t i;
Thierry Redingedec4af2012-11-15 21:28:23 +0000648
Thierry Redingac24c222012-11-23 15:14:00 +0100649 for (i = size; i > 0; i--)
650 value = (value << 8) | ptr[i - 1];
Thierry Redingedec4af2012-11-15 21:28:23 +0000651
Thierry Redingac24c222012-11-23 15:14:00 +0100652 return value;
653}
Thierry Redingedec4af2012-11-15 21:28:23 +0000654
Thierry Redingac24c222012-11-23 15:14:00 +0100655static void tegra_hdmi_write_infopack(struct tegra_hdmi *hdmi, const void *data,
656 size_t size)
657{
658 const u8 *ptr = data;
659 unsigned long offset;
Thierry Redingac24c222012-11-23 15:14:00 +0100660 size_t i, j;
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100661 u32 value;
Thierry Redingedec4af2012-11-15 21:28:23 +0000662
Thierry Redingac24c222012-11-23 15:14:00 +0100663 switch (ptr[0]) {
664 case HDMI_INFOFRAME_TYPE_AVI:
665 offset = HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_HEADER;
666 break;
667
668 case HDMI_INFOFRAME_TYPE_AUDIO:
669 offset = HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_HEADER;
670 break;
671
672 case HDMI_INFOFRAME_TYPE_VENDOR:
673 offset = HDMI_NV_PDISP_HDMI_GENERIC_HEADER;
674 break;
675
676 default:
677 dev_err(hdmi->dev, "unsupported infoframe type: %02x\n",
678 ptr[0]);
679 return;
680 }
681
682 value = INFOFRAME_HEADER_TYPE(ptr[0]) |
683 INFOFRAME_HEADER_VERSION(ptr[1]) |
684 INFOFRAME_HEADER_LEN(ptr[2]);
Thierry Redingedec4af2012-11-15 21:28:23 +0000685 tegra_hdmi_writel(hdmi, value, offset);
Thierry Redingac24c222012-11-23 15:14:00 +0100686 offset++;
Thierry Redingedec4af2012-11-15 21:28:23 +0000687
Thierry Redingac24c222012-11-23 15:14:00 +0100688 /*
689 * Each subpack contains 7 bytes, divided into:
690 * - subpack_low: bytes 0 - 3
691 * - subpack_high: bytes 4 - 6 (with byte 7 padded to 0x00)
Thierry Redingedec4af2012-11-15 21:28:23 +0000692 */
Thierry Redingac24c222012-11-23 15:14:00 +0100693 for (i = 3, j = 0; i < size; i += 7, j += 8) {
694 size_t rem = size - i, num = min_t(size_t, rem, 4);
Thierry Redingedec4af2012-11-15 21:28:23 +0000695
Thierry Redingac24c222012-11-23 15:14:00 +0100696 value = tegra_hdmi_subpack(&ptr[i], num);
697 tegra_hdmi_writel(hdmi, value, offset++);
Thierry Redingedec4af2012-11-15 21:28:23 +0000698
Thierry Redingac24c222012-11-23 15:14:00 +0100699 num = min_t(size_t, rem - num, 3);
Thierry Redingedec4af2012-11-15 21:28:23 +0000700
Thierry Redingac24c222012-11-23 15:14:00 +0100701 value = tegra_hdmi_subpack(&ptr[i + 4], num);
702 tegra_hdmi_writel(hdmi, value, offset++);
Thierry Redingedec4af2012-11-15 21:28:23 +0000703 }
704}
705
706static void tegra_hdmi_setup_avi_infoframe(struct tegra_hdmi *hdmi,
707 struct drm_display_mode *mode)
708{
709 struct hdmi_avi_infoframe frame;
Thierry Redingac24c222012-11-23 15:14:00 +0100710 u8 buffer[17];
711 ssize_t err;
Thierry Redingedec4af2012-11-15 21:28:23 +0000712
Thierry Redingac24c222012-11-23 15:14:00 +0100713 err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
714 if (err < 0) {
715 dev_err(hdmi->dev, "failed to setup AVI infoframe: %zd\n", err);
716 return;
Thierry Redingedec4af2012-11-15 21:28:23 +0000717 }
718
Thierry Redingac24c222012-11-23 15:14:00 +0100719 err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
720 if (err < 0) {
721 dev_err(hdmi->dev, "failed to pack AVI infoframe: %zd\n", err);
722 return;
723 }
724
725 tegra_hdmi_write_infopack(hdmi, buffer, err);
Thierry Reding2ccb3962015-01-15 13:43:18 +0100726}
Thierry Redingedec4af2012-11-15 21:28:23 +0000727
Thierry Reding2ccb3962015-01-15 13:43:18 +0100728static void tegra_hdmi_disable_avi_infoframe(struct tegra_hdmi *hdmi)
729{
730 u32 value;
731
732 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_CTRL);
733 value &= ~INFOFRAME_CTRL_ENABLE;
734 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_CTRL);
735}
736
737static void tegra_hdmi_enable_avi_infoframe(struct tegra_hdmi *hdmi)
738{
739 u32 value;
740
741 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_CTRL);
742 value |= INFOFRAME_CTRL_ENABLE;
743 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_CTRL);
Thierry Redingedec4af2012-11-15 21:28:23 +0000744}
745
746static void tegra_hdmi_setup_audio_infoframe(struct tegra_hdmi *hdmi)
747{
748 struct hdmi_audio_infoframe frame;
Thierry Redingac24c222012-11-23 15:14:00 +0100749 u8 buffer[14];
750 ssize_t err;
Thierry Redingedec4af2012-11-15 21:28:23 +0000751
Thierry Redingac24c222012-11-23 15:14:00 +0100752 err = hdmi_audio_infoframe_init(&frame);
753 if (err < 0) {
Thierry Redingef284c72013-10-16 19:51:22 +0200754 dev_err(hdmi->dev, "failed to setup audio infoframe: %zd\n",
Thierry Redingac24c222012-11-23 15:14:00 +0100755 err);
756 return;
757 }
Thierry Redingedec4af2012-11-15 21:28:23 +0000758
Thierry Reding2ccb3962015-01-15 13:43:18 +0100759 frame.channels = hdmi->audio_channels;
Thierry Redingac24c222012-11-23 15:14:00 +0100760
761 err = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
762 if (err < 0) {
763 dev_err(hdmi->dev, "failed to pack audio infoframe: %zd\n",
764 err);
765 return;
766 }
767
768 /*
769 * The audio infoframe has only one set of subpack registers, so the
770 * infoframe needs to be truncated. One set of subpack registers can
771 * contain 7 bytes. Including the 3 byte header only the first 10
772 * bytes can be programmed.
773 */
Thierry Redingef284c72013-10-16 19:51:22 +0200774 tegra_hdmi_write_infopack(hdmi, buffer, min_t(size_t, 10, err));
Thierry Reding2ccb3962015-01-15 13:43:18 +0100775}
Thierry Redingedec4af2012-11-15 21:28:23 +0000776
Thierry Reding2ccb3962015-01-15 13:43:18 +0100777static void tegra_hdmi_disable_audio_infoframe(struct tegra_hdmi *hdmi)
778{
779 u32 value;
780
781 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_CTRL);
782 value &= ~INFOFRAME_CTRL_ENABLE;
783 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_CTRL);
784}
785
786static void tegra_hdmi_enable_audio_infoframe(struct tegra_hdmi *hdmi)
787{
788 u32 value;
789
790 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_CTRL);
791 value |= INFOFRAME_CTRL_ENABLE;
792 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_CTRL);
Thierry Redingedec4af2012-11-15 21:28:23 +0000793}
794
795static void tegra_hdmi_setup_stereo_infoframe(struct tegra_hdmi *hdmi)
796{
Lespiau, Damienae84b902013-08-19 16:59:02 +0100797 struct hdmi_vendor_infoframe frame;
Thierry Redingac24c222012-11-23 15:14:00 +0100798 u8 buffer[10];
799 ssize_t err;
Thierry Redingedec4af2012-11-15 21:28:23 +0000800
Lespiau, Damienae84b902013-08-19 16:59:02 +0100801 hdmi_vendor_infoframe_init(&frame);
Lespiau, Damiena26a58e82013-08-19 16:58:59 +0100802 frame.s3d_struct = HDMI_3D_STRUCTURE_FRAME_PACKING;
Thierry Redingac24c222012-11-23 15:14:00 +0100803
Lespiau, Damienae84b902013-08-19 16:59:02 +0100804 err = hdmi_vendor_infoframe_pack(&frame, buffer, sizeof(buffer));
Thierry Redingac24c222012-11-23 15:14:00 +0100805 if (err < 0) {
806 dev_err(hdmi->dev, "failed to pack vendor infoframe: %zd\n",
807 err);
808 return;
809 }
810
811 tegra_hdmi_write_infopack(hdmi, buffer, err);
Thierry Reding2ccb3962015-01-15 13:43:18 +0100812}
813
814static void tegra_hdmi_disable_stereo_infoframe(struct tegra_hdmi *hdmi)
815{
816 u32 value;
817
818 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
819 value &= ~GENERIC_CTRL_ENABLE;
820 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
821}
822
823static void tegra_hdmi_enable_stereo_infoframe(struct tegra_hdmi *hdmi)
824{
825 u32 value;
Thierry Redingedec4af2012-11-15 21:28:23 +0000826
827 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
828 value |= GENERIC_CTRL_ENABLE;
829 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
830}
831
832static void tegra_hdmi_setup_tmds(struct tegra_hdmi *hdmi,
833 const struct tmds_config *tmds)
834{
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100835 u32 value;
Thierry Redingedec4af2012-11-15 21:28:23 +0000836
837 tegra_hdmi_writel(hdmi, tmds->pll0, HDMI_NV_PDISP_SOR_PLL0);
838 tegra_hdmi_writel(hdmi, tmds->pll1, HDMI_NV_PDISP_SOR_PLL1);
839 tegra_hdmi_writel(hdmi, tmds->pe_current, HDMI_NV_PDISP_PE_CURRENT);
840
Thierry Reding59af0592013-10-14 09:43:05 +0200841 tegra_hdmi_writel(hdmi, tmds->drive_current,
842 HDMI_NV_PDISP_SOR_LANE_DRIVE_CURRENT);
843
844 value = tegra_hdmi_readl(hdmi, hdmi->config->fuse_override_offset);
845 value |= hdmi->config->fuse_override_value;
846 tegra_hdmi_writel(hdmi, value, hdmi->config->fuse_override_offset);
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +0200847
848 if (hdmi->config->has_sor_io_peak_current)
849 tegra_hdmi_writel(hdmi, tmds->peak_current,
850 HDMI_NV_PDISP_SOR_IO_PEAK_CURRENT);
Thierry Redingedec4af2012-11-15 21:28:23 +0000851}
852
Mikko Perttunen9f159122013-08-28 18:48:38 +0300853static bool tegra_output_is_hdmi(struct tegra_output *output)
854{
855 struct edid *edid;
856
857 if (!output->connector.edid_blob_ptr)
858 return false;
859
860 edid = (struct edid *)output->connector.edid_blob_ptr->data;
861
862 return drm_detect_hdmi_monitor(edid);
863}
864
Thierry Reding2ccb3962015-01-15 13:43:18 +0100865static enum drm_connector_status
866tegra_hdmi_connector_detect(struct drm_connector *connector, bool force)
867{
868 struct tegra_output *output = connector_to_output(connector);
869 struct tegra_hdmi *hdmi = to_hdmi(output);
870 enum drm_connector_status status;
871
872 status = tegra_output_connector_detect(connector, force);
873 if (status == connector_status_connected)
874 return status;
875
876 tegra_hdmi_writel(hdmi, 0, HDMI_NV_PDISP_SOR_AUDIO_HDA_PRESENSE);
877 return status;
878}
879
Thierry Reding59682712014-11-28 16:50:59 +0100880static const struct drm_connector_funcs tegra_hdmi_connector_funcs = {
Thierry Reding29871b22015-07-29 09:46:40 +0200881 .dpms = drm_atomic_helper_connector_dpms,
Thierry Reding9d441892014-11-24 17:02:53 +0100882 .reset = drm_atomic_helper_connector_reset,
Thierry Reding2ccb3962015-01-15 13:43:18 +0100883 .detect = tegra_hdmi_connector_detect,
Thierry Reding59682712014-11-28 16:50:59 +0100884 .fill_modes = drm_helper_probe_single_connector_modes,
885 .destroy = tegra_output_connector_destroy,
Thierry Reding9d441892014-11-24 17:02:53 +0100886 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
Thierry Reding4aa3df72014-11-24 16:27:13 +0100887 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Thierry Reding59682712014-11-28 16:50:59 +0100888};
889
890static enum drm_mode_status
891tegra_hdmi_connector_mode_valid(struct drm_connector *connector,
892 struct drm_display_mode *mode)
893{
894 struct tegra_output *output = connector_to_output(connector);
895 struct tegra_hdmi *hdmi = to_hdmi(output);
896 unsigned long pclk = mode->clock * 1000;
897 enum drm_mode_status status = MODE_OK;
898 struct clk *parent;
899 long err;
900
901 parent = clk_get_parent(hdmi->clk_parent);
902
903 err = clk_round_rate(parent, pclk * 4);
904 if (err <= 0)
905 status = MODE_NOCLOCK;
906
907 return status;
908}
909
910static const struct drm_connector_helper_funcs
911tegra_hdmi_connector_helper_funcs = {
912 .get_modes = tegra_output_connector_get_modes,
913 .mode_valid = tegra_hdmi_connector_mode_valid,
914 .best_encoder = tegra_output_connector_best_encoder,
915};
916
917static const struct drm_encoder_funcs tegra_hdmi_encoder_funcs = {
918 .destroy = tegra_output_encoder_destroy,
919};
920
Thierry Reding29871b22015-07-29 09:46:40 +0200921static void tegra_hdmi_encoder_disable(struct drm_encoder *encoder)
Thierry Reding59682712014-11-28 16:50:59 +0100922{
Thierry Reding2ccb3962015-01-15 13:43:18 +0100923 struct tegra_output *output = encoder_to_output(encoder);
Thierry Reding29871b22015-07-29 09:46:40 +0200924 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
Thierry Reding2ccb3962015-01-15 13:43:18 +0100925 struct tegra_hdmi *hdmi = to_hdmi(output);
Thierry Reding29871b22015-07-29 09:46:40 +0200926 u32 value;
927
928 /*
929 * The following accesses registers of the display controller, so make
930 * sure it's only executed when the output is attached to one.
931 */
932 if (dc) {
933 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
934 value &= ~HDMI_ENABLE;
935 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
936
937 tegra_dc_commit(dc);
938 }
Thierry Reding2ccb3962015-01-15 13:43:18 +0100939
940 if (!hdmi->dvi) {
941 if (hdmi->stereo)
942 tegra_hdmi_disable_stereo_infoframe(hdmi);
943
944 tegra_hdmi_disable_audio_infoframe(hdmi);
945 tegra_hdmi_disable_avi_infoframe(hdmi);
946 tegra_hdmi_disable_audio(hdmi);
947 }
948}
949
950static void tegra_hdmi_write_eld(struct tegra_hdmi *hdmi)
951{
952 size_t length = drm_eld_size(hdmi->output.connector.eld), i;
953 u32 value;
954
955 for (i = 0; i < length; i++)
956 tegra_hdmi_writel(hdmi, i << 8 | hdmi->output.connector.eld[i],
957 HDMI_NV_PDISP_SOR_AUDIO_HDA_ELD_BUFWR);
958
959 /*
960 * The HDA codec will always report an ELD buffer size of 96 bytes and
961 * the HDA codec driver will check that each byte read from the buffer
962 * is valid. Therefore every byte must be written, even if no 96 bytes
963 * were parsed from EDID.
964 */
965 for (i = length; i < HDMI_ELD_BUFFER_SIZE; i++)
966 tegra_hdmi_writel(hdmi, i << 8 | 0,
967 HDMI_NV_PDISP_SOR_AUDIO_HDA_ELD_BUFWR);
968
969 value = SOR_AUDIO_HDA_PRESENSE_VALID | SOR_AUDIO_HDA_PRESENSE_PRESENT;
970 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_AUDIO_HDA_PRESENSE);
Thierry Reding59682712014-11-28 16:50:59 +0100971}
972
Thierry Reding29871b22015-07-29 09:46:40 +0200973static void tegra_hdmi_encoder_enable(struct drm_encoder *encoder)
Thierry Reding59682712014-11-28 16:50:59 +0100974{
Thierry Reding29871b22015-07-29 09:46:40 +0200975 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
Thierry Redingedec4af2012-11-15 21:28:23 +0000976 unsigned int h_sync_width, h_front_porch, h_back_porch, i, rekey;
Thierry Reding59682712014-11-28 16:50:59 +0100977 struct tegra_output *output = encoder_to_output(encoder);
978 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
Thierry Redingedec4af2012-11-15 21:28:23 +0000979 struct tegra_hdmi *hdmi = to_hdmi(output);
Thierry Reding2ccb3962015-01-15 13:43:18 +0100980 unsigned int pulse_start, div82;
Thierry Redingedec4af2012-11-15 21:28:23 +0000981 int retries = 1000;
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100982 u32 value;
Thierry Redingedec4af2012-11-15 21:28:23 +0000983 int err;
984
Thierry Reding2ccb3962015-01-15 13:43:18 +0100985 hdmi->pixel_clock = mode->clock * 1000;
Thierry Redingedec4af2012-11-15 21:28:23 +0000986 h_sync_width = mode->hsync_end - mode->hsync_start;
Lucas Stach40495082012-12-19 21:38:52 +0000987 h_back_porch = mode->htotal - mode->hsync_end;
988 h_front_porch = mode->hsync_start - mode->hdisplay;
Thierry Redingedec4af2012-11-15 21:28:23 +0000989
Thierry Reding2ccb3962015-01-15 13:43:18 +0100990 err = clk_set_rate(hdmi->clk, hdmi->pixel_clock);
Thierry Redingc03bf1b2015-02-18 10:34:08 +0100991 if (err < 0) {
992 dev_err(hdmi->dev, "failed to set HDMI clock frequency: %d\n",
993 err);
994 }
995
996 DRM_DEBUG_KMS("HDMI clock rate: %lu Hz\n", clk_get_rate(hdmi->clk));
997
Thierry Reding8c8282c2014-04-16 10:46:24 +0200998 /* power up sequence */
999 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_SOR_PLL0);
1000 value &= ~SOR_PLL_PDBG;
1001 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_PLL0);
1002
1003 usleep_range(10, 20);
1004
1005 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_SOR_PLL0);
1006 value &= ~SOR_PLL_PWR;
1007 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_PLL0);
1008
Thierry Redingedec4af2012-11-15 21:28:23 +00001009 tegra_dc_writel(dc, VSYNC_H_POSITION(1),
1010 DC_DISP_DISP_TIMING_OPTIONS);
Thierry Reding472a6d12015-08-05 16:39:55 +02001011 tegra_dc_writel(dc, DITHER_CONTROL_DISABLE | BASE_COLOR_SIZE_888,
Thierry Redingedec4af2012-11-15 21:28:23 +00001012 DC_DISP_DISP_COLOR_CONTROL);
1013
1014 /* video_preamble uses h_pulse2 */
1015 pulse_start = 1 + h_sync_width + h_back_porch - 10;
1016
Thierry Reding8fd3ffa2015-04-27 14:48:35 +02001017 tegra_dc_writel(dc, H_PULSE2_ENABLE, DC_DISP_DISP_SIGNAL_OPTIONS0);
Thierry Redingedec4af2012-11-15 21:28:23 +00001018
1019 value = PULSE_MODE_NORMAL | PULSE_POLARITY_HIGH | PULSE_QUAL_VACTIVE |
1020 PULSE_LAST_END_A;
1021 tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_CONTROL);
1022
1023 value = PULSE_START(pulse_start) | PULSE_END(pulse_start + 8);
1024 tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_POSITION_A);
1025
1026 value = VSYNC_WINDOW_END(0x210) | VSYNC_WINDOW_START(0x200) |
1027 VSYNC_WINDOW_ENABLE;
1028 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_VSYNC_WINDOW);
1029
1030 if (dc->pipe)
1031 value = HDMI_SRC_DISPLAYB;
1032 else
1033 value = HDMI_SRC_DISPLAYA;
1034
1035 if ((mode->hdisplay == 720) && ((mode->vdisplay == 480) ||
1036 (mode->vdisplay == 576)))
1037 tegra_hdmi_writel(hdmi,
1038 value | ARM_VIDEO_RANGE_FULL,
1039 HDMI_NV_PDISP_INPUT_CONTROL);
1040 else
1041 tegra_hdmi_writel(hdmi,
1042 value | ARM_VIDEO_RANGE_LIMITED,
1043 HDMI_NV_PDISP_INPUT_CONTROL);
1044
1045 div82 = clk_get_rate(hdmi->clk) / 1000000 * 4;
1046 value = SOR_REFCLK_DIV_INT(div82 >> 2) | SOR_REFCLK_DIV_FRAC(div82);
1047 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_REFCLK);
1048
Thierry Reding2ccb3962015-01-15 13:43:18 +01001049 hdmi->dvi = !tegra_output_is_hdmi(output);
Thierry Redingedec4af2012-11-15 21:28:23 +00001050 if (!hdmi->dvi) {
Thierry Reding2ccb3962015-01-15 13:43:18 +01001051 err = tegra_hdmi_setup_audio(hdmi);
Thierry Redingedec4af2012-11-15 21:28:23 +00001052 if (err < 0)
1053 hdmi->dvi = true;
1054 }
1055
Thierry Reding2ccb3962015-01-15 13:43:18 +01001056 if (hdmi->config->has_hda)
1057 tegra_hdmi_write_eld(hdmi);
Thierry Redingedec4af2012-11-15 21:28:23 +00001058
1059 rekey = HDMI_REKEY_DEFAULT;
1060 value = HDMI_CTRL_REKEY(rekey);
1061 value |= HDMI_CTRL_MAX_AC_PACKET((h_sync_width + h_back_porch +
1062 h_front_porch - rekey - 18) / 32);
1063
1064 if (!hdmi->dvi)
1065 value |= HDMI_CTRL_ENABLE;
1066
1067 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_CTRL);
1068
Thierry Reding2ccb3962015-01-15 13:43:18 +01001069 if (!hdmi->dvi) {
1070 tegra_hdmi_setup_avi_infoframe(hdmi, mode);
1071 tegra_hdmi_setup_audio_infoframe(hdmi);
Thierry Redingedec4af2012-11-15 21:28:23 +00001072
Thierry Reding2ccb3962015-01-15 13:43:18 +01001073 if (hdmi->stereo)
1074 tegra_hdmi_setup_stereo_infoframe(hdmi);
1075 }
Thierry Redingedec4af2012-11-15 21:28:23 +00001076
1077 /* TMDS CONFIG */
Thierry Reding59af0592013-10-14 09:43:05 +02001078 for (i = 0; i < hdmi->config->num_tmds; i++) {
Thierry Reding2ccb3962015-01-15 13:43:18 +01001079 if (hdmi->pixel_clock <= hdmi->config->tmds[i].pclk) {
Thierry Reding59af0592013-10-14 09:43:05 +02001080 tegra_hdmi_setup_tmds(hdmi, &hdmi->config->tmds[i]);
Thierry Redingedec4af2012-11-15 21:28:23 +00001081 break;
1082 }
1083 }
1084
1085 tegra_hdmi_writel(hdmi,
Thierry Reding5c1c0712015-01-28 16:32:52 +01001086 SOR_SEQ_PU_PC(0) |
Thierry Redingedec4af2012-11-15 21:28:23 +00001087 SOR_SEQ_PU_PC_ALT(0) |
1088 SOR_SEQ_PD_PC(8) |
1089 SOR_SEQ_PD_PC_ALT(8),
1090 HDMI_NV_PDISP_SOR_SEQ_CTL);
1091
1092 value = SOR_SEQ_INST_WAIT_TIME(1) |
1093 SOR_SEQ_INST_WAIT_UNITS_VSYNC |
1094 SOR_SEQ_INST_HALT |
1095 SOR_SEQ_INST_PIN_A_LOW |
1096 SOR_SEQ_INST_PIN_B_LOW |
1097 SOR_SEQ_INST_DRIVE_PWM_OUT_LO;
1098
1099 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_SEQ_INST(0));
1100 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_SEQ_INST(8));
1101
Thierry Reding9cbfc732014-04-16 10:47:36 +02001102 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_SOR_CSTM);
Thierry Redingedec4af2012-11-15 21:28:23 +00001103 value &= ~SOR_CSTM_ROTCLK(~0);
1104 value |= SOR_CSTM_ROTCLK(2);
Thierry Reding9cbfc732014-04-16 10:47:36 +02001105 value |= SOR_CSTM_PLLDIV;
1106 value &= ~SOR_CSTM_LVDS_ENABLE;
1107 value &= ~SOR_CSTM_MODE_MASK;
1108 value |= SOR_CSTM_MODE_TMDS;
Thierry Redingedec4af2012-11-15 21:28:23 +00001109 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_CSTM);
1110
Thierry Redingedec4af2012-11-15 21:28:23 +00001111 /* start SOR */
1112 tegra_hdmi_writel(hdmi,
1113 SOR_PWR_NORMAL_STATE_PU |
1114 SOR_PWR_NORMAL_START_NORMAL |
1115 SOR_PWR_SAFE_STATE_PD |
1116 SOR_PWR_SETTING_NEW_TRIGGER,
1117 HDMI_NV_PDISP_SOR_PWR);
1118 tegra_hdmi_writel(hdmi,
1119 SOR_PWR_NORMAL_STATE_PU |
1120 SOR_PWR_NORMAL_START_NORMAL |
1121 SOR_PWR_SAFE_STATE_PD |
1122 SOR_PWR_SETTING_NEW_DONE,
1123 HDMI_NV_PDISP_SOR_PWR);
1124
1125 do {
1126 BUG_ON(--retries < 0);
1127 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_SOR_PWR);
1128 } while (value & SOR_PWR_SETTING_NEW_PENDING);
1129
1130 value = SOR_STATE_ASY_CRCMODE_COMPLETE |
1131 SOR_STATE_ASY_OWNER_HEAD0 |
1132 SOR_STATE_ASY_SUBOWNER_BOTH |
1133 SOR_STATE_ASY_PROTOCOL_SINGLE_TMDS_A |
1134 SOR_STATE_ASY_DEPOL_POS;
1135
1136 /* setup sync polarities */
1137 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
1138 value |= SOR_STATE_ASY_HSYNCPOL_POS;
1139
1140 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1141 value |= SOR_STATE_ASY_HSYNCPOL_NEG;
1142
1143 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
1144 value |= SOR_STATE_ASY_VSYNCPOL_POS;
1145
1146 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1147 value |= SOR_STATE_ASY_VSYNCPOL_NEG;
1148
1149 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_STATE2);
1150
1151 value = SOR_STATE_ASY_HEAD_OPMODE_AWAKE | SOR_STATE_ASY_ORMODE_NORMAL;
1152 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_STATE1);
1153
1154 tegra_hdmi_writel(hdmi, 0, HDMI_NV_PDISP_SOR_STATE0);
1155 tegra_hdmi_writel(hdmi, SOR_STATE_UPDATE, HDMI_NV_PDISP_SOR_STATE0);
1156 tegra_hdmi_writel(hdmi, value | SOR_STATE_ATTACHED,
1157 HDMI_NV_PDISP_SOR_STATE1);
1158 tegra_hdmi_writel(hdmi, 0, HDMI_NV_PDISP_SOR_STATE0);
1159
Thierry Reding72d30282013-12-12 11:06:55 +01001160 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1161 value |= HDMI_ENABLE;
1162 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
Thierry Redingedec4af2012-11-15 21:28:23 +00001163
Thierry Reding62b9e062014-11-21 17:33:33 +01001164 tegra_dc_commit(dc);
Thierry Redingedec4af2012-11-15 21:28:23 +00001165
Thierry Reding2ccb3962015-01-15 13:43:18 +01001166 if (!hdmi->dvi) {
1167 tegra_hdmi_enable_avi_infoframe(hdmi);
1168 tegra_hdmi_enable_audio_infoframe(hdmi);
1169 tegra_hdmi_enable_audio(hdmi);
1170
1171 if (hdmi->stereo)
1172 tegra_hdmi_enable_stereo_infoframe(hdmi);
1173 }
1174
Thierry Redingedec4af2012-11-15 21:28:23 +00001175 /* TODO: add HDCP support */
Thierry Redingedec4af2012-11-15 21:28:23 +00001176}
1177
Thierry Redinga9825a62014-12-08 16:33:03 +01001178static int
1179tegra_hdmi_encoder_atomic_check(struct drm_encoder *encoder,
1180 struct drm_crtc_state *crtc_state,
1181 struct drm_connector_state *conn_state)
1182{
1183 struct tegra_output *output = encoder_to_output(encoder);
1184 struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
1185 unsigned long pclk = crtc_state->mode.clock * 1000;
1186 struct tegra_hdmi *hdmi = to_hdmi(output);
1187 int err;
1188
1189 err = tegra_dc_state_setup_clock(dc, crtc_state, hdmi->clk_parent,
1190 pclk, 0);
1191 if (err < 0) {
1192 dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
1193 return err;
1194 }
1195
1196 return err;
1197}
1198
Thierry Reding59682712014-11-28 16:50:59 +01001199static const struct drm_encoder_helper_funcs tegra_hdmi_encoder_helper_funcs = {
Thierry Reding59682712014-11-28 16:50:59 +01001200 .disable = tegra_hdmi_encoder_disable,
Thierry Reding29871b22015-07-29 09:46:40 +02001201 .enable = tegra_hdmi_encoder_enable,
Thierry Redinga9825a62014-12-08 16:33:03 +01001202 .atomic_check = tegra_hdmi_encoder_atomic_check,
Thierry Redingedec4af2012-11-15 21:28:23 +00001203};
1204
1205static int tegra_hdmi_show_regs(struct seq_file *s, void *data)
1206{
1207 struct drm_info_node *node = s->private;
1208 struct tegra_hdmi *hdmi = node->info_ent->data;
Thierry Reding29871b22015-07-29 09:46:40 +02001209 struct drm_crtc *crtc = hdmi->output.encoder.crtc;
1210 struct drm_device *drm = node->minor->dev;
1211 int err = 0;
Mikko Perttunenccaddfe2013-07-30 11:35:03 +03001212
Thierry Reding29871b22015-07-29 09:46:40 +02001213 drm_modeset_lock_all(drm);
1214
1215 if (!crtc || !crtc->state->active) {
1216 err = -EBUSY;
1217 goto unlock;
1218 }
Thierry Redingedec4af2012-11-15 21:28:23 +00001219
1220#define DUMP_REG(name) \
Thierry Reding4ee8cee2014-12-08 16:25:14 +01001221 seq_printf(s, "%-56s %#05x %08x\n", #name, name, \
1222 tegra_hdmi_readl(hdmi, name))
Thierry Redingedec4af2012-11-15 21:28:23 +00001223
1224 DUMP_REG(HDMI_CTXSW);
1225 DUMP_REG(HDMI_NV_PDISP_SOR_STATE0);
1226 DUMP_REG(HDMI_NV_PDISP_SOR_STATE1);
1227 DUMP_REG(HDMI_NV_PDISP_SOR_STATE2);
1228 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_AN_MSB);
1229 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_AN_LSB);
1230 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_CN_MSB);
1231 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_CN_LSB);
1232 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_AKSV_MSB);
1233 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_AKSV_LSB);
1234 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_BKSV_MSB);
1235 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_BKSV_LSB);
1236 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_CKSV_MSB);
1237 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_CKSV_LSB);
1238 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_DKSV_MSB);
1239 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_DKSV_LSB);
1240 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_CTRL);
1241 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_CMODE);
1242 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_MPRIME_MSB);
1243 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_MPRIME_LSB);
1244 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_SPRIME_MSB);
1245 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_SPRIME_LSB2);
1246 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_SPRIME_LSB1);
1247 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_RI);
1248 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_CS_MSB);
1249 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_CS_LSB);
1250 DUMP_REG(HDMI_NV_PDISP_HDMI_AUDIO_EMU0);
1251 DUMP_REG(HDMI_NV_PDISP_HDMI_AUDIO_EMU_RDATA0);
1252 DUMP_REG(HDMI_NV_PDISP_HDMI_AUDIO_EMU1);
1253 DUMP_REG(HDMI_NV_PDISP_HDMI_AUDIO_EMU2);
1254 DUMP_REG(HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_CTRL);
1255 DUMP_REG(HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_STATUS);
1256 DUMP_REG(HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_HEADER);
1257 DUMP_REG(HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_SUBPACK0_LOW);
1258 DUMP_REG(HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_SUBPACK0_HIGH);
1259 DUMP_REG(HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_CTRL);
1260 DUMP_REG(HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_STATUS);
1261 DUMP_REG(HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_HEADER);
1262 DUMP_REG(HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_SUBPACK0_LOW);
1263 DUMP_REG(HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_SUBPACK0_HIGH);
1264 DUMP_REG(HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_SUBPACK1_LOW);
1265 DUMP_REG(HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_SUBPACK1_HIGH);
1266 DUMP_REG(HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
1267 DUMP_REG(HDMI_NV_PDISP_HDMI_GENERIC_STATUS);
1268 DUMP_REG(HDMI_NV_PDISP_HDMI_GENERIC_HEADER);
1269 DUMP_REG(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK0_LOW);
1270 DUMP_REG(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK0_HIGH);
1271 DUMP_REG(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK1_LOW);
1272 DUMP_REG(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK1_HIGH);
1273 DUMP_REG(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK2_LOW);
1274 DUMP_REG(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK2_HIGH);
1275 DUMP_REG(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK3_LOW);
1276 DUMP_REG(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK3_HIGH);
1277 DUMP_REG(HDMI_NV_PDISP_HDMI_ACR_CTRL);
1278 DUMP_REG(HDMI_NV_PDISP_HDMI_ACR_0320_SUBPACK_LOW);
1279 DUMP_REG(HDMI_NV_PDISP_HDMI_ACR_0320_SUBPACK_HIGH);
1280 DUMP_REG(HDMI_NV_PDISP_HDMI_ACR_0441_SUBPACK_LOW);
1281 DUMP_REG(HDMI_NV_PDISP_HDMI_ACR_0441_SUBPACK_HIGH);
1282 DUMP_REG(HDMI_NV_PDISP_HDMI_ACR_0882_SUBPACK_LOW);
1283 DUMP_REG(HDMI_NV_PDISP_HDMI_ACR_0882_SUBPACK_HIGH);
1284 DUMP_REG(HDMI_NV_PDISP_HDMI_ACR_1764_SUBPACK_LOW);
1285 DUMP_REG(HDMI_NV_PDISP_HDMI_ACR_1764_SUBPACK_HIGH);
1286 DUMP_REG(HDMI_NV_PDISP_HDMI_ACR_0480_SUBPACK_LOW);
1287 DUMP_REG(HDMI_NV_PDISP_HDMI_ACR_0480_SUBPACK_HIGH);
1288 DUMP_REG(HDMI_NV_PDISP_HDMI_ACR_0960_SUBPACK_LOW);
1289 DUMP_REG(HDMI_NV_PDISP_HDMI_ACR_0960_SUBPACK_HIGH);
1290 DUMP_REG(HDMI_NV_PDISP_HDMI_ACR_1920_SUBPACK_LOW);
1291 DUMP_REG(HDMI_NV_PDISP_HDMI_ACR_1920_SUBPACK_HIGH);
1292 DUMP_REG(HDMI_NV_PDISP_HDMI_CTRL);
1293 DUMP_REG(HDMI_NV_PDISP_HDMI_VSYNC_KEEPOUT);
1294 DUMP_REG(HDMI_NV_PDISP_HDMI_VSYNC_WINDOW);
1295 DUMP_REG(HDMI_NV_PDISP_HDMI_GCP_CTRL);
1296 DUMP_REG(HDMI_NV_PDISP_HDMI_GCP_STATUS);
1297 DUMP_REG(HDMI_NV_PDISP_HDMI_GCP_SUBPACK);
1298 DUMP_REG(HDMI_NV_PDISP_HDMI_CHANNEL_STATUS1);
1299 DUMP_REG(HDMI_NV_PDISP_HDMI_CHANNEL_STATUS2);
1300 DUMP_REG(HDMI_NV_PDISP_HDMI_EMU0);
1301 DUMP_REG(HDMI_NV_PDISP_HDMI_EMU1);
1302 DUMP_REG(HDMI_NV_PDISP_HDMI_EMU1_RDATA);
1303 DUMP_REG(HDMI_NV_PDISP_HDMI_SPARE);
1304 DUMP_REG(HDMI_NV_PDISP_HDMI_SPDIF_CHN_STATUS1);
1305 DUMP_REG(HDMI_NV_PDISP_HDMI_SPDIF_CHN_STATUS2);
1306 DUMP_REG(HDMI_NV_PDISP_HDMI_HDCPRIF_ROM_CTRL);
1307 DUMP_REG(HDMI_NV_PDISP_SOR_CAP);
1308 DUMP_REG(HDMI_NV_PDISP_SOR_PWR);
1309 DUMP_REG(HDMI_NV_PDISP_SOR_TEST);
1310 DUMP_REG(HDMI_NV_PDISP_SOR_PLL0);
1311 DUMP_REG(HDMI_NV_PDISP_SOR_PLL1);
1312 DUMP_REG(HDMI_NV_PDISP_SOR_PLL2);
1313 DUMP_REG(HDMI_NV_PDISP_SOR_CSTM);
1314 DUMP_REG(HDMI_NV_PDISP_SOR_LVDS);
1315 DUMP_REG(HDMI_NV_PDISP_SOR_CRCA);
1316 DUMP_REG(HDMI_NV_PDISP_SOR_CRCB);
1317 DUMP_REG(HDMI_NV_PDISP_SOR_BLANK);
1318 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_CTL);
1319 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(0));
1320 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(1));
1321 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(2));
1322 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(3));
1323 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(4));
1324 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(5));
1325 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(6));
1326 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(7));
1327 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(8));
1328 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(9));
1329 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(10));
1330 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(11));
1331 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(12));
1332 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(13));
1333 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(14));
1334 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(15));
1335 DUMP_REG(HDMI_NV_PDISP_SOR_VCRCA0);
1336 DUMP_REG(HDMI_NV_PDISP_SOR_VCRCA1);
1337 DUMP_REG(HDMI_NV_PDISP_SOR_CCRCA0);
1338 DUMP_REG(HDMI_NV_PDISP_SOR_CCRCA1);
1339 DUMP_REG(HDMI_NV_PDISP_SOR_EDATAA0);
1340 DUMP_REG(HDMI_NV_PDISP_SOR_EDATAA1);
1341 DUMP_REG(HDMI_NV_PDISP_SOR_COUNTA0);
1342 DUMP_REG(HDMI_NV_PDISP_SOR_COUNTA1);
1343 DUMP_REG(HDMI_NV_PDISP_SOR_DEBUGA0);
1344 DUMP_REG(HDMI_NV_PDISP_SOR_DEBUGA1);
1345 DUMP_REG(HDMI_NV_PDISP_SOR_TRIG);
1346 DUMP_REG(HDMI_NV_PDISP_SOR_MSCHECK);
1347 DUMP_REG(HDMI_NV_PDISP_SOR_LANE_DRIVE_CURRENT);
1348 DUMP_REG(HDMI_NV_PDISP_AUDIO_DEBUG0);
1349 DUMP_REG(HDMI_NV_PDISP_AUDIO_DEBUG1);
1350 DUMP_REG(HDMI_NV_PDISP_AUDIO_DEBUG2);
1351 DUMP_REG(HDMI_NV_PDISP_AUDIO_FS(0));
1352 DUMP_REG(HDMI_NV_PDISP_AUDIO_FS(1));
1353 DUMP_REG(HDMI_NV_PDISP_AUDIO_FS(2));
1354 DUMP_REG(HDMI_NV_PDISP_AUDIO_FS(3));
1355 DUMP_REG(HDMI_NV_PDISP_AUDIO_FS(4));
1356 DUMP_REG(HDMI_NV_PDISP_AUDIO_FS(5));
1357 DUMP_REG(HDMI_NV_PDISP_AUDIO_FS(6));
1358 DUMP_REG(HDMI_NV_PDISP_AUDIO_PULSE_WIDTH);
1359 DUMP_REG(HDMI_NV_PDISP_AUDIO_THRESHOLD);
1360 DUMP_REG(HDMI_NV_PDISP_AUDIO_CNTRL0);
1361 DUMP_REG(HDMI_NV_PDISP_AUDIO_N);
1362 DUMP_REG(HDMI_NV_PDISP_HDCPRIF_ROM_TIMING);
1363 DUMP_REG(HDMI_NV_PDISP_SOR_REFCLK);
1364 DUMP_REG(HDMI_NV_PDISP_CRC_CONTROL);
1365 DUMP_REG(HDMI_NV_PDISP_INPUT_CONTROL);
1366 DUMP_REG(HDMI_NV_PDISP_SCRATCH);
1367 DUMP_REG(HDMI_NV_PDISP_PE_CURRENT);
1368 DUMP_REG(HDMI_NV_PDISP_KEY_CTRL);
1369 DUMP_REG(HDMI_NV_PDISP_KEY_DEBUG0);
1370 DUMP_REG(HDMI_NV_PDISP_KEY_DEBUG1);
1371 DUMP_REG(HDMI_NV_PDISP_KEY_DEBUG2);
1372 DUMP_REG(HDMI_NV_PDISP_KEY_HDCP_KEY_0);
1373 DUMP_REG(HDMI_NV_PDISP_KEY_HDCP_KEY_1);
1374 DUMP_REG(HDMI_NV_PDISP_KEY_HDCP_KEY_2);
1375 DUMP_REG(HDMI_NV_PDISP_KEY_HDCP_KEY_3);
1376 DUMP_REG(HDMI_NV_PDISP_KEY_HDCP_KEY_TRIG);
1377 DUMP_REG(HDMI_NV_PDISP_KEY_SKEY_INDEX);
1378 DUMP_REG(HDMI_NV_PDISP_SOR_AUDIO_CNTRL0);
Thierry Reding2ccb3962015-01-15 13:43:18 +01001379 DUMP_REG(HDMI_NV_PDISP_SOR_AUDIO_SPARE0);
1380 DUMP_REG(HDMI_NV_PDISP_SOR_AUDIO_HDA_CODEC_SCRATCH0);
1381 DUMP_REG(HDMI_NV_PDISP_SOR_AUDIO_HDA_CODEC_SCRATCH1);
Thierry Redingedec4af2012-11-15 21:28:23 +00001382 DUMP_REG(HDMI_NV_PDISP_SOR_AUDIO_HDA_ELD_BUFWR);
1383 DUMP_REG(HDMI_NV_PDISP_SOR_AUDIO_HDA_PRESENSE);
Thierry Reding2ccb3962015-01-15 13:43:18 +01001384 DUMP_REG(HDMI_NV_PDISP_INT_STATUS);
1385 DUMP_REG(HDMI_NV_PDISP_INT_MASK);
1386 DUMP_REG(HDMI_NV_PDISP_INT_ENABLE);
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +02001387 DUMP_REG(HDMI_NV_PDISP_SOR_IO_PEAK_CURRENT);
Thierry Redingedec4af2012-11-15 21:28:23 +00001388
1389#undef DUMP_REG
1390
Thierry Reding29871b22015-07-29 09:46:40 +02001391unlock:
1392 drm_modeset_unlock_all(drm);
1393 return err;
Thierry Redingedec4af2012-11-15 21:28:23 +00001394}
1395
1396static struct drm_info_list debugfs_files[] = {
1397 { "regs", tegra_hdmi_show_regs, 0, NULL },
1398};
1399
1400static int tegra_hdmi_debugfs_init(struct tegra_hdmi *hdmi,
1401 struct drm_minor *minor)
1402{
1403 unsigned int i;
1404 int err;
1405
1406 hdmi->debugfs = debugfs_create_dir("hdmi", minor->debugfs_root);
1407 if (!hdmi->debugfs)
1408 return -ENOMEM;
1409
1410 hdmi->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1411 GFP_KERNEL);
1412 if (!hdmi->debugfs_files) {
1413 err = -ENOMEM;
1414 goto remove;
1415 }
1416
1417 for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
1418 hdmi->debugfs_files[i].data = hdmi;
1419
1420 err = drm_debugfs_create_files(hdmi->debugfs_files,
1421 ARRAY_SIZE(debugfs_files),
1422 hdmi->debugfs, minor);
1423 if (err < 0)
1424 goto free;
1425
1426 hdmi->minor = minor;
1427
1428 return 0;
1429
1430free:
1431 kfree(hdmi->debugfs_files);
1432 hdmi->debugfs_files = NULL;
1433remove:
1434 debugfs_remove(hdmi->debugfs);
1435 hdmi->debugfs = NULL;
1436
1437 return err;
1438}
1439
Thierry Reding4009c222014-12-19 15:47:30 +01001440static void tegra_hdmi_debugfs_exit(struct tegra_hdmi *hdmi)
Thierry Redingedec4af2012-11-15 21:28:23 +00001441{
1442 drm_debugfs_remove_files(hdmi->debugfs_files, ARRAY_SIZE(debugfs_files),
1443 hdmi->minor);
1444 hdmi->minor = NULL;
1445
1446 kfree(hdmi->debugfs_files);
1447 hdmi->debugfs_files = NULL;
1448
1449 debugfs_remove(hdmi->debugfs);
1450 hdmi->debugfs = NULL;
Thierry Redingedec4af2012-11-15 21:28:23 +00001451}
1452
Thierry Reding53fa7f72013-09-24 15:35:40 +02001453static int tegra_hdmi_init(struct host1x_client *client)
Thierry Redingedec4af2012-11-15 21:28:23 +00001454{
Thierry Reding9910f5c2014-05-22 09:57:15 +02001455 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Reding776dc382013-10-14 14:43:22 +02001456 struct tegra_hdmi *hdmi = host1x_client_to_hdmi(client);
Thierry Redingedec4af2012-11-15 21:28:23 +00001457 int err;
1458
Thierry Redingedec4af2012-11-15 21:28:23 +00001459 hdmi->output.dev = client->dev;
Thierry Redingedec4af2012-11-15 21:28:23 +00001460
Thierry Reding59682712014-11-28 16:50:59 +01001461 drm_connector_init(drm, &hdmi->output.connector,
1462 &tegra_hdmi_connector_funcs,
1463 DRM_MODE_CONNECTOR_HDMIA);
1464 drm_connector_helper_add(&hdmi->output.connector,
1465 &tegra_hdmi_connector_helper_funcs);
1466 hdmi->output.connector.dpms = DRM_MODE_DPMS_OFF;
1467
1468 drm_encoder_init(drm, &hdmi->output.encoder, &tegra_hdmi_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001469 DRM_MODE_ENCODER_TMDS, NULL);
Thierry Reding59682712014-11-28 16:50:59 +01001470 drm_encoder_helper_add(&hdmi->output.encoder,
1471 &tegra_hdmi_encoder_helper_funcs);
1472
1473 drm_mode_connector_attach_encoder(&hdmi->output.connector,
1474 &hdmi->output.encoder);
1475 drm_connector_register(&hdmi->output.connector);
1476
Thierry Redingea130b22014-12-19 15:51:35 +01001477 err = tegra_output_init(drm, &hdmi->output);
1478 if (err < 0) {
1479 dev_err(client->dev, "failed to initialize output: %d\n", err);
1480 return err;
1481 }
Thierry Reding59682712014-11-28 16:50:59 +01001482
Thierry Redingea130b22014-12-19 15:51:35 +01001483 hdmi->output.encoder.possible_crtcs = 0x3;
Thierry Redingedec4af2012-11-15 21:28:23 +00001484
1485 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
Thierry Reding9910f5c2014-05-22 09:57:15 +02001486 err = tegra_hdmi_debugfs_init(hdmi, drm->primary);
Thierry Redingedec4af2012-11-15 21:28:23 +00001487 if (err < 0)
1488 dev_err(client->dev, "debugfs setup failed: %d\n", err);
1489 }
1490
Thierry Redingfb50a112014-02-28 16:57:34 +01001491 err = regulator_enable(hdmi->hdmi);
1492 if (err < 0) {
1493 dev_err(client->dev, "failed to enable HDMI regulator: %d\n",
1494 err);
1495 return err;
1496 }
1497
Thierry Reding59682712014-11-28 16:50:59 +01001498 err = regulator_enable(hdmi->pll);
1499 if (err < 0) {
1500 dev_err(hdmi->dev, "failed to enable PLL regulator: %d\n", err);
1501 return err;
1502 }
1503
1504 err = regulator_enable(hdmi->vdd);
1505 if (err < 0) {
1506 dev_err(hdmi->dev, "failed to enable VDD regulator: %d\n", err);
1507 return err;
1508 }
1509
1510 err = clk_prepare_enable(hdmi->clk);
1511 if (err < 0) {
1512 dev_err(hdmi->dev, "failed to enable clock: %d\n", err);
1513 return err;
1514 }
1515
1516 reset_control_deassert(hdmi->rst);
1517
Thierry Reding2ccb3962015-01-15 13:43:18 +01001518 /*
1519 * Enable and unmask the HDA codec SCRATCH0 register interrupt. This
1520 * is used for interoperability between the HDA codec driver and the
1521 * HDMI driver.
1522 */
1523 tegra_hdmi_writel(hdmi, INT_CODEC_SCRATCH0, HDMI_NV_PDISP_INT_ENABLE);
1524 tegra_hdmi_writel(hdmi, INT_CODEC_SCRATCH0, HDMI_NV_PDISP_INT_MASK);
1525
Thierry Redingedec4af2012-11-15 21:28:23 +00001526 return 0;
1527}
1528
Thierry Reding53fa7f72013-09-24 15:35:40 +02001529static int tegra_hdmi_exit(struct host1x_client *client)
Thierry Redingedec4af2012-11-15 21:28:23 +00001530{
Thierry Reding776dc382013-10-14 14:43:22 +02001531 struct tegra_hdmi *hdmi = host1x_client_to_hdmi(client);
Thierry Redingedec4af2012-11-15 21:28:23 +00001532
Thierry Reding2ccb3962015-01-15 13:43:18 +01001533 tegra_hdmi_writel(hdmi, 0, HDMI_NV_PDISP_INT_MASK);
1534 tegra_hdmi_writel(hdmi, 0, HDMI_NV_PDISP_INT_ENABLE);
1535
Thierry Reding59682712014-11-28 16:50:59 +01001536 tegra_output_exit(&hdmi->output);
1537
Thierry Reding59682712014-11-28 16:50:59 +01001538 reset_control_assert(hdmi->rst);
Thierry Reding375e1182015-01-28 16:14:26 +01001539 clk_disable_unprepare(hdmi->clk);
Thierry Reding59682712014-11-28 16:50:59 +01001540
1541 regulator_disable(hdmi->vdd);
1542 regulator_disable(hdmi->pll);
Thierry Redingfb50a112014-02-28 16:57:34 +01001543 regulator_disable(hdmi->hdmi);
1544
Thierry Reding4009c222014-12-19 15:47:30 +01001545 if (IS_ENABLED(CONFIG_DEBUG_FS))
1546 tegra_hdmi_debugfs_exit(hdmi);
Thierry Redingedec4af2012-11-15 21:28:23 +00001547
Thierry Redingedec4af2012-11-15 21:28:23 +00001548 return 0;
1549}
1550
1551static const struct host1x_client_ops hdmi_client_ops = {
Thierry Reding53fa7f72013-09-24 15:35:40 +02001552 .init = tegra_hdmi_init,
1553 .exit = tegra_hdmi_exit,
Thierry Redingedec4af2012-11-15 21:28:23 +00001554};
1555
Thierry Reding59af0592013-10-14 09:43:05 +02001556static const struct tegra_hdmi_config tegra20_hdmi_config = {
1557 .tmds = tegra20_tmds_config,
1558 .num_tmds = ARRAY_SIZE(tegra20_tmds_config),
1559 .fuse_override_offset = HDMI_NV_PDISP_SOR_LANE_DRIVE_CURRENT,
1560 .fuse_override_value = 1 << 31,
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +02001561 .has_sor_io_peak_current = false,
Thierry Reding2ccb3962015-01-15 13:43:18 +01001562 .has_hda = false,
1563 .has_hbr = false,
Thierry Reding59af0592013-10-14 09:43:05 +02001564};
1565
1566static const struct tegra_hdmi_config tegra30_hdmi_config = {
1567 .tmds = tegra30_tmds_config,
1568 .num_tmds = ARRAY_SIZE(tegra30_tmds_config),
1569 .fuse_override_offset = HDMI_NV_PDISP_SOR_LANE_DRIVE_CURRENT,
1570 .fuse_override_value = 1 << 31,
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +02001571 .has_sor_io_peak_current = false,
Thierry Reding2ccb3962015-01-15 13:43:18 +01001572 .has_hda = true,
1573 .has_hbr = false,
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +02001574};
1575
1576static const struct tegra_hdmi_config tegra114_hdmi_config = {
1577 .tmds = tegra114_tmds_config,
1578 .num_tmds = ARRAY_SIZE(tegra114_tmds_config),
1579 .fuse_override_offset = HDMI_NV_PDISP_SOR_PAD_CTLS0,
1580 .fuse_override_value = 1 << 31,
1581 .has_sor_io_peak_current = true,
Thierry Reding2ccb3962015-01-15 13:43:18 +01001582 .has_hda = true,
1583 .has_hbr = true,
Thierry Reding59af0592013-10-14 09:43:05 +02001584};
1585
Thierry Redingfb7be702013-11-15 16:07:32 +01001586static const struct tegra_hdmi_config tegra124_hdmi_config = {
1587 .tmds = tegra124_tmds_config,
1588 .num_tmds = ARRAY_SIZE(tegra124_tmds_config),
1589 .fuse_override_offset = HDMI_NV_PDISP_SOR_PAD_CTLS0,
1590 .fuse_override_value = 1 << 31,
1591 .has_sor_io_peak_current = true,
Thierry Reding2ccb3962015-01-15 13:43:18 +01001592 .has_hda = true,
1593 .has_hbr = true,
Thierry Redingfb7be702013-11-15 16:07:32 +01001594};
1595
Thierry Reding59af0592013-10-14 09:43:05 +02001596static const struct of_device_id tegra_hdmi_of_match[] = {
Thierry Redingfb7be702013-11-15 16:07:32 +01001597 { .compatible = "nvidia,tegra124-hdmi", .data = &tegra124_hdmi_config },
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +02001598 { .compatible = "nvidia,tegra114-hdmi", .data = &tegra114_hdmi_config },
Thierry Reding59af0592013-10-14 09:43:05 +02001599 { .compatible = "nvidia,tegra30-hdmi", .data = &tegra30_hdmi_config },
1600 { .compatible = "nvidia,tegra20-hdmi", .data = &tegra20_hdmi_config },
1601 { },
1602};
Stephen Warrenef707282014-06-18 16:21:55 -06001603MODULE_DEVICE_TABLE(of, tegra_hdmi_of_match);
Thierry Reding59af0592013-10-14 09:43:05 +02001604
Thierry Reding2ccb3962015-01-15 13:43:18 +01001605static void hda_format_parse(unsigned int format, unsigned int *rate,
1606 unsigned int *channels)
1607{
1608 unsigned int mul, div;
1609
1610 if (format & AC_FMT_BASE_44K)
1611 *rate = 44100;
1612 else
1613 *rate = 48000;
1614
1615 mul = (format & AC_FMT_MULT_MASK) >> AC_FMT_MULT_SHIFT;
1616 div = (format & AC_FMT_DIV_MASK) >> AC_FMT_DIV_SHIFT;
1617
1618 *rate = *rate * (mul + 1) / (div + 1);
1619
1620 *channels = (format & AC_FMT_CHAN_MASK) >> AC_FMT_CHAN_SHIFT;
1621}
1622
1623static irqreturn_t tegra_hdmi_irq(int irq, void *data)
1624{
1625 struct tegra_hdmi *hdmi = data;
1626 u32 value;
1627 int err;
1628
1629 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_INT_STATUS);
1630 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_INT_STATUS);
1631
1632 if (value & INT_CODEC_SCRATCH0) {
1633 unsigned int format;
1634 u32 value;
1635
1636 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_SOR_AUDIO_HDA_CODEC_SCRATCH0);
1637
1638 if (value & SOR_AUDIO_HDA_CODEC_SCRATCH0_VALID) {
1639 unsigned int sample_rate, channels;
1640
1641 format = value & SOR_AUDIO_HDA_CODEC_SCRATCH0_FMT_MASK;
1642
1643 hda_format_parse(format, &sample_rate, &channels);
1644
1645 hdmi->audio_sample_rate = sample_rate;
1646 hdmi->audio_channels = channels;
1647
1648 err = tegra_hdmi_setup_audio(hdmi);
1649 if (err < 0) {
1650 tegra_hdmi_disable_audio_infoframe(hdmi);
1651 tegra_hdmi_disable_audio(hdmi);
1652 } else {
1653 tegra_hdmi_setup_audio_infoframe(hdmi);
1654 tegra_hdmi_enable_audio_infoframe(hdmi);
1655 tegra_hdmi_enable_audio(hdmi);
1656 }
1657 } else {
1658 tegra_hdmi_disable_audio_infoframe(hdmi);
1659 tegra_hdmi_disable_audio(hdmi);
1660 }
1661 }
1662
1663 return IRQ_HANDLED;
1664}
1665
Thierry Redingedec4af2012-11-15 21:28:23 +00001666static int tegra_hdmi_probe(struct platform_device *pdev)
1667{
Thierry Reding59af0592013-10-14 09:43:05 +02001668 const struct of_device_id *match;
Thierry Redingedec4af2012-11-15 21:28:23 +00001669 struct tegra_hdmi *hdmi;
1670 struct resource *regs;
1671 int err;
1672
Thierry Reding59af0592013-10-14 09:43:05 +02001673 match = of_match_node(tegra_hdmi_of_match, pdev->dev.of_node);
1674 if (!match)
1675 return -ENODEV;
1676
Thierry Redingedec4af2012-11-15 21:28:23 +00001677 hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
1678 if (!hdmi)
1679 return -ENOMEM;
1680
Thierry Reding59af0592013-10-14 09:43:05 +02001681 hdmi->config = match->data;
Thierry Redingedec4af2012-11-15 21:28:23 +00001682 hdmi->dev = &pdev->dev;
Thierry Reding2ccb3962015-01-15 13:43:18 +01001683
Thierry Redingedec4af2012-11-15 21:28:23 +00001684 hdmi->audio_source = AUTO;
Thierry Reding2ccb3962015-01-15 13:43:18 +01001685 hdmi->audio_sample_rate = 48000;
1686 hdmi->audio_channels = 2;
Thierry Redingedec4af2012-11-15 21:28:23 +00001687 hdmi->stereo = false;
1688 hdmi->dvi = false;
1689
1690 hdmi->clk = devm_clk_get(&pdev->dev, NULL);
1691 if (IS_ERR(hdmi->clk)) {
1692 dev_err(&pdev->dev, "failed to get clock\n");
1693 return PTR_ERR(hdmi->clk);
1694 }
1695
Stephen Warrenca480802013-11-06 16:20:54 -07001696 hdmi->rst = devm_reset_control_get(&pdev->dev, "hdmi");
1697 if (IS_ERR(hdmi->rst)) {
1698 dev_err(&pdev->dev, "failed to get reset\n");
1699 return PTR_ERR(hdmi->rst);
1700 }
1701
Thierry Redingedec4af2012-11-15 21:28:23 +00001702 hdmi->clk_parent = devm_clk_get(&pdev->dev, "parent");
1703 if (IS_ERR(hdmi->clk_parent))
1704 return PTR_ERR(hdmi->clk_parent);
1705
Thierry Redingedec4af2012-11-15 21:28:23 +00001706 err = clk_set_parent(hdmi->clk, hdmi->clk_parent);
1707 if (err < 0) {
1708 dev_err(&pdev->dev, "failed to setup clocks: %d\n", err);
1709 return err;
1710 }
1711
Thierry Redingfb50a112014-02-28 16:57:34 +01001712 hdmi->hdmi = devm_regulator_get(&pdev->dev, "hdmi");
1713 if (IS_ERR(hdmi->hdmi)) {
1714 dev_err(&pdev->dev, "failed to get HDMI regulator\n");
1715 return PTR_ERR(hdmi->hdmi);
1716 }
1717
Thierry Redingedec4af2012-11-15 21:28:23 +00001718 hdmi->pll = devm_regulator_get(&pdev->dev, "pll");
1719 if (IS_ERR(hdmi->pll)) {
1720 dev_err(&pdev->dev, "failed to get PLL regulator\n");
1721 return PTR_ERR(hdmi->pll);
1722 }
1723
Thierry Reding88685682014-04-16 10:24:12 +02001724 hdmi->vdd = devm_regulator_get(&pdev->dev, "vdd");
1725 if (IS_ERR(hdmi->vdd)) {
1726 dev_err(&pdev->dev, "failed to get VDD regulator\n");
1727 return PTR_ERR(hdmi->vdd);
1728 }
1729
Thierry Redingedec4af2012-11-15 21:28:23 +00001730 hdmi->output.dev = &pdev->dev;
1731
Thierry Reding59d29c02013-10-14 14:26:42 +02001732 err = tegra_output_probe(&hdmi->output);
Thierry Redingedec4af2012-11-15 21:28:23 +00001733 if (err < 0)
1734 return err;
1735
1736 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Redingd4ed6022013-01-21 11:09:02 +01001737 hdmi->regs = devm_ioremap_resource(&pdev->dev, regs);
1738 if (IS_ERR(hdmi->regs))
1739 return PTR_ERR(hdmi->regs);
Thierry Redingedec4af2012-11-15 21:28:23 +00001740
1741 err = platform_get_irq(pdev, 0);
1742 if (err < 0)
1743 return err;
1744
1745 hdmi->irq = err;
1746
Thierry Reding2ccb3962015-01-15 13:43:18 +01001747 err = devm_request_irq(hdmi->dev, hdmi->irq, tegra_hdmi_irq, 0,
1748 dev_name(hdmi->dev), hdmi);
1749 if (err < 0) {
1750 dev_err(&pdev->dev, "failed to request IRQ#%u: %d\n",
1751 hdmi->irq, err);
1752 return err;
1753 }
1754
Thierry Reding776dc382013-10-14 14:43:22 +02001755 INIT_LIST_HEAD(&hdmi->client.list);
1756 hdmi->client.ops = &hdmi_client_ops;
1757 hdmi->client.dev = &pdev->dev;
Thierry Redingedec4af2012-11-15 21:28:23 +00001758
Thierry Reding776dc382013-10-14 14:43:22 +02001759 err = host1x_client_register(&hdmi->client);
Thierry Redingedec4af2012-11-15 21:28:23 +00001760 if (err < 0) {
1761 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
1762 err);
1763 return err;
1764 }
1765
1766 platform_set_drvdata(pdev, hdmi);
1767
1768 return 0;
1769}
1770
1771static int tegra_hdmi_remove(struct platform_device *pdev)
1772{
Thierry Redingedec4af2012-11-15 21:28:23 +00001773 struct tegra_hdmi *hdmi = platform_get_drvdata(pdev);
1774 int err;
1775
Thierry Reding776dc382013-10-14 14:43:22 +02001776 err = host1x_client_unregister(&hdmi->client);
Thierry Redingedec4af2012-11-15 21:28:23 +00001777 if (err < 0) {
1778 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1779 err);
1780 return err;
1781 }
1782
Thierry Reding328ec692014-12-19 15:55:08 +01001783 tegra_output_remove(&hdmi->output);
Thierry Reding59d29c02013-10-14 14:26:42 +02001784
Thierry Redingd06e7f82014-04-16 10:43:41 +02001785 clk_disable_unprepare(hdmi->clk_parent);
1786 clk_disable_unprepare(hdmi->clk);
Thierry Redingedec4af2012-11-15 21:28:23 +00001787
1788 return 0;
1789}
1790
Thierry Redingedec4af2012-11-15 21:28:23 +00001791struct platform_driver tegra_hdmi_driver = {
1792 .driver = {
1793 .name = "tegra-hdmi",
1794 .owner = THIS_MODULE,
1795 .of_match_table = tegra_hdmi_of_match,
1796 },
1797 .probe = tegra_hdmi_probe,
1798 .remove = tegra_hdmi_remove,
1799};