blob: cf9f69fc12e6557c147a58fbeffad91da1f028b7 [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>
Thierry Reding52345492015-08-07 16:00:43 +020014#include <linux/pm_runtime.h>
Thierry Reding9eb9b222013-09-24 16:32:47 +020015#include <linux/regulator/consumer.h>
Stephen Warrenca480802013-11-06 16:20:54 -070016#include <linux/reset.h>
Thierry Redingac24c222012-11-23 15:14:00 +010017
Thierry Reding4aa3df72014-11-24 16:27:13 +010018#include <drm/drm_atomic_helper.h>
Thierry Reding59682712014-11-28 16:50:59 +010019#include <drm/drm_crtc.h>
20#include <drm/drm_crtc_helper.h>
21
Thierry Reding2ccb3962015-01-15 13:43:18 +010022#include <sound/hda_verbs.h>
23
Thierry Redingedec4af2012-11-15 21:28:23 +000024#include "hdmi.h"
25#include "drm.h"
26#include "dc.h"
Thierry Reding07a8aab2017-08-15 15:41:11 +020027#include "trace.h"
Thierry Redingedec4af2012-11-15 21:28:23 +000028
Thierry Reding2ccb3962015-01-15 13:43:18 +010029#define HDMI_ELD_BUFFER_SIZE 96
30
Thierry Reding59af0592013-10-14 09:43:05 +020031struct tmds_config {
32 unsigned int pclk;
33 u32 pll0;
34 u32 pll1;
35 u32 pe_current;
36 u32 drive_current;
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +020037 u32 peak_current;
Thierry Reding59af0592013-10-14 09:43:05 +020038};
39
40struct tegra_hdmi_config {
41 const struct tmds_config *tmds;
42 unsigned int num_tmds;
43
44 unsigned long fuse_override_offset;
Thierry Reding4ee8cee2014-12-08 16:25:14 +010045 u32 fuse_override_value;
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +020046
47 bool has_sor_io_peak_current;
Thierry Reding2ccb3962015-01-15 13:43:18 +010048 bool has_hda;
49 bool has_hbr;
Thierry Reding59af0592013-10-14 09:43:05 +020050};
51
Thierry Redingedec4af2012-11-15 21:28:23 +000052struct tegra_hdmi {
Thierry Reding776dc382013-10-14 14:43:22 +020053 struct host1x_client client;
Thierry Redingedec4af2012-11-15 21:28:23 +000054 struct tegra_output output;
55 struct device *dev;
56
Thierry Redingfb50a112014-02-28 16:57:34 +010057 struct regulator *hdmi;
Thierry Redingedec4af2012-11-15 21:28:23 +000058 struct regulator *pll;
Thierry Reding88685682014-04-16 10:24:12 +020059 struct regulator *vdd;
Thierry Redingedec4af2012-11-15 21:28:23 +000060
61 void __iomem *regs;
62 unsigned int irq;
63
64 struct clk *clk_parent;
65 struct clk *clk;
Stephen Warrenca480802013-11-06 16:20:54 -070066 struct reset_control *rst;
Thierry Redingedec4af2012-11-15 21:28:23 +000067
Thierry Reding59af0592013-10-14 09:43:05 +020068 const struct tegra_hdmi_config *config;
69
Thierry Redingedec4af2012-11-15 21:28:23 +000070 unsigned int audio_source;
Thierry Reding2ccb3962015-01-15 13:43:18 +010071 unsigned int audio_sample_rate;
72 unsigned int audio_channels;
73
74 unsigned int pixel_clock;
Thierry Redingedec4af2012-11-15 21:28:23 +000075 bool stereo;
76 bool dvi;
77
78 struct drm_info_list *debugfs_files;
79 struct drm_minor *minor;
80 struct dentry *debugfs;
81};
82
83static inline struct tegra_hdmi *
Thierry Reding776dc382013-10-14 14:43:22 +020084host1x_client_to_hdmi(struct host1x_client *client)
Thierry Redingedec4af2012-11-15 21:28:23 +000085{
86 return container_of(client, struct tegra_hdmi, client);
87}
88
89static inline struct tegra_hdmi *to_hdmi(struct tegra_output *output)
90{
91 return container_of(output, struct tegra_hdmi, output);
92}
93
94#define HDMI_AUDIOCLK_FREQ 216000000
95#define HDMI_REKEY_DEFAULT 56
96
97enum {
98 AUTO = 0,
99 SPDIF,
100 HDA,
101};
102
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100103static inline u32 tegra_hdmi_readl(struct tegra_hdmi *hdmi,
Thierry Reding7efe20c2017-08-15 15:41:08 +0200104 unsigned int offset)
Thierry Redingedec4af2012-11-15 21:28:23 +0000105{
Thierry Reding07a8aab2017-08-15 15:41:11 +0200106 u32 value = readl(hdmi->regs + (offset << 2));
107
108 trace_hdmi_readl(hdmi->dev, offset, value);
109
110 return value;
Thierry Redingedec4af2012-11-15 21:28:23 +0000111}
112
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100113static inline void tegra_hdmi_writel(struct tegra_hdmi *hdmi, u32 value,
Thierry Reding7efe20c2017-08-15 15:41:08 +0200114 unsigned int offset)
Thierry Redingedec4af2012-11-15 21:28:23 +0000115{
Thierry Reding07a8aab2017-08-15 15:41:11 +0200116 trace_hdmi_writel(hdmi->dev, offset, value);
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100117 writel(value, hdmi->regs + (offset << 2));
Thierry Redingedec4af2012-11-15 21:28:23 +0000118}
119
120struct tegra_hdmi_audio_config {
121 unsigned int pclk;
122 unsigned int n;
123 unsigned int cts;
124 unsigned int aval;
125};
126
127static const struct tegra_hdmi_audio_config tegra_hdmi_audio_32k[] = {
128 { 25200000, 4096, 25200, 24000 },
129 { 27000000, 4096, 27000, 24000 },
130 { 74250000, 4096, 74250, 24000 },
131 { 148500000, 4096, 148500, 24000 },
132 { 0, 0, 0, 0 },
133};
134
135static const struct tegra_hdmi_audio_config tegra_hdmi_audio_44_1k[] = {
136 { 25200000, 5880, 26250, 25000 },
137 { 27000000, 5880, 28125, 25000 },
138 { 74250000, 4704, 61875, 20000 },
139 { 148500000, 4704, 123750, 20000 },
140 { 0, 0, 0, 0 },
141};
142
143static const struct tegra_hdmi_audio_config tegra_hdmi_audio_48k[] = {
144 { 25200000, 6144, 25200, 24000 },
145 { 27000000, 6144, 27000, 24000 },
146 { 74250000, 6144, 74250, 24000 },
147 { 148500000, 6144, 148500, 24000 },
148 { 0, 0, 0, 0 },
149};
150
151static const struct tegra_hdmi_audio_config tegra_hdmi_audio_88_2k[] = {
152 { 25200000, 11760, 26250, 25000 },
153 { 27000000, 11760, 28125, 25000 },
154 { 74250000, 9408, 61875, 20000 },
155 { 148500000, 9408, 123750, 20000 },
156 { 0, 0, 0, 0 },
157};
158
159static const struct tegra_hdmi_audio_config tegra_hdmi_audio_96k[] = {
160 { 25200000, 12288, 25200, 24000 },
161 { 27000000, 12288, 27000, 24000 },
162 { 74250000, 12288, 74250, 24000 },
163 { 148500000, 12288, 148500, 24000 },
164 { 0, 0, 0, 0 },
165};
166
167static const struct tegra_hdmi_audio_config tegra_hdmi_audio_176_4k[] = {
168 { 25200000, 23520, 26250, 25000 },
169 { 27000000, 23520, 28125, 25000 },
170 { 74250000, 18816, 61875, 20000 },
171 { 148500000, 18816, 123750, 20000 },
172 { 0, 0, 0, 0 },
173};
174
175static const struct tegra_hdmi_audio_config tegra_hdmi_audio_192k[] = {
176 { 25200000, 24576, 25200, 24000 },
177 { 27000000, 24576, 27000, 24000 },
178 { 74250000, 24576, 74250, 24000 },
179 { 148500000, 24576, 148500, 24000 },
180 { 0, 0, 0, 0 },
181};
182
Thierry Redingf27db962013-09-30 15:14:41 +0200183static const struct tmds_config tegra20_tmds_config[] = {
Lucas Stachfa416dd2012-12-19 21:38:55 +0000184 { /* slow pixel clock modes */
Thierry Redingedec4af2012-11-15 21:28:23 +0000185 .pclk = 27000000,
186 .pll0 = SOR_PLL_BG_V17_S(3) | SOR_PLL_ICHPMP(1) |
187 SOR_PLL_RESISTORSEL | SOR_PLL_VCOCAP(0) |
188 SOR_PLL_TX_REG_LOAD(3),
189 .pll1 = SOR_PLL_TMDS_TERM_ENABLE,
190 .pe_current = PE_CURRENT0(PE_CURRENT_0_0_mA) |
191 PE_CURRENT1(PE_CURRENT_0_0_mA) |
192 PE_CURRENT2(PE_CURRENT_0_0_mA) |
193 PE_CURRENT3(PE_CURRENT_0_0_mA),
194 .drive_current = DRIVE_CURRENT_LANE0(DRIVE_CURRENT_7_125_mA) |
195 DRIVE_CURRENT_LANE1(DRIVE_CURRENT_7_125_mA) |
196 DRIVE_CURRENT_LANE2(DRIVE_CURRENT_7_125_mA) |
197 DRIVE_CURRENT_LANE3(DRIVE_CURRENT_7_125_mA),
Lucas Stachfa416dd2012-12-19 21:38:55 +0000198 },
199 { /* high pixel clock modes */
Thierry Redingedec4af2012-11-15 21:28:23 +0000200 .pclk = UINT_MAX,
201 .pll0 = SOR_PLL_BG_V17_S(3) | SOR_PLL_ICHPMP(1) |
202 SOR_PLL_RESISTORSEL | SOR_PLL_VCOCAP(1) |
203 SOR_PLL_TX_REG_LOAD(3),
204 .pll1 = SOR_PLL_TMDS_TERM_ENABLE | SOR_PLL_PE_EN,
205 .pe_current = PE_CURRENT0(PE_CURRENT_6_0_mA) |
206 PE_CURRENT1(PE_CURRENT_6_0_mA) |
207 PE_CURRENT2(PE_CURRENT_6_0_mA) |
208 PE_CURRENT3(PE_CURRENT_6_0_mA),
209 .drive_current = DRIVE_CURRENT_LANE0(DRIVE_CURRENT_7_125_mA) |
210 DRIVE_CURRENT_LANE1(DRIVE_CURRENT_7_125_mA) |
211 DRIVE_CURRENT_LANE2(DRIVE_CURRENT_7_125_mA) |
212 DRIVE_CURRENT_LANE3(DRIVE_CURRENT_7_125_mA),
213 },
214};
215
Thierry Redingf27db962013-09-30 15:14:41 +0200216static const struct tmds_config tegra30_tmds_config[] = {
Thierry Redingedec4af2012-11-15 21:28:23 +0000217 { /* 480p modes */
218 .pclk = 27000000,
219 .pll0 = SOR_PLL_BG_V17_S(3) | SOR_PLL_ICHPMP(1) |
220 SOR_PLL_RESISTORSEL | SOR_PLL_VCOCAP(0) |
221 SOR_PLL_TX_REG_LOAD(0),
222 .pll1 = SOR_PLL_TMDS_TERM_ENABLE,
223 .pe_current = PE_CURRENT0(PE_CURRENT_0_0_mA) |
224 PE_CURRENT1(PE_CURRENT_0_0_mA) |
225 PE_CURRENT2(PE_CURRENT_0_0_mA) |
226 PE_CURRENT3(PE_CURRENT_0_0_mA),
227 .drive_current = DRIVE_CURRENT_LANE0(DRIVE_CURRENT_5_250_mA) |
228 DRIVE_CURRENT_LANE1(DRIVE_CURRENT_5_250_mA) |
229 DRIVE_CURRENT_LANE2(DRIVE_CURRENT_5_250_mA) |
230 DRIVE_CURRENT_LANE3(DRIVE_CURRENT_5_250_mA),
231 }, { /* 720p modes */
232 .pclk = 74250000,
233 .pll0 = SOR_PLL_BG_V17_S(3) | SOR_PLL_ICHPMP(1) |
234 SOR_PLL_RESISTORSEL | SOR_PLL_VCOCAP(1) |
235 SOR_PLL_TX_REG_LOAD(0),
236 .pll1 = SOR_PLL_TMDS_TERM_ENABLE | SOR_PLL_PE_EN,
237 .pe_current = PE_CURRENT0(PE_CURRENT_5_0_mA) |
238 PE_CURRENT1(PE_CURRENT_5_0_mA) |
239 PE_CURRENT2(PE_CURRENT_5_0_mA) |
240 PE_CURRENT3(PE_CURRENT_5_0_mA),
241 .drive_current = DRIVE_CURRENT_LANE0(DRIVE_CURRENT_5_250_mA) |
242 DRIVE_CURRENT_LANE1(DRIVE_CURRENT_5_250_mA) |
243 DRIVE_CURRENT_LANE2(DRIVE_CURRENT_5_250_mA) |
244 DRIVE_CURRENT_LANE3(DRIVE_CURRENT_5_250_mA),
245 }, { /* 1080p modes */
246 .pclk = UINT_MAX,
247 .pll0 = SOR_PLL_BG_V17_S(3) | SOR_PLL_ICHPMP(1) |
248 SOR_PLL_RESISTORSEL | SOR_PLL_VCOCAP(3) |
249 SOR_PLL_TX_REG_LOAD(0),
250 .pll1 = SOR_PLL_TMDS_TERM_ENABLE | SOR_PLL_PE_EN,
251 .pe_current = PE_CURRENT0(PE_CURRENT_5_0_mA) |
252 PE_CURRENT1(PE_CURRENT_5_0_mA) |
253 PE_CURRENT2(PE_CURRENT_5_0_mA) |
254 PE_CURRENT3(PE_CURRENT_5_0_mA),
255 .drive_current = DRIVE_CURRENT_LANE0(DRIVE_CURRENT_5_250_mA) |
256 DRIVE_CURRENT_LANE1(DRIVE_CURRENT_5_250_mA) |
257 DRIVE_CURRENT_LANE2(DRIVE_CURRENT_5_250_mA) |
258 DRIVE_CURRENT_LANE3(DRIVE_CURRENT_5_250_mA),
259 },
260};
261
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +0200262static const struct tmds_config tegra114_tmds_config[] = {
263 { /* 480p/576p / 25.2MHz/27MHz modes */
264 .pclk = 27000000,
265 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
266 SOR_PLL_VCOCAP(0) | SOR_PLL_RESISTORSEL,
267 .pll1 = SOR_PLL_LOADADJ(3) | SOR_PLL_TMDS_TERMADJ(0),
268 .pe_current = PE_CURRENT0(PE_CURRENT_0_mA_T114) |
269 PE_CURRENT1(PE_CURRENT_0_mA_T114) |
270 PE_CURRENT2(PE_CURRENT_0_mA_T114) |
271 PE_CURRENT3(PE_CURRENT_0_mA_T114),
272 .drive_current =
273 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_10_400_mA_T114) |
274 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_10_400_mA_T114) |
275 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_10_400_mA_T114) |
276 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_10_400_mA_T114),
277 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_0_000_mA) |
278 PEAK_CURRENT_LANE1(PEAK_CURRENT_0_000_mA) |
279 PEAK_CURRENT_LANE2(PEAK_CURRENT_0_000_mA) |
280 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_000_mA),
281 }, { /* 720p / 74.25MHz modes */
282 .pclk = 74250000,
283 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
284 SOR_PLL_VCOCAP(1) | SOR_PLL_RESISTORSEL,
285 .pll1 = SOR_PLL_PE_EN | SOR_PLL_LOADADJ(3) |
286 SOR_PLL_TMDS_TERMADJ(0),
287 .pe_current = PE_CURRENT0(PE_CURRENT_15_mA_T114) |
288 PE_CURRENT1(PE_CURRENT_15_mA_T114) |
289 PE_CURRENT2(PE_CURRENT_15_mA_T114) |
290 PE_CURRENT3(PE_CURRENT_15_mA_T114),
291 .drive_current =
292 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_10_400_mA_T114) |
293 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_10_400_mA_T114) |
294 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_10_400_mA_T114) |
295 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_10_400_mA_T114),
296 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_0_000_mA) |
297 PEAK_CURRENT_LANE1(PEAK_CURRENT_0_000_mA) |
298 PEAK_CURRENT_LANE2(PEAK_CURRENT_0_000_mA) |
299 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_000_mA),
300 }, { /* 1080p / 148.5MHz modes */
301 .pclk = 148500000,
302 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
303 SOR_PLL_VCOCAP(3) | SOR_PLL_RESISTORSEL,
304 .pll1 = SOR_PLL_PE_EN | SOR_PLL_LOADADJ(3) |
305 SOR_PLL_TMDS_TERMADJ(0),
306 .pe_current = PE_CURRENT0(PE_CURRENT_10_mA_T114) |
307 PE_CURRENT1(PE_CURRENT_10_mA_T114) |
308 PE_CURRENT2(PE_CURRENT_10_mA_T114) |
309 PE_CURRENT3(PE_CURRENT_10_mA_T114),
310 .drive_current =
311 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_12_400_mA_T114) |
312 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_12_400_mA_T114) |
313 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_12_400_mA_T114) |
314 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_12_400_mA_T114),
315 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_0_000_mA) |
316 PEAK_CURRENT_LANE1(PEAK_CURRENT_0_000_mA) |
317 PEAK_CURRENT_LANE2(PEAK_CURRENT_0_000_mA) |
318 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_000_mA),
319 }, { /* 225/297MHz modes */
320 .pclk = UINT_MAX,
321 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
322 SOR_PLL_VCOCAP(0xf) | SOR_PLL_RESISTORSEL,
323 .pll1 = SOR_PLL_LOADADJ(3) | SOR_PLL_TMDS_TERMADJ(7)
324 | SOR_PLL_TMDS_TERM_ENABLE,
325 .pe_current = PE_CURRENT0(PE_CURRENT_0_mA_T114) |
326 PE_CURRENT1(PE_CURRENT_0_mA_T114) |
327 PE_CURRENT2(PE_CURRENT_0_mA_T114) |
328 PE_CURRENT3(PE_CURRENT_0_mA_T114),
329 .drive_current =
330 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_25_200_mA_T114) |
331 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_25_200_mA_T114) |
332 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_25_200_mA_T114) |
333 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_19_200_mA_T114),
334 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_3_000_mA) |
335 PEAK_CURRENT_LANE1(PEAK_CURRENT_3_000_mA) |
336 PEAK_CURRENT_LANE2(PEAK_CURRENT_3_000_mA) |
337 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_800_mA),
338 },
339};
340
Thierry Redingfb7be702013-11-15 16:07:32 +0100341static const struct tmds_config tegra124_tmds_config[] = {
342 { /* 480p/576p / 25.2MHz/27MHz modes */
343 .pclk = 27000000,
344 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
345 SOR_PLL_VCOCAP(0) | SOR_PLL_RESISTORSEL,
346 .pll1 = SOR_PLL_LOADADJ(3) | SOR_PLL_TMDS_TERMADJ(0),
347 .pe_current = PE_CURRENT0(PE_CURRENT_0_mA_T114) |
348 PE_CURRENT1(PE_CURRENT_0_mA_T114) |
349 PE_CURRENT2(PE_CURRENT_0_mA_T114) |
350 PE_CURRENT3(PE_CURRENT_0_mA_T114),
351 .drive_current =
352 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_10_400_mA_T114) |
353 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_10_400_mA_T114) |
354 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_10_400_mA_T114) |
355 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_10_400_mA_T114),
356 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_0_000_mA) |
357 PEAK_CURRENT_LANE1(PEAK_CURRENT_0_000_mA) |
358 PEAK_CURRENT_LANE2(PEAK_CURRENT_0_000_mA) |
359 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_000_mA),
360 }, { /* 720p / 74.25MHz modes */
361 .pclk = 74250000,
362 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
363 SOR_PLL_VCOCAP(1) | SOR_PLL_RESISTORSEL,
364 .pll1 = SOR_PLL_PE_EN | SOR_PLL_LOADADJ(3) |
365 SOR_PLL_TMDS_TERMADJ(0),
366 .pe_current = PE_CURRENT0(PE_CURRENT_15_mA_T114) |
367 PE_CURRENT1(PE_CURRENT_15_mA_T114) |
368 PE_CURRENT2(PE_CURRENT_15_mA_T114) |
369 PE_CURRENT3(PE_CURRENT_15_mA_T114),
370 .drive_current =
371 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_10_400_mA_T114) |
372 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_10_400_mA_T114) |
373 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_10_400_mA_T114) |
374 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_10_400_mA_T114),
375 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_0_000_mA) |
376 PEAK_CURRENT_LANE1(PEAK_CURRENT_0_000_mA) |
377 PEAK_CURRENT_LANE2(PEAK_CURRENT_0_000_mA) |
378 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_000_mA),
379 }, { /* 1080p / 148.5MHz modes */
380 .pclk = 148500000,
381 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
382 SOR_PLL_VCOCAP(3) | SOR_PLL_RESISTORSEL,
383 .pll1 = SOR_PLL_PE_EN | SOR_PLL_LOADADJ(3) |
384 SOR_PLL_TMDS_TERMADJ(0),
385 .pe_current = PE_CURRENT0(PE_CURRENT_10_mA_T114) |
386 PE_CURRENT1(PE_CURRENT_10_mA_T114) |
387 PE_CURRENT2(PE_CURRENT_10_mA_T114) |
388 PE_CURRENT3(PE_CURRENT_10_mA_T114),
389 .drive_current =
390 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_12_400_mA_T114) |
391 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_12_400_mA_T114) |
392 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_12_400_mA_T114) |
393 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_12_400_mA_T114),
394 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_0_000_mA) |
395 PEAK_CURRENT_LANE1(PEAK_CURRENT_0_000_mA) |
396 PEAK_CURRENT_LANE2(PEAK_CURRENT_0_000_mA) |
397 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_000_mA),
398 }, { /* 225/297MHz modes */
399 .pclk = UINT_MAX,
400 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
401 SOR_PLL_VCOCAP(0xf) | SOR_PLL_RESISTORSEL,
402 .pll1 = SOR_PLL_LOADADJ(3) | SOR_PLL_TMDS_TERMADJ(7)
403 | SOR_PLL_TMDS_TERM_ENABLE,
404 .pe_current = PE_CURRENT0(PE_CURRENT_0_mA_T114) |
405 PE_CURRENT1(PE_CURRENT_0_mA_T114) |
406 PE_CURRENT2(PE_CURRENT_0_mA_T114) |
407 PE_CURRENT3(PE_CURRENT_0_mA_T114),
408 .drive_current =
409 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_25_200_mA_T114) |
410 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_25_200_mA_T114) |
411 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_25_200_mA_T114) |
412 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_19_200_mA_T114),
413 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_3_000_mA) |
414 PEAK_CURRENT_LANE1(PEAK_CURRENT_3_000_mA) |
415 PEAK_CURRENT_LANE2(PEAK_CURRENT_3_000_mA) |
416 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_800_mA),
417 },
418};
419
Thierry Redingedec4af2012-11-15 21:28:23 +0000420static const struct tegra_hdmi_audio_config *
Thierry Reding2ccb3962015-01-15 13:43:18 +0100421tegra_hdmi_get_audio_config(unsigned int sample_rate, unsigned int pclk)
Thierry Redingedec4af2012-11-15 21:28:23 +0000422{
423 const struct tegra_hdmi_audio_config *table;
424
Thierry Reding2ccb3962015-01-15 13:43:18 +0100425 switch (sample_rate) {
Thierry Redingedec4af2012-11-15 21:28:23 +0000426 case 32000:
427 table = tegra_hdmi_audio_32k;
428 break;
429
430 case 44100:
431 table = tegra_hdmi_audio_44_1k;
432 break;
433
434 case 48000:
435 table = tegra_hdmi_audio_48k;
436 break;
437
438 case 88200:
439 table = tegra_hdmi_audio_88_2k;
440 break;
441
442 case 96000:
443 table = tegra_hdmi_audio_96k;
444 break;
445
446 case 176400:
447 table = tegra_hdmi_audio_176_4k;
448 break;
449
450 case 192000:
451 table = tegra_hdmi_audio_192k;
452 break;
453
454 default:
455 return NULL;
456 }
457
458 while (table->pclk) {
459 if (table->pclk == pclk)
460 return table;
461
462 table++;
463 }
464
465 return NULL;
466}
467
468static void tegra_hdmi_setup_audio_fs_tables(struct tegra_hdmi *hdmi)
469{
470 const unsigned int freqs[] = {
471 32000, 44100, 48000, 88200, 96000, 176400, 192000
472 };
473 unsigned int i;
474
475 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
476 unsigned int f = freqs[i];
477 unsigned int eight_half;
Thierry Redingedec4af2012-11-15 21:28:23 +0000478 unsigned int delta;
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100479 u32 value;
Thierry Redingedec4af2012-11-15 21:28:23 +0000480
481 if (f > 96000)
482 delta = 2;
Thierry Reding17a8b6b2013-12-16 10:01:24 +0100483 else if (f > 48000)
Thierry Redingedec4af2012-11-15 21:28:23 +0000484 delta = 6;
485 else
486 delta = 9;
487
488 eight_half = (8 * HDMI_AUDIOCLK_FREQ) / (f * 128);
489 value = AUDIO_FS_LOW(eight_half - delta) |
490 AUDIO_FS_HIGH(eight_half + delta);
491 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_AUDIO_FS(i));
492 }
493}
494
Thierry Reding2ccb3962015-01-15 13:43:18 +0100495static void tegra_hdmi_write_aval(struct tegra_hdmi *hdmi, u32 value)
Thierry Redingedec4af2012-11-15 21:28:23 +0000496{
Thierry Reding2ccb3962015-01-15 13:43:18 +0100497 static const struct {
498 unsigned int sample_rate;
499 unsigned int offset;
500 } regs[] = {
501 { 32000, HDMI_NV_PDISP_SOR_AUDIO_AVAL_0320 },
502 { 44100, HDMI_NV_PDISP_SOR_AUDIO_AVAL_0441 },
503 { 48000, HDMI_NV_PDISP_SOR_AUDIO_AVAL_0480 },
504 { 88200, HDMI_NV_PDISP_SOR_AUDIO_AVAL_0882 },
505 { 96000, HDMI_NV_PDISP_SOR_AUDIO_AVAL_0960 },
506 { 176400, HDMI_NV_PDISP_SOR_AUDIO_AVAL_1764 },
507 { 192000, HDMI_NV_PDISP_SOR_AUDIO_AVAL_1920 },
508 };
509 unsigned int i;
510
511 for (i = 0; i < ARRAY_SIZE(regs); i++) {
512 if (regs[i].sample_rate == hdmi->audio_sample_rate) {
513 tegra_hdmi_writel(hdmi, value, regs[i].offset);
514 break;
515 }
516 }
517}
518
519static int tegra_hdmi_setup_audio(struct tegra_hdmi *hdmi)
520{
Thierry Redingedec4af2012-11-15 21:28:23 +0000521 const struct tegra_hdmi_audio_config *config;
Thierry Reding2ccb3962015-01-15 13:43:18 +0100522 u32 source, value;
Thierry Redingedec4af2012-11-15 21:28:23 +0000523
524 switch (hdmi->audio_source) {
525 case HDA:
Thierry Reding2ccb3962015-01-15 13:43:18 +0100526 if (hdmi->config->has_hda)
527 source = SOR_AUDIO_CNTRL0_SOURCE_SELECT_HDAL;
528 else
529 return -EINVAL;
530
Thierry Redingedec4af2012-11-15 21:28:23 +0000531 break;
532
533 case SPDIF:
Thierry Reding2ccb3962015-01-15 13:43:18 +0100534 if (hdmi->config->has_hda)
535 source = SOR_AUDIO_CNTRL0_SOURCE_SELECT_SPDIF;
536 else
537 source = AUDIO_CNTRL0_SOURCE_SELECT_SPDIF;
Thierry Redingedec4af2012-11-15 21:28:23 +0000538 break;
539
540 default:
Thierry Reding2ccb3962015-01-15 13:43:18 +0100541 if (hdmi->config->has_hda)
542 source = SOR_AUDIO_CNTRL0_SOURCE_SELECT_AUTO;
543 else
544 source = AUDIO_CNTRL0_SOURCE_SELECT_AUTO;
Thierry Redingedec4af2012-11-15 21:28:23 +0000545 break;
546 }
547
Thierry Reding2ccb3962015-01-15 13:43:18 +0100548 /*
549 * Tegra30 and later use a slightly modified version of the register
550 * layout to accomodate for changes related to supporting HDA as the
551 * audio input source for HDMI. The source select field has moved to
552 * the SOR_AUDIO_CNTRL0 register, but the error tolerance and frames
553 * per block fields remain in the AUDIO_CNTRL0 register.
554 */
555 if (hdmi->config->has_hda) {
556 /*
557 * Inject null samples into the audio FIFO for every frame in
558 * which the codec did not receive any samples. This applies
559 * to stereo LPCM only.
560 *
561 * XXX: This seems to be a remnant of MCP days when this was
562 * used to work around issues with monitors not being able to
563 * play back system startup sounds early. It is possibly not
564 * needed on Linux at all.
565 */
566 if (hdmi->audio_channels == 2)
567 value = SOR_AUDIO_CNTRL0_INJECT_NULLSMPL;
568 else
569 value = 0;
Thierry Redingedec4af2012-11-15 21:28:23 +0000570
Thierry Reding2ccb3962015-01-15 13:43:18 +0100571 value |= source;
572
573 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_AUDIO_CNTRL0);
Thierry Redingedec4af2012-11-15 21:28:23 +0000574 }
575
Thierry Reding2ccb3962015-01-15 13:43:18 +0100576 /*
577 * On Tegra20, HDA is not a supported audio source and the source
578 * select field is part of the AUDIO_CNTRL0 register.
579 */
580 value = AUDIO_CNTRL0_FRAMES_PER_BLOCK(0xc0) |
581 AUDIO_CNTRL0_ERROR_TOLERANCE(6);
582
583 if (!hdmi->config->has_hda)
584 value |= source;
585
586 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_AUDIO_CNTRL0);
587
588 /*
589 * Advertise support for High Bit-Rate on Tegra114 and later.
590 */
591 if (hdmi->config->has_hbr) {
592 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_SOR_AUDIO_SPARE0);
593 value |= SOR_AUDIO_SPARE0_HBR_ENABLE;
594 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_AUDIO_SPARE0);
595 }
596
597 config = tegra_hdmi_get_audio_config(hdmi->audio_sample_rate,
598 hdmi->pixel_clock);
Thierry Redingedec4af2012-11-15 21:28:23 +0000599 if (!config) {
Thierry Reding2ccb3962015-01-15 13:43:18 +0100600 dev_err(hdmi->dev,
601 "cannot set audio to %u Hz at %u Hz pixel clock\n",
602 hdmi->audio_sample_rate, hdmi->pixel_clock);
Thierry Redingedec4af2012-11-15 21:28:23 +0000603 return -EINVAL;
604 }
605
606 tegra_hdmi_writel(hdmi, 0, HDMI_NV_PDISP_HDMI_ACR_CTRL);
607
608 value = AUDIO_N_RESETF | AUDIO_N_GENERATE_ALTERNATE |
609 AUDIO_N_VALUE(config->n - 1);
610 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_AUDIO_N);
611
612 tegra_hdmi_writel(hdmi, ACR_SUBPACK_N(config->n) | ACR_ENABLE,
613 HDMI_NV_PDISP_HDMI_ACR_0441_SUBPACK_HIGH);
614
Thierry Reding2ccb3962015-01-15 13:43:18 +0100615 tegra_hdmi_writel(hdmi, ACR_SUBPACK_CTS(config->cts),
616 HDMI_NV_PDISP_HDMI_ACR_0441_SUBPACK_LOW);
Thierry Redingedec4af2012-11-15 21:28:23 +0000617
618 value = SPARE_HW_CTS | SPARE_FORCE_SW_CTS | SPARE_CTS_RESET_VAL(1);
619 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_SPARE);
620
621 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_AUDIO_N);
622 value &= ~AUDIO_N_RESETF;
623 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_AUDIO_N);
624
Thierry Reding2ccb3962015-01-15 13:43:18 +0100625 if (hdmi->config->has_hda)
626 tegra_hdmi_write_aval(hdmi, config->aval);
Thierry Redingedec4af2012-11-15 21:28:23 +0000627
628 tegra_hdmi_setup_audio_fs_tables(hdmi);
629
630 return 0;
631}
632
Thierry Reding2ccb3962015-01-15 13:43:18 +0100633static void tegra_hdmi_disable_audio(struct tegra_hdmi *hdmi)
634{
635 u32 value;
636
637 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
638 value &= ~GENERIC_CTRL_AUDIO;
639 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
640}
641
642static void tegra_hdmi_enable_audio(struct tegra_hdmi *hdmi)
643{
644 u32 value;
645
646 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
647 value |= GENERIC_CTRL_AUDIO;
648 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
649}
650
Thierry Reding52345492015-08-07 16:00:43 +0200651static void tegra_hdmi_write_eld(struct tegra_hdmi *hdmi)
652{
653 size_t length = drm_eld_size(hdmi->output.connector.eld), i;
654 u32 value;
655
656 for (i = 0; i < length; i++)
657 tegra_hdmi_writel(hdmi, i << 8 | hdmi->output.connector.eld[i],
658 HDMI_NV_PDISP_SOR_AUDIO_HDA_ELD_BUFWR);
659
660 /*
661 * The HDA codec will always report an ELD buffer size of 96 bytes and
662 * the HDA codec driver will check that each byte read from the buffer
663 * is valid. Therefore every byte must be written, even if no 96 bytes
664 * were parsed from EDID.
665 */
666 for (i = length; i < HDMI_ELD_BUFFER_SIZE; i++)
667 tegra_hdmi_writel(hdmi, i << 8 | 0,
668 HDMI_NV_PDISP_SOR_AUDIO_HDA_ELD_BUFWR);
669
670 value = SOR_AUDIO_HDA_PRESENSE_VALID | SOR_AUDIO_HDA_PRESENSE_PRESENT;
671 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_AUDIO_HDA_PRESENSE);
672}
673
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100674static inline u32 tegra_hdmi_subpack(const u8 *ptr, size_t size)
Thierry Redingedec4af2012-11-15 21:28:23 +0000675{
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100676 u32 value = 0;
Thierry Redingedec4af2012-11-15 21:28:23 +0000677 size_t i;
Thierry Redingedec4af2012-11-15 21:28:23 +0000678
Thierry Redingac24c222012-11-23 15:14:00 +0100679 for (i = size; i > 0; i--)
680 value = (value << 8) | ptr[i - 1];
Thierry Redingedec4af2012-11-15 21:28:23 +0000681
Thierry Redingac24c222012-11-23 15:14:00 +0100682 return value;
683}
Thierry Redingedec4af2012-11-15 21:28:23 +0000684
Thierry Redingac24c222012-11-23 15:14:00 +0100685static void tegra_hdmi_write_infopack(struct tegra_hdmi *hdmi, const void *data,
686 size_t size)
687{
688 const u8 *ptr = data;
689 unsigned long offset;
Thierry Redingac24c222012-11-23 15:14:00 +0100690 size_t i, j;
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100691 u32 value;
Thierry Redingedec4af2012-11-15 21:28:23 +0000692
Thierry Redingac24c222012-11-23 15:14:00 +0100693 switch (ptr[0]) {
694 case HDMI_INFOFRAME_TYPE_AVI:
695 offset = HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_HEADER;
696 break;
697
698 case HDMI_INFOFRAME_TYPE_AUDIO:
699 offset = HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_HEADER;
700 break;
701
702 case HDMI_INFOFRAME_TYPE_VENDOR:
703 offset = HDMI_NV_PDISP_HDMI_GENERIC_HEADER;
704 break;
705
706 default:
707 dev_err(hdmi->dev, "unsupported infoframe type: %02x\n",
708 ptr[0]);
709 return;
710 }
711
712 value = INFOFRAME_HEADER_TYPE(ptr[0]) |
713 INFOFRAME_HEADER_VERSION(ptr[1]) |
714 INFOFRAME_HEADER_LEN(ptr[2]);
Thierry Redingedec4af2012-11-15 21:28:23 +0000715 tegra_hdmi_writel(hdmi, value, offset);
Thierry Redingac24c222012-11-23 15:14:00 +0100716 offset++;
Thierry Redingedec4af2012-11-15 21:28:23 +0000717
Thierry Redingac24c222012-11-23 15:14:00 +0100718 /*
719 * Each subpack contains 7 bytes, divided into:
720 * - subpack_low: bytes 0 - 3
721 * - subpack_high: bytes 4 - 6 (with byte 7 padded to 0x00)
Thierry Redingedec4af2012-11-15 21:28:23 +0000722 */
Thierry Redingac24c222012-11-23 15:14:00 +0100723 for (i = 3, j = 0; i < size; i += 7, j += 8) {
724 size_t rem = size - i, num = min_t(size_t, rem, 4);
Thierry Redingedec4af2012-11-15 21:28:23 +0000725
Thierry Redingac24c222012-11-23 15:14:00 +0100726 value = tegra_hdmi_subpack(&ptr[i], num);
727 tegra_hdmi_writel(hdmi, value, offset++);
Thierry Redingedec4af2012-11-15 21:28:23 +0000728
Thierry Redingac24c222012-11-23 15:14:00 +0100729 num = min_t(size_t, rem - num, 3);
Thierry Redingedec4af2012-11-15 21:28:23 +0000730
Thierry Redingac24c222012-11-23 15:14:00 +0100731 value = tegra_hdmi_subpack(&ptr[i + 4], num);
732 tegra_hdmi_writel(hdmi, value, offset++);
Thierry Redingedec4af2012-11-15 21:28:23 +0000733 }
734}
735
736static void tegra_hdmi_setup_avi_infoframe(struct tegra_hdmi *hdmi,
737 struct drm_display_mode *mode)
738{
739 struct hdmi_avi_infoframe frame;
Thierry Redingac24c222012-11-23 15:14:00 +0100740 u8 buffer[17];
741 ssize_t err;
Thierry Redingedec4af2012-11-15 21:28:23 +0000742
Thierry Redingac24c222012-11-23 15:14:00 +0100743 err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
744 if (err < 0) {
745 dev_err(hdmi->dev, "failed to setup AVI infoframe: %zd\n", err);
746 return;
Thierry Redingedec4af2012-11-15 21:28:23 +0000747 }
748
Thierry Redingac24c222012-11-23 15:14:00 +0100749 err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
750 if (err < 0) {
751 dev_err(hdmi->dev, "failed to pack AVI infoframe: %zd\n", err);
752 return;
753 }
754
755 tegra_hdmi_write_infopack(hdmi, buffer, err);
Thierry Reding2ccb3962015-01-15 13:43:18 +0100756}
Thierry Redingedec4af2012-11-15 21:28:23 +0000757
Thierry Reding2ccb3962015-01-15 13:43:18 +0100758static void tegra_hdmi_disable_avi_infoframe(struct tegra_hdmi *hdmi)
759{
760 u32 value;
761
762 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_CTRL);
763 value &= ~INFOFRAME_CTRL_ENABLE;
764 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_CTRL);
765}
766
767static void tegra_hdmi_enable_avi_infoframe(struct tegra_hdmi *hdmi)
768{
769 u32 value;
770
771 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_CTRL);
772 value |= INFOFRAME_CTRL_ENABLE;
773 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_CTRL);
Thierry Redingedec4af2012-11-15 21:28:23 +0000774}
775
776static void tegra_hdmi_setup_audio_infoframe(struct tegra_hdmi *hdmi)
777{
778 struct hdmi_audio_infoframe frame;
Thierry Redingac24c222012-11-23 15:14:00 +0100779 u8 buffer[14];
780 ssize_t err;
Thierry Redingedec4af2012-11-15 21:28:23 +0000781
Thierry Redingac24c222012-11-23 15:14:00 +0100782 err = hdmi_audio_infoframe_init(&frame);
783 if (err < 0) {
Thierry Redingef284c72013-10-16 19:51:22 +0200784 dev_err(hdmi->dev, "failed to setup audio infoframe: %zd\n",
Thierry Redingac24c222012-11-23 15:14:00 +0100785 err);
786 return;
787 }
Thierry Redingedec4af2012-11-15 21:28:23 +0000788
Thierry Reding2ccb3962015-01-15 13:43:18 +0100789 frame.channels = hdmi->audio_channels;
Thierry Redingac24c222012-11-23 15:14:00 +0100790
791 err = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
792 if (err < 0) {
793 dev_err(hdmi->dev, "failed to pack audio infoframe: %zd\n",
794 err);
795 return;
796 }
797
798 /*
799 * The audio infoframe has only one set of subpack registers, so the
800 * infoframe needs to be truncated. One set of subpack registers can
801 * contain 7 bytes. Including the 3 byte header only the first 10
802 * bytes can be programmed.
803 */
Thierry Redingef284c72013-10-16 19:51:22 +0200804 tegra_hdmi_write_infopack(hdmi, buffer, min_t(size_t, 10, err));
Thierry Reding2ccb3962015-01-15 13:43:18 +0100805}
Thierry Redingedec4af2012-11-15 21:28:23 +0000806
Thierry Reding2ccb3962015-01-15 13:43:18 +0100807static void tegra_hdmi_disable_audio_infoframe(struct tegra_hdmi *hdmi)
808{
809 u32 value;
810
811 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_CTRL);
812 value &= ~INFOFRAME_CTRL_ENABLE;
813 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_CTRL);
814}
815
816static void tegra_hdmi_enable_audio_infoframe(struct tegra_hdmi *hdmi)
817{
818 u32 value;
819
820 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_CTRL);
821 value |= INFOFRAME_CTRL_ENABLE;
822 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_CTRL);
Thierry Redingedec4af2012-11-15 21:28:23 +0000823}
824
825static void tegra_hdmi_setup_stereo_infoframe(struct tegra_hdmi *hdmi)
826{
Lespiau, Damienae84b902013-08-19 16:59:02 +0100827 struct hdmi_vendor_infoframe frame;
Thierry Redingac24c222012-11-23 15:14:00 +0100828 u8 buffer[10];
829 ssize_t err;
Thierry Redingedec4af2012-11-15 21:28:23 +0000830
Lespiau, Damienae84b902013-08-19 16:59:02 +0100831 hdmi_vendor_infoframe_init(&frame);
Lespiau, Damiena26a58e82013-08-19 16:58:59 +0100832 frame.s3d_struct = HDMI_3D_STRUCTURE_FRAME_PACKING;
Thierry Redingac24c222012-11-23 15:14:00 +0100833
Lespiau, Damienae84b902013-08-19 16:59:02 +0100834 err = hdmi_vendor_infoframe_pack(&frame, buffer, sizeof(buffer));
Thierry Redingac24c222012-11-23 15:14:00 +0100835 if (err < 0) {
836 dev_err(hdmi->dev, "failed to pack vendor infoframe: %zd\n",
837 err);
838 return;
839 }
840
841 tegra_hdmi_write_infopack(hdmi, buffer, err);
Thierry Reding2ccb3962015-01-15 13:43:18 +0100842}
843
844static void tegra_hdmi_disable_stereo_infoframe(struct tegra_hdmi *hdmi)
845{
846 u32 value;
847
848 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
849 value &= ~GENERIC_CTRL_ENABLE;
850 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
851}
852
853static void tegra_hdmi_enable_stereo_infoframe(struct tegra_hdmi *hdmi)
854{
855 u32 value;
Thierry Redingedec4af2012-11-15 21:28:23 +0000856
857 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
858 value |= GENERIC_CTRL_ENABLE;
859 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
860}
861
862static void tegra_hdmi_setup_tmds(struct tegra_hdmi *hdmi,
863 const struct tmds_config *tmds)
864{
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100865 u32 value;
Thierry Redingedec4af2012-11-15 21:28:23 +0000866
867 tegra_hdmi_writel(hdmi, tmds->pll0, HDMI_NV_PDISP_SOR_PLL0);
868 tegra_hdmi_writel(hdmi, tmds->pll1, HDMI_NV_PDISP_SOR_PLL1);
869 tegra_hdmi_writel(hdmi, tmds->pe_current, HDMI_NV_PDISP_PE_CURRENT);
870
Thierry Reding59af0592013-10-14 09:43:05 +0200871 tegra_hdmi_writel(hdmi, tmds->drive_current,
872 HDMI_NV_PDISP_SOR_LANE_DRIVE_CURRENT);
873
874 value = tegra_hdmi_readl(hdmi, hdmi->config->fuse_override_offset);
875 value |= hdmi->config->fuse_override_value;
876 tegra_hdmi_writel(hdmi, value, hdmi->config->fuse_override_offset);
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +0200877
878 if (hdmi->config->has_sor_io_peak_current)
879 tegra_hdmi_writel(hdmi, tmds->peak_current,
880 HDMI_NV_PDISP_SOR_IO_PEAK_CURRENT);
Thierry Redingedec4af2012-11-15 21:28:23 +0000881}
882
Mikko Perttunen9f159122013-08-28 18:48:38 +0300883static bool tegra_output_is_hdmi(struct tegra_output *output)
884{
885 struct edid *edid;
886
887 if (!output->connector.edid_blob_ptr)
888 return false;
889
890 edid = (struct edid *)output->connector.edid_blob_ptr->data;
891
892 return drm_detect_hdmi_monitor(edid);
893}
894
Thierry Reding2ccb3962015-01-15 13:43:18 +0100895static enum drm_connector_status
896tegra_hdmi_connector_detect(struct drm_connector *connector, bool force)
897{
898 struct tegra_output *output = connector_to_output(connector);
899 struct tegra_hdmi *hdmi = to_hdmi(output);
900 enum drm_connector_status status;
901
902 status = tegra_output_connector_detect(connector, force);
903 if (status == connector_status_connected)
904 return status;
905
906 tegra_hdmi_writel(hdmi, 0, HDMI_NV_PDISP_SOR_AUDIO_HDA_PRESENSE);
907 return status;
908}
909
Thierry Reding59682712014-11-28 16:50:59 +0100910static const struct drm_connector_funcs tegra_hdmi_connector_funcs = {
Thierry Reding29871b22015-07-29 09:46:40 +0200911 .dpms = drm_atomic_helper_connector_dpms,
Thierry Reding9d441892014-11-24 17:02:53 +0100912 .reset = drm_atomic_helper_connector_reset,
Thierry Reding2ccb3962015-01-15 13:43:18 +0100913 .detect = tegra_hdmi_connector_detect,
Thierry Reding59682712014-11-28 16:50:59 +0100914 .fill_modes = drm_helper_probe_single_connector_modes,
915 .destroy = tegra_output_connector_destroy,
Thierry Reding9d441892014-11-24 17:02:53 +0100916 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
Thierry Reding4aa3df72014-11-24 16:27:13 +0100917 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Thierry Reding59682712014-11-28 16:50:59 +0100918};
919
920static enum drm_mode_status
921tegra_hdmi_connector_mode_valid(struct drm_connector *connector,
922 struct drm_display_mode *mode)
923{
924 struct tegra_output *output = connector_to_output(connector);
925 struct tegra_hdmi *hdmi = to_hdmi(output);
926 unsigned long pclk = mode->clock * 1000;
927 enum drm_mode_status status = MODE_OK;
928 struct clk *parent;
929 long err;
930
931 parent = clk_get_parent(hdmi->clk_parent);
932
933 err = clk_round_rate(parent, pclk * 4);
934 if (err <= 0)
935 status = MODE_NOCLOCK;
936
937 return status;
938}
939
940static const struct drm_connector_helper_funcs
941tegra_hdmi_connector_helper_funcs = {
942 .get_modes = tegra_output_connector_get_modes,
943 .mode_valid = tegra_hdmi_connector_mode_valid,
Thierry Reding59682712014-11-28 16:50:59 +0100944};
945
946static const struct drm_encoder_funcs tegra_hdmi_encoder_funcs = {
947 .destroy = tegra_output_encoder_destroy,
948};
949
Thierry Reding29871b22015-07-29 09:46:40 +0200950static void tegra_hdmi_encoder_disable(struct drm_encoder *encoder)
Thierry Reding59682712014-11-28 16:50:59 +0100951{
Thierry Reding2ccb3962015-01-15 13:43:18 +0100952 struct tegra_output *output = encoder_to_output(encoder);
Thierry Reding29871b22015-07-29 09:46:40 +0200953 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
Thierry Reding2ccb3962015-01-15 13:43:18 +0100954 struct tegra_hdmi *hdmi = to_hdmi(output);
Thierry Reding29871b22015-07-29 09:46:40 +0200955 u32 value;
956
957 /*
958 * The following accesses registers of the display controller, so make
959 * sure it's only executed when the output is attached to one.
960 */
961 if (dc) {
962 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
963 value &= ~HDMI_ENABLE;
964 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
965
966 tegra_dc_commit(dc);
967 }
Thierry Reding2ccb3962015-01-15 13:43:18 +0100968
969 if (!hdmi->dvi) {
970 if (hdmi->stereo)
971 tegra_hdmi_disable_stereo_infoframe(hdmi);
972
973 tegra_hdmi_disable_audio_infoframe(hdmi);
974 tegra_hdmi_disable_avi_infoframe(hdmi);
975 tegra_hdmi_disable_audio(hdmi);
976 }
Thierry Reding2ccb3962015-01-15 13:43:18 +0100977
Thierry Reding52345492015-08-07 16:00:43 +0200978 tegra_hdmi_writel(hdmi, 0, HDMI_NV_PDISP_INT_ENABLE);
979 tegra_hdmi_writel(hdmi, 0, HDMI_NV_PDISP_INT_MASK);
Thierry Reding2ccb3962015-01-15 13:43:18 +0100980
Thierry Reding52345492015-08-07 16:00:43 +0200981 pm_runtime_put(hdmi->dev);
Thierry Reding59682712014-11-28 16:50:59 +0100982}
983
Thierry Reding29871b22015-07-29 09:46:40 +0200984static void tegra_hdmi_encoder_enable(struct drm_encoder *encoder)
Thierry Reding59682712014-11-28 16:50:59 +0100985{
Thierry Reding29871b22015-07-29 09:46:40 +0200986 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
Thierry Redingedec4af2012-11-15 21:28:23 +0000987 unsigned int h_sync_width, h_front_porch, h_back_porch, i, rekey;
Thierry Reding59682712014-11-28 16:50:59 +0100988 struct tegra_output *output = encoder_to_output(encoder);
989 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
Thierry Redingedec4af2012-11-15 21:28:23 +0000990 struct tegra_hdmi *hdmi = to_hdmi(output);
Thierry Reding2ccb3962015-01-15 13:43:18 +0100991 unsigned int pulse_start, div82;
Thierry Redingedec4af2012-11-15 21:28:23 +0000992 int retries = 1000;
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100993 u32 value;
Thierry Redingedec4af2012-11-15 21:28:23 +0000994 int err;
995
Thierry Reding52345492015-08-07 16:00:43 +0200996 pm_runtime_get_sync(hdmi->dev);
Mikko Perttunen9f159122013-08-28 18:48:38 +0300997
Thierry Reding52345492015-08-07 16:00:43 +0200998 /*
999 * Enable and unmask the HDA codec SCRATCH0 register interrupt. This
1000 * is used for interoperability between the HDA codec driver and the
1001 * HDMI driver.
1002 */
1003 tegra_hdmi_writel(hdmi, INT_CODEC_SCRATCH0, HDMI_NV_PDISP_INT_ENABLE);
1004 tegra_hdmi_writel(hdmi, INT_CODEC_SCRATCH0, HDMI_NV_PDISP_INT_MASK);
1005
Thierry Reding2ccb3962015-01-15 13:43:18 +01001006 hdmi->pixel_clock = mode->clock * 1000;
Thierry Redingedec4af2012-11-15 21:28:23 +00001007 h_sync_width = mode->hsync_end - mode->hsync_start;
Lucas Stach40495082012-12-19 21:38:52 +00001008 h_back_porch = mode->htotal - mode->hsync_end;
1009 h_front_porch = mode->hsync_start - mode->hdisplay;
Thierry Redingedec4af2012-11-15 21:28:23 +00001010
Thierry Reding2ccb3962015-01-15 13:43:18 +01001011 err = clk_set_rate(hdmi->clk, hdmi->pixel_clock);
Thierry Redingc03bf1bf2015-02-18 10:34:08 +01001012 if (err < 0) {
1013 dev_err(hdmi->dev, "failed to set HDMI clock frequency: %d\n",
1014 err);
1015 }
1016
1017 DRM_DEBUG_KMS("HDMI clock rate: %lu Hz\n", clk_get_rate(hdmi->clk));
1018
Thierry Reding8c8282c2014-04-16 10:46:24 +02001019 /* power up sequence */
1020 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_SOR_PLL0);
1021 value &= ~SOR_PLL_PDBG;
1022 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_PLL0);
1023
1024 usleep_range(10, 20);
1025
1026 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_SOR_PLL0);
1027 value &= ~SOR_PLL_PWR;
1028 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_PLL0);
1029
Thierry Redingedec4af2012-11-15 21:28:23 +00001030 tegra_dc_writel(dc, VSYNC_H_POSITION(1),
1031 DC_DISP_DISP_TIMING_OPTIONS);
Thierry Reding472a6d12015-08-05 16:39:55 +02001032 tegra_dc_writel(dc, DITHER_CONTROL_DISABLE | BASE_COLOR_SIZE_888,
Thierry Redingedec4af2012-11-15 21:28:23 +00001033 DC_DISP_DISP_COLOR_CONTROL);
1034
1035 /* video_preamble uses h_pulse2 */
1036 pulse_start = 1 + h_sync_width + h_back_porch - 10;
1037
Thierry Reding8fd3ffa2015-04-27 14:48:35 +02001038 tegra_dc_writel(dc, H_PULSE2_ENABLE, DC_DISP_DISP_SIGNAL_OPTIONS0);
Thierry Redingedec4af2012-11-15 21:28:23 +00001039
1040 value = PULSE_MODE_NORMAL | PULSE_POLARITY_HIGH | PULSE_QUAL_VACTIVE |
1041 PULSE_LAST_END_A;
1042 tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_CONTROL);
1043
1044 value = PULSE_START(pulse_start) | PULSE_END(pulse_start + 8);
1045 tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_POSITION_A);
1046
1047 value = VSYNC_WINDOW_END(0x210) | VSYNC_WINDOW_START(0x200) |
1048 VSYNC_WINDOW_ENABLE;
1049 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_VSYNC_WINDOW);
1050
1051 if (dc->pipe)
1052 value = HDMI_SRC_DISPLAYB;
1053 else
1054 value = HDMI_SRC_DISPLAYA;
1055
1056 if ((mode->hdisplay == 720) && ((mode->vdisplay == 480) ||
1057 (mode->vdisplay == 576)))
1058 tegra_hdmi_writel(hdmi,
1059 value | ARM_VIDEO_RANGE_FULL,
1060 HDMI_NV_PDISP_INPUT_CONTROL);
1061 else
1062 tegra_hdmi_writel(hdmi,
1063 value | ARM_VIDEO_RANGE_LIMITED,
1064 HDMI_NV_PDISP_INPUT_CONTROL);
1065
1066 div82 = clk_get_rate(hdmi->clk) / 1000000 * 4;
1067 value = SOR_REFCLK_DIV_INT(div82 >> 2) | SOR_REFCLK_DIV_FRAC(div82);
1068 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_REFCLK);
1069
Thierry Reding2ccb3962015-01-15 13:43:18 +01001070 hdmi->dvi = !tegra_output_is_hdmi(output);
Thierry Redingedec4af2012-11-15 21:28:23 +00001071 if (!hdmi->dvi) {
Thierry Reding2ccb3962015-01-15 13:43:18 +01001072 err = tegra_hdmi_setup_audio(hdmi);
Thierry Redingedec4af2012-11-15 21:28:23 +00001073 if (err < 0)
1074 hdmi->dvi = true;
1075 }
1076
Thierry Reding2ccb3962015-01-15 13:43:18 +01001077 if (hdmi->config->has_hda)
1078 tegra_hdmi_write_eld(hdmi);
Thierry Redingedec4af2012-11-15 21:28:23 +00001079
1080 rekey = HDMI_REKEY_DEFAULT;
1081 value = HDMI_CTRL_REKEY(rekey);
1082 value |= HDMI_CTRL_MAX_AC_PACKET((h_sync_width + h_back_porch +
1083 h_front_porch - rekey - 18) / 32);
1084
1085 if (!hdmi->dvi)
1086 value |= HDMI_CTRL_ENABLE;
1087
1088 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_CTRL);
1089
Thierry Reding2ccb3962015-01-15 13:43:18 +01001090 if (!hdmi->dvi) {
1091 tegra_hdmi_setup_avi_infoframe(hdmi, mode);
1092 tegra_hdmi_setup_audio_infoframe(hdmi);
Thierry Redingedec4af2012-11-15 21:28:23 +00001093
Thierry Reding2ccb3962015-01-15 13:43:18 +01001094 if (hdmi->stereo)
1095 tegra_hdmi_setup_stereo_infoframe(hdmi);
1096 }
Thierry Redingedec4af2012-11-15 21:28:23 +00001097
1098 /* TMDS CONFIG */
Thierry Reding59af0592013-10-14 09:43:05 +02001099 for (i = 0; i < hdmi->config->num_tmds; i++) {
Thierry Reding2ccb3962015-01-15 13:43:18 +01001100 if (hdmi->pixel_clock <= hdmi->config->tmds[i].pclk) {
Thierry Reding59af0592013-10-14 09:43:05 +02001101 tegra_hdmi_setup_tmds(hdmi, &hdmi->config->tmds[i]);
Thierry Redingedec4af2012-11-15 21:28:23 +00001102 break;
1103 }
1104 }
1105
1106 tegra_hdmi_writel(hdmi,
Thierry Reding5c1c0712015-01-28 16:32:52 +01001107 SOR_SEQ_PU_PC(0) |
Thierry Redingedec4af2012-11-15 21:28:23 +00001108 SOR_SEQ_PU_PC_ALT(0) |
1109 SOR_SEQ_PD_PC(8) |
1110 SOR_SEQ_PD_PC_ALT(8),
1111 HDMI_NV_PDISP_SOR_SEQ_CTL);
1112
1113 value = SOR_SEQ_INST_WAIT_TIME(1) |
1114 SOR_SEQ_INST_WAIT_UNITS_VSYNC |
1115 SOR_SEQ_INST_HALT |
1116 SOR_SEQ_INST_PIN_A_LOW |
1117 SOR_SEQ_INST_PIN_B_LOW |
1118 SOR_SEQ_INST_DRIVE_PWM_OUT_LO;
1119
1120 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_SEQ_INST(0));
1121 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_SEQ_INST(8));
1122
Thierry Reding9cbfc732014-04-16 10:47:36 +02001123 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_SOR_CSTM);
Thierry Redingedec4af2012-11-15 21:28:23 +00001124 value &= ~SOR_CSTM_ROTCLK(~0);
1125 value |= SOR_CSTM_ROTCLK(2);
Thierry Reding9cbfc732014-04-16 10:47:36 +02001126 value |= SOR_CSTM_PLLDIV;
1127 value &= ~SOR_CSTM_LVDS_ENABLE;
1128 value &= ~SOR_CSTM_MODE_MASK;
1129 value |= SOR_CSTM_MODE_TMDS;
Thierry Redingedec4af2012-11-15 21:28:23 +00001130 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_CSTM);
1131
Thierry Redingedec4af2012-11-15 21:28:23 +00001132 /* start SOR */
1133 tegra_hdmi_writel(hdmi,
1134 SOR_PWR_NORMAL_STATE_PU |
1135 SOR_PWR_NORMAL_START_NORMAL |
1136 SOR_PWR_SAFE_STATE_PD |
1137 SOR_PWR_SETTING_NEW_TRIGGER,
1138 HDMI_NV_PDISP_SOR_PWR);
1139 tegra_hdmi_writel(hdmi,
1140 SOR_PWR_NORMAL_STATE_PU |
1141 SOR_PWR_NORMAL_START_NORMAL |
1142 SOR_PWR_SAFE_STATE_PD |
1143 SOR_PWR_SETTING_NEW_DONE,
1144 HDMI_NV_PDISP_SOR_PWR);
1145
1146 do {
1147 BUG_ON(--retries < 0);
1148 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_SOR_PWR);
1149 } while (value & SOR_PWR_SETTING_NEW_PENDING);
1150
1151 value = SOR_STATE_ASY_CRCMODE_COMPLETE |
1152 SOR_STATE_ASY_OWNER_HEAD0 |
1153 SOR_STATE_ASY_SUBOWNER_BOTH |
1154 SOR_STATE_ASY_PROTOCOL_SINGLE_TMDS_A |
1155 SOR_STATE_ASY_DEPOL_POS;
1156
1157 /* setup sync polarities */
1158 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
1159 value |= SOR_STATE_ASY_HSYNCPOL_POS;
1160
1161 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1162 value |= SOR_STATE_ASY_HSYNCPOL_NEG;
1163
1164 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
1165 value |= SOR_STATE_ASY_VSYNCPOL_POS;
1166
1167 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1168 value |= SOR_STATE_ASY_VSYNCPOL_NEG;
1169
1170 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_STATE2);
1171
1172 value = SOR_STATE_ASY_HEAD_OPMODE_AWAKE | SOR_STATE_ASY_ORMODE_NORMAL;
1173 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_STATE1);
1174
1175 tegra_hdmi_writel(hdmi, 0, HDMI_NV_PDISP_SOR_STATE0);
1176 tegra_hdmi_writel(hdmi, SOR_STATE_UPDATE, HDMI_NV_PDISP_SOR_STATE0);
1177 tegra_hdmi_writel(hdmi, value | SOR_STATE_ATTACHED,
1178 HDMI_NV_PDISP_SOR_STATE1);
1179 tegra_hdmi_writel(hdmi, 0, HDMI_NV_PDISP_SOR_STATE0);
1180
Thierry Reding72d30282013-12-12 11:06:55 +01001181 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1182 value |= HDMI_ENABLE;
1183 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
Thierry Redingedec4af2012-11-15 21:28:23 +00001184
Thierry Reding62b9e062014-11-21 17:33:33 +01001185 tegra_dc_commit(dc);
Thierry Redingedec4af2012-11-15 21:28:23 +00001186
Thierry Reding2ccb3962015-01-15 13:43:18 +01001187 if (!hdmi->dvi) {
1188 tegra_hdmi_enable_avi_infoframe(hdmi);
1189 tegra_hdmi_enable_audio_infoframe(hdmi);
1190 tegra_hdmi_enable_audio(hdmi);
1191
1192 if (hdmi->stereo)
1193 tegra_hdmi_enable_stereo_infoframe(hdmi);
1194 }
1195
Thierry Redingedec4af2012-11-15 21:28:23 +00001196 /* TODO: add HDCP support */
Thierry Redingedec4af2012-11-15 21:28:23 +00001197}
1198
Thierry Redinga9825a62014-12-08 16:33:03 +01001199static int
1200tegra_hdmi_encoder_atomic_check(struct drm_encoder *encoder,
1201 struct drm_crtc_state *crtc_state,
1202 struct drm_connector_state *conn_state)
1203{
1204 struct tegra_output *output = encoder_to_output(encoder);
1205 struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
1206 unsigned long pclk = crtc_state->mode.clock * 1000;
1207 struct tegra_hdmi *hdmi = to_hdmi(output);
1208 int err;
1209
1210 err = tegra_dc_state_setup_clock(dc, crtc_state, hdmi->clk_parent,
1211 pclk, 0);
1212 if (err < 0) {
1213 dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
1214 return err;
1215 }
1216
1217 return err;
1218}
1219
Thierry Reding59682712014-11-28 16:50:59 +01001220static const struct drm_encoder_helper_funcs tegra_hdmi_encoder_helper_funcs = {
Thierry Reding59682712014-11-28 16:50:59 +01001221 .disable = tegra_hdmi_encoder_disable,
Thierry Reding29871b22015-07-29 09:46:40 +02001222 .enable = tegra_hdmi_encoder_enable,
Thierry Redinga9825a62014-12-08 16:33:03 +01001223 .atomic_check = tegra_hdmi_encoder_atomic_check,
Thierry Redingedec4af2012-11-15 21:28:23 +00001224};
1225
1226static int tegra_hdmi_show_regs(struct seq_file *s, void *data)
1227{
1228 struct drm_info_node *node = s->private;
1229 struct tegra_hdmi *hdmi = node->info_ent->data;
Thierry Reding29871b22015-07-29 09:46:40 +02001230 struct drm_crtc *crtc = hdmi->output.encoder.crtc;
1231 struct drm_device *drm = node->minor->dev;
1232 int err = 0;
Mikko Perttunenccaddfe2013-07-30 11:35:03 +03001233
Thierry Reding29871b22015-07-29 09:46:40 +02001234 drm_modeset_lock_all(drm);
1235
1236 if (!crtc || !crtc->state->active) {
1237 err = -EBUSY;
1238 goto unlock;
1239 }
Thierry Redingedec4af2012-11-15 21:28:23 +00001240
1241#define DUMP_REG(name) \
Thierry Reding4ee8cee2014-12-08 16:25:14 +01001242 seq_printf(s, "%-56s %#05x %08x\n", #name, name, \
1243 tegra_hdmi_readl(hdmi, name))
Thierry Redingedec4af2012-11-15 21:28:23 +00001244
1245 DUMP_REG(HDMI_CTXSW);
1246 DUMP_REG(HDMI_NV_PDISP_SOR_STATE0);
1247 DUMP_REG(HDMI_NV_PDISP_SOR_STATE1);
1248 DUMP_REG(HDMI_NV_PDISP_SOR_STATE2);
1249 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_AN_MSB);
1250 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_AN_LSB);
1251 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_CN_MSB);
1252 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_CN_LSB);
1253 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_AKSV_MSB);
1254 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_AKSV_LSB);
1255 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_BKSV_MSB);
1256 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_BKSV_LSB);
1257 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_CKSV_MSB);
1258 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_CKSV_LSB);
1259 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_DKSV_MSB);
1260 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_DKSV_LSB);
1261 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_CTRL);
1262 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_CMODE);
1263 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_MPRIME_MSB);
1264 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_MPRIME_LSB);
1265 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_SPRIME_MSB);
1266 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_SPRIME_LSB2);
1267 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_SPRIME_LSB1);
1268 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_RI);
1269 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_CS_MSB);
1270 DUMP_REG(HDMI_NV_PDISP_RG_HDCP_CS_LSB);
1271 DUMP_REG(HDMI_NV_PDISP_HDMI_AUDIO_EMU0);
1272 DUMP_REG(HDMI_NV_PDISP_HDMI_AUDIO_EMU_RDATA0);
1273 DUMP_REG(HDMI_NV_PDISP_HDMI_AUDIO_EMU1);
1274 DUMP_REG(HDMI_NV_PDISP_HDMI_AUDIO_EMU2);
1275 DUMP_REG(HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_CTRL);
1276 DUMP_REG(HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_STATUS);
1277 DUMP_REG(HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_HEADER);
1278 DUMP_REG(HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_SUBPACK0_LOW);
1279 DUMP_REG(HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_SUBPACK0_HIGH);
1280 DUMP_REG(HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_CTRL);
1281 DUMP_REG(HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_STATUS);
1282 DUMP_REG(HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_HEADER);
1283 DUMP_REG(HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_SUBPACK0_LOW);
1284 DUMP_REG(HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_SUBPACK0_HIGH);
1285 DUMP_REG(HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_SUBPACK1_LOW);
1286 DUMP_REG(HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_SUBPACK1_HIGH);
1287 DUMP_REG(HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
1288 DUMP_REG(HDMI_NV_PDISP_HDMI_GENERIC_STATUS);
1289 DUMP_REG(HDMI_NV_PDISP_HDMI_GENERIC_HEADER);
1290 DUMP_REG(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK0_LOW);
1291 DUMP_REG(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK0_HIGH);
1292 DUMP_REG(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK1_LOW);
1293 DUMP_REG(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK1_HIGH);
1294 DUMP_REG(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK2_LOW);
1295 DUMP_REG(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK2_HIGH);
1296 DUMP_REG(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK3_LOW);
1297 DUMP_REG(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK3_HIGH);
1298 DUMP_REG(HDMI_NV_PDISP_HDMI_ACR_CTRL);
1299 DUMP_REG(HDMI_NV_PDISP_HDMI_ACR_0320_SUBPACK_LOW);
1300 DUMP_REG(HDMI_NV_PDISP_HDMI_ACR_0320_SUBPACK_HIGH);
1301 DUMP_REG(HDMI_NV_PDISP_HDMI_ACR_0441_SUBPACK_LOW);
1302 DUMP_REG(HDMI_NV_PDISP_HDMI_ACR_0441_SUBPACK_HIGH);
1303 DUMP_REG(HDMI_NV_PDISP_HDMI_ACR_0882_SUBPACK_LOW);
1304 DUMP_REG(HDMI_NV_PDISP_HDMI_ACR_0882_SUBPACK_HIGH);
1305 DUMP_REG(HDMI_NV_PDISP_HDMI_ACR_1764_SUBPACK_LOW);
1306 DUMP_REG(HDMI_NV_PDISP_HDMI_ACR_1764_SUBPACK_HIGH);
1307 DUMP_REG(HDMI_NV_PDISP_HDMI_ACR_0480_SUBPACK_LOW);
1308 DUMP_REG(HDMI_NV_PDISP_HDMI_ACR_0480_SUBPACK_HIGH);
1309 DUMP_REG(HDMI_NV_PDISP_HDMI_ACR_0960_SUBPACK_LOW);
1310 DUMP_REG(HDMI_NV_PDISP_HDMI_ACR_0960_SUBPACK_HIGH);
1311 DUMP_REG(HDMI_NV_PDISP_HDMI_ACR_1920_SUBPACK_LOW);
1312 DUMP_REG(HDMI_NV_PDISP_HDMI_ACR_1920_SUBPACK_HIGH);
1313 DUMP_REG(HDMI_NV_PDISP_HDMI_CTRL);
1314 DUMP_REG(HDMI_NV_PDISP_HDMI_VSYNC_KEEPOUT);
1315 DUMP_REG(HDMI_NV_PDISP_HDMI_VSYNC_WINDOW);
1316 DUMP_REG(HDMI_NV_PDISP_HDMI_GCP_CTRL);
1317 DUMP_REG(HDMI_NV_PDISP_HDMI_GCP_STATUS);
1318 DUMP_REG(HDMI_NV_PDISP_HDMI_GCP_SUBPACK);
1319 DUMP_REG(HDMI_NV_PDISP_HDMI_CHANNEL_STATUS1);
1320 DUMP_REG(HDMI_NV_PDISP_HDMI_CHANNEL_STATUS2);
1321 DUMP_REG(HDMI_NV_PDISP_HDMI_EMU0);
1322 DUMP_REG(HDMI_NV_PDISP_HDMI_EMU1);
1323 DUMP_REG(HDMI_NV_PDISP_HDMI_EMU1_RDATA);
1324 DUMP_REG(HDMI_NV_PDISP_HDMI_SPARE);
1325 DUMP_REG(HDMI_NV_PDISP_HDMI_SPDIF_CHN_STATUS1);
1326 DUMP_REG(HDMI_NV_PDISP_HDMI_SPDIF_CHN_STATUS2);
1327 DUMP_REG(HDMI_NV_PDISP_HDMI_HDCPRIF_ROM_CTRL);
1328 DUMP_REG(HDMI_NV_PDISP_SOR_CAP);
1329 DUMP_REG(HDMI_NV_PDISP_SOR_PWR);
1330 DUMP_REG(HDMI_NV_PDISP_SOR_TEST);
1331 DUMP_REG(HDMI_NV_PDISP_SOR_PLL0);
1332 DUMP_REG(HDMI_NV_PDISP_SOR_PLL1);
1333 DUMP_REG(HDMI_NV_PDISP_SOR_PLL2);
1334 DUMP_REG(HDMI_NV_PDISP_SOR_CSTM);
1335 DUMP_REG(HDMI_NV_PDISP_SOR_LVDS);
1336 DUMP_REG(HDMI_NV_PDISP_SOR_CRCA);
1337 DUMP_REG(HDMI_NV_PDISP_SOR_CRCB);
1338 DUMP_REG(HDMI_NV_PDISP_SOR_BLANK);
1339 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_CTL);
1340 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(0));
1341 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(1));
1342 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(2));
1343 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(3));
1344 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(4));
1345 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(5));
1346 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(6));
1347 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(7));
1348 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(8));
1349 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(9));
1350 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(10));
1351 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(11));
1352 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(12));
1353 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(13));
1354 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(14));
1355 DUMP_REG(HDMI_NV_PDISP_SOR_SEQ_INST(15));
1356 DUMP_REG(HDMI_NV_PDISP_SOR_VCRCA0);
1357 DUMP_REG(HDMI_NV_PDISP_SOR_VCRCA1);
1358 DUMP_REG(HDMI_NV_PDISP_SOR_CCRCA0);
1359 DUMP_REG(HDMI_NV_PDISP_SOR_CCRCA1);
1360 DUMP_REG(HDMI_NV_PDISP_SOR_EDATAA0);
1361 DUMP_REG(HDMI_NV_PDISP_SOR_EDATAA1);
1362 DUMP_REG(HDMI_NV_PDISP_SOR_COUNTA0);
1363 DUMP_REG(HDMI_NV_PDISP_SOR_COUNTA1);
1364 DUMP_REG(HDMI_NV_PDISP_SOR_DEBUGA0);
1365 DUMP_REG(HDMI_NV_PDISP_SOR_DEBUGA1);
1366 DUMP_REG(HDMI_NV_PDISP_SOR_TRIG);
1367 DUMP_REG(HDMI_NV_PDISP_SOR_MSCHECK);
1368 DUMP_REG(HDMI_NV_PDISP_SOR_LANE_DRIVE_CURRENT);
1369 DUMP_REG(HDMI_NV_PDISP_AUDIO_DEBUG0);
1370 DUMP_REG(HDMI_NV_PDISP_AUDIO_DEBUG1);
1371 DUMP_REG(HDMI_NV_PDISP_AUDIO_DEBUG2);
1372 DUMP_REG(HDMI_NV_PDISP_AUDIO_FS(0));
1373 DUMP_REG(HDMI_NV_PDISP_AUDIO_FS(1));
1374 DUMP_REG(HDMI_NV_PDISP_AUDIO_FS(2));
1375 DUMP_REG(HDMI_NV_PDISP_AUDIO_FS(3));
1376 DUMP_REG(HDMI_NV_PDISP_AUDIO_FS(4));
1377 DUMP_REG(HDMI_NV_PDISP_AUDIO_FS(5));
1378 DUMP_REG(HDMI_NV_PDISP_AUDIO_FS(6));
1379 DUMP_REG(HDMI_NV_PDISP_AUDIO_PULSE_WIDTH);
1380 DUMP_REG(HDMI_NV_PDISP_AUDIO_THRESHOLD);
1381 DUMP_REG(HDMI_NV_PDISP_AUDIO_CNTRL0);
1382 DUMP_REG(HDMI_NV_PDISP_AUDIO_N);
1383 DUMP_REG(HDMI_NV_PDISP_HDCPRIF_ROM_TIMING);
1384 DUMP_REG(HDMI_NV_PDISP_SOR_REFCLK);
1385 DUMP_REG(HDMI_NV_PDISP_CRC_CONTROL);
1386 DUMP_REG(HDMI_NV_PDISP_INPUT_CONTROL);
1387 DUMP_REG(HDMI_NV_PDISP_SCRATCH);
1388 DUMP_REG(HDMI_NV_PDISP_PE_CURRENT);
1389 DUMP_REG(HDMI_NV_PDISP_KEY_CTRL);
1390 DUMP_REG(HDMI_NV_PDISP_KEY_DEBUG0);
1391 DUMP_REG(HDMI_NV_PDISP_KEY_DEBUG1);
1392 DUMP_REG(HDMI_NV_PDISP_KEY_DEBUG2);
1393 DUMP_REG(HDMI_NV_PDISP_KEY_HDCP_KEY_0);
1394 DUMP_REG(HDMI_NV_PDISP_KEY_HDCP_KEY_1);
1395 DUMP_REG(HDMI_NV_PDISP_KEY_HDCP_KEY_2);
1396 DUMP_REG(HDMI_NV_PDISP_KEY_HDCP_KEY_3);
1397 DUMP_REG(HDMI_NV_PDISP_KEY_HDCP_KEY_TRIG);
1398 DUMP_REG(HDMI_NV_PDISP_KEY_SKEY_INDEX);
1399 DUMP_REG(HDMI_NV_PDISP_SOR_AUDIO_CNTRL0);
Thierry Reding2ccb3962015-01-15 13:43:18 +01001400 DUMP_REG(HDMI_NV_PDISP_SOR_AUDIO_SPARE0);
1401 DUMP_REG(HDMI_NV_PDISP_SOR_AUDIO_HDA_CODEC_SCRATCH0);
1402 DUMP_REG(HDMI_NV_PDISP_SOR_AUDIO_HDA_CODEC_SCRATCH1);
Thierry Redingedec4af2012-11-15 21:28:23 +00001403 DUMP_REG(HDMI_NV_PDISP_SOR_AUDIO_HDA_ELD_BUFWR);
1404 DUMP_REG(HDMI_NV_PDISP_SOR_AUDIO_HDA_PRESENSE);
Thierry Reding2ccb3962015-01-15 13:43:18 +01001405 DUMP_REG(HDMI_NV_PDISP_INT_STATUS);
1406 DUMP_REG(HDMI_NV_PDISP_INT_MASK);
1407 DUMP_REG(HDMI_NV_PDISP_INT_ENABLE);
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +02001408 DUMP_REG(HDMI_NV_PDISP_SOR_IO_PEAK_CURRENT);
Thierry Redingedec4af2012-11-15 21:28:23 +00001409
1410#undef DUMP_REG
1411
Thierry Reding29871b22015-07-29 09:46:40 +02001412unlock:
1413 drm_modeset_unlock_all(drm);
1414 return err;
Thierry Redingedec4af2012-11-15 21:28:23 +00001415}
1416
1417static struct drm_info_list debugfs_files[] = {
1418 { "regs", tegra_hdmi_show_regs, 0, NULL },
1419};
1420
1421static int tegra_hdmi_debugfs_init(struct tegra_hdmi *hdmi,
1422 struct drm_minor *minor)
1423{
1424 unsigned int i;
1425 int err;
1426
1427 hdmi->debugfs = debugfs_create_dir("hdmi", minor->debugfs_root);
1428 if (!hdmi->debugfs)
1429 return -ENOMEM;
1430
1431 hdmi->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1432 GFP_KERNEL);
1433 if (!hdmi->debugfs_files) {
1434 err = -ENOMEM;
1435 goto remove;
1436 }
1437
1438 for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
1439 hdmi->debugfs_files[i].data = hdmi;
1440
1441 err = drm_debugfs_create_files(hdmi->debugfs_files,
1442 ARRAY_SIZE(debugfs_files),
1443 hdmi->debugfs, minor);
1444 if (err < 0)
1445 goto free;
1446
1447 hdmi->minor = minor;
1448
1449 return 0;
1450
1451free:
1452 kfree(hdmi->debugfs_files);
1453 hdmi->debugfs_files = NULL;
1454remove:
1455 debugfs_remove(hdmi->debugfs);
1456 hdmi->debugfs = NULL;
1457
1458 return err;
1459}
1460
Thierry Reding4009c222014-12-19 15:47:30 +01001461static void tegra_hdmi_debugfs_exit(struct tegra_hdmi *hdmi)
Thierry Redingedec4af2012-11-15 21:28:23 +00001462{
1463 drm_debugfs_remove_files(hdmi->debugfs_files, ARRAY_SIZE(debugfs_files),
1464 hdmi->minor);
1465 hdmi->minor = NULL;
1466
1467 kfree(hdmi->debugfs_files);
1468 hdmi->debugfs_files = NULL;
1469
1470 debugfs_remove(hdmi->debugfs);
1471 hdmi->debugfs = NULL;
Thierry Redingedec4af2012-11-15 21:28:23 +00001472}
1473
Thierry Reding53fa7f72013-09-24 15:35:40 +02001474static int tegra_hdmi_init(struct host1x_client *client)
Thierry Redingedec4af2012-11-15 21:28:23 +00001475{
Thierry Reding9910f5c2014-05-22 09:57:15 +02001476 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Reding776dc382013-10-14 14:43:22 +02001477 struct tegra_hdmi *hdmi = host1x_client_to_hdmi(client);
Thierry Redingedec4af2012-11-15 21:28:23 +00001478 int err;
1479
Thierry Redingedec4af2012-11-15 21:28:23 +00001480 hdmi->output.dev = client->dev;
Thierry Redingedec4af2012-11-15 21:28:23 +00001481
Thierry Reding59682712014-11-28 16:50:59 +01001482 drm_connector_init(drm, &hdmi->output.connector,
1483 &tegra_hdmi_connector_funcs,
1484 DRM_MODE_CONNECTOR_HDMIA);
1485 drm_connector_helper_add(&hdmi->output.connector,
1486 &tegra_hdmi_connector_helper_funcs);
1487 hdmi->output.connector.dpms = DRM_MODE_DPMS_OFF;
1488
1489 drm_encoder_init(drm, &hdmi->output.encoder, &tegra_hdmi_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001490 DRM_MODE_ENCODER_TMDS, NULL);
Thierry Reding59682712014-11-28 16:50:59 +01001491 drm_encoder_helper_add(&hdmi->output.encoder,
1492 &tegra_hdmi_encoder_helper_funcs);
1493
1494 drm_mode_connector_attach_encoder(&hdmi->output.connector,
1495 &hdmi->output.encoder);
1496 drm_connector_register(&hdmi->output.connector);
1497
Thierry Redingea130b22014-12-19 15:51:35 +01001498 err = tegra_output_init(drm, &hdmi->output);
1499 if (err < 0) {
1500 dev_err(client->dev, "failed to initialize output: %d\n", err);
1501 return err;
1502 }
Thierry Reding59682712014-11-28 16:50:59 +01001503
Thierry Redingea130b22014-12-19 15:51:35 +01001504 hdmi->output.encoder.possible_crtcs = 0x3;
Thierry Redingedec4af2012-11-15 21:28:23 +00001505
1506 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
Thierry Reding9910f5c2014-05-22 09:57:15 +02001507 err = tegra_hdmi_debugfs_init(hdmi, drm->primary);
Thierry Redingedec4af2012-11-15 21:28:23 +00001508 if (err < 0)
1509 dev_err(client->dev, "debugfs setup failed: %d\n", err);
1510 }
1511
Thierry Redingfb50a112014-02-28 16:57:34 +01001512 err = regulator_enable(hdmi->hdmi);
1513 if (err < 0) {
1514 dev_err(client->dev, "failed to enable HDMI regulator: %d\n",
1515 err);
1516 return err;
1517 }
1518
Thierry Reding59682712014-11-28 16:50:59 +01001519 err = regulator_enable(hdmi->pll);
1520 if (err < 0) {
1521 dev_err(hdmi->dev, "failed to enable PLL regulator: %d\n", err);
1522 return err;
1523 }
1524
1525 err = regulator_enable(hdmi->vdd);
1526 if (err < 0) {
1527 dev_err(hdmi->dev, "failed to enable VDD regulator: %d\n", err);
1528 return err;
1529 }
1530
Thierry Redingedec4af2012-11-15 21:28:23 +00001531 return 0;
1532}
1533
Thierry Reding53fa7f72013-09-24 15:35:40 +02001534static int tegra_hdmi_exit(struct host1x_client *client)
Thierry Redingedec4af2012-11-15 21:28:23 +00001535{
Thierry Reding776dc382013-10-14 14:43:22 +02001536 struct tegra_hdmi *hdmi = host1x_client_to_hdmi(client);
Thierry Redingedec4af2012-11-15 21:28:23 +00001537
Thierry Reding59682712014-11-28 16:50:59 +01001538 tegra_output_exit(&hdmi->output);
1539
Thierry Reding59682712014-11-28 16:50:59 +01001540 regulator_disable(hdmi->vdd);
1541 regulator_disable(hdmi->pll);
Thierry Redingfb50a112014-02-28 16:57:34 +01001542 regulator_disable(hdmi->hdmi);
1543
Thierry Reding4009c222014-12-19 15:47:30 +01001544 if (IS_ENABLED(CONFIG_DEBUG_FS))
1545 tegra_hdmi_debugfs_exit(hdmi);
Thierry Redingedec4af2012-11-15 21:28:23 +00001546
Thierry Redingedec4af2012-11-15 21:28:23 +00001547 return 0;
1548}
1549
1550static const struct host1x_client_ops hdmi_client_ops = {
Thierry Reding53fa7f72013-09-24 15:35:40 +02001551 .init = tegra_hdmi_init,
1552 .exit = tegra_hdmi_exit,
Thierry Redingedec4af2012-11-15 21:28:23 +00001553};
1554
Thierry Reding59af0592013-10-14 09:43:05 +02001555static const struct tegra_hdmi_config tegra20_hdmi_config = {
1556 .tmds = tegra20_tmds_config,
1557 .num_tmds = ARRAY_SIZE(tegra20_tmds_config),
1558 .fuse_override_offset = HDMI_NV_PDISP_SOR_LANE_DRIVE_CURRENT,
1559 .fuse_override_value = 1 << 31,
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +02001560 .has_sor_io_peak_current = false,
Thierry Reding2ccb3962015-01-15 13:43:18 +01001561 .has_hda = false,
1562 .has_hbr = false,
Thierry Reding59af0592013-10-14 09:43:05 +02001563};
1564
1565static const struct tegra_hdmi_config tegra30_hdmi_config = {
1566 .tmds = tegra30_tmds_config,
1567 .num_tmds = ARRAY_SIZE(tegra30_tmds_config),
1568 .fuse_override_offset = HDMI_NV_PDISP_SOR_LANE_DRIVE_CURRENT,
1569 .fuse_override_value = 1 << 31,
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +02001570 .has_sor_io_peak_current = false,
Thierry Reding2ccb3962015-01-15 13:43:18 +01001571 .has_hda = true,
1572 .has_hbr = false,
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +02001573};
1574
1575static const struct tegra_hdmi_config tegra114_hdmi_config = {
1576 .tmds = tegra114_tmds_config,
1577 .num_tmds = ARRAY_SIZE(tegra114_tmds_config),
1578 .fuse_override_offset = HDMI_NV_PDISP_SOR_PAD_CTLS0,
1579 .fuse_override_value = 1 << 31,
1580 .has_sor_io_peak_current = true,
Thierry Reding2ccb3962015-01-15 13:43:18 +01001581 .has_hda = true,
1582 .has_hbr = true,
Thierry Reding59af0592013-10-14 09:43:05 +02001583};
1584
Thierry Redingfb7be702013-11-15 16:07:32 +01001585static const struct tegra_hdmi_config tegra124_hdmi_config = {
1586 .tmds = tegra124_tmds_config,
1587 .num_tmds = ARRAY_SIZE(tegra124_tmds_config),
1588 .fuse_override_offset = HDMI_NV_PDISP_SOR_PAD_CTLS0,
1589 .fuse_override_value = 1 << 31,
1590 .has_sor_io_peak_current = true,
Thierry Reding2ccb3962015-01-15 13:43:18 +01001591 .has_hda = true,
1592 .has_hbr = true,
Thierry Redingfb7be702013-11-15 16:07:32 +01001593};
1594
Thierry Reding59af0592013-10-14 09:43:05 +02001595static const struct of_device_id tegra_hdmi_of_match[] = {
Thierry Redingfb7be702013-11-15 16:07:32 +01001596 { .compatible = "nvidia,tegra124-hdmi", .data = &tegra124_hdmi_config },
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +02001597 { .compatible = "nvidia,tegra114-hdmi", .data = &tegra114_hdmi_config },
Thierry Reding59af0592013-10-14 09:43:05 +02001598 { .compatible = "nvidia,tegra30-hdmi", .data = &tegra30_hdmi_config },
1599 { .compatible = "nvidia,tegra20-hdmi", .data = &tegra20_hdmi_config },
1600 { },
1601};
Stephen Warrenef707282014-06-18 16:21:55 -06001602MODULE_DEVICE_TABLE(of, tegra_hdmi_of_match);
Thierry Reding59af0592013-10-14 09:43:05 +02001603
Thierry Reding2ccb3962015-01-15 13:43:18 +01001604static void hda_format_parse(unsigned int format, unsigned int *rate,
1605 unsigned int *channels)
1606{
1607 unsigned int mul, div;
1608
1609 if (format & AC_FMT_BASE_44K)
1610 *rate = 44100;
1611 else
1612 *rate = 48000;
1613
1614 mul = (format & AC_FMT_MULT_MASK) >> AC_FMT_MULT_SHIFT;
1615 div = (format & AC_FMT_DIV_MASK) >> AC_FMT_DIV_SHIFT;
1616
1617 *rate = *rate * (mul + 1) / (div + 1);
1618
1619 *channels = (format & AC_FMT_CHAN_MASK) >> AC_FMT_CHAN_SHIFT;
1620}
1621
1622static irqreturn_t tegra_hdmi_irq(int irq, void *data)
1623{
1624 struct tegra_hdmi *hdmi = data;
1625 u32 value;
1626 int err;
1627
1628 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_INT_STATUS);
1629 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_INT_STATUS);
1630
1631 if (value & INT_CODEC_SCRATCH0) {
1632 unsigned int format;
1633 u32 value;
1634
1635 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_SOR_AUDIO_HDA_CODEC_SCRATCH0);
1636
1637 if (value & SOR_AUDIO_HDA_CODEC_SCRATCH0_VALID) {
1638 unsigned int sample_rate, channels;
1639
1640 format = value & SOR_AUDIO_HDA_CODEC_SCRATCH0_FMT_MASK;
1641
1642 hda_format_parse(format, &sample_rate, &channels);
1643
1644 hdmi->audio_sample_rate = sample_rate;
1645 hdmi->audio_channels = channels;
1646
1647 err = tegra_hdmi_setup_audio(hdmi);
1648 if (err < 0) {
1649 tegra_hdmi_disable_audio_infoframe(hdmi);
1650 tegra_hdmi_disable_audio(hdmi);
1651 } else {
1652 tegra_hdmi_setup_audio_infoframe(hdmi);
1653 tegra_hdmi_enable_audio_infoframe(hdmi);
1654 tegra_hdmi_enable_audio(hdmi);
1655 }
1656 } else {
1657 tegra_hdmi_disable_audio_infoframe(hdmi);
1658 tegra_hdmi_disable_audio(hdmi);
1659 }
1660 }
1661
1662 return IRQ_HANDLED;
1663}
1664
Thierry Redingedec4af2012-11-15 21:28:23 +00001665static int tegra_hdmi_probe(struct platform_device *pdev)
1666{
Thierry Reding59af0592013-10-14 09:43:05 +02001667 const struct of_device_id *match;
Thierry Redingedec4af2012-11-15 21:28:23 +00001668 struct tegra_hdmi *hdmi;
1669 struct resource *regs;
1670 int err;
1671
Thierry Reding59af0592013-10-14 09:43:05 +02001672 match = of_match_node(tegra_hdmi_of_match, pdev->dev.of_node);
1673 if (!match)
1674 return -ENODEV;
1675
Thierry Redingedec4af2012-11-15 21:28:23 +00001676 hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
1677 if (!hdmi)
1678 return -ENOMEM;
1679
Thierry Reding59af0592013-10-14 09:43:05 +02001680 hdmi->config = match->data;
Thierry Redingedec4af2012-11-15 21:28:23 +00001681 hdmi->dev = &pdev->dev;
Thierry Reding2ccb3962015-01-15 13:43:18 +01001682
Thierry Redingedec4af2012-11-15 21:28:23 +00001683 hdmi->audio_source = AUTO;
Thierry Reding2ccb3962015-01-15 13:43:18 +01001684 hdmi->audio_sample_rate = 48000;
1685 hdmi->audio_channels = 2;
Thierry Redingedec4af2012-11-15 21:28:23 +00001686 hdmi->stereo = false;
1687 hdmi->dvi = false;
1688
1689 hdmi->clk = devm_clk_get(&pdev->dev, NULL);
1690 if (IS_ERR(hdmi->clk)) {
1691 dev_err(&pdev->dev, "failed to get clock\n");
1692 return PTR_ERR(hdmi->clk);
1693 }
1694
Stephen Warrenca480802013-11-06 16:20:54 -07001695 hdmi->rst = devm_reset_control_get(&pdev->dev, "hdmi");
1696 if (IS_ERR(hdmi->rst)) {
1697 dev_err(&pdev->dev, "failed to get reset\n");
1698 return PTR_ERR(hdmi->rst);
1699 }
1700
Thierry Redingedec4af2012-11-15 21:28:23 +00001701 hdmi->clk_parent = devm_clk_get(&pdev->dev, "parent");
1702 if (IS_ERR(hdmi->clk_parent))
1703 return PTR_ERR(hdmi->clk_parent);
1704
Thierry Redingedec4af2012-11-15 21:28:23 +00001705 err = clk_set_parent(hdmi->clk, hdmi->clk_parent);
1706 if (err < 0) {
1707 dev_err(&pdev->dev, "failed to setup clocks: %d\n", err);
1708 return err;
1709 }
1710
Thierry Redingfb50a112014-02-28 16:57:34 +01001711 hdmi->hdmi = devm_regulator_get(&pdev->dev, "hdmi");
1712 if (IS_ERR(hdmi->hdmi)) {
1713 dev_err(&pdev->dev, "failed to get HDMI regulator\n");
1714 return PTR_ERR(hdmi->hdmi);
1715 }
1716
Thierry Redingedec4af2012-11-15 21:28:23 +00001717 hdmi->pll = devm_regulator_get(&pdev->dev, "pll");
1718 if (IS_ERR(hdmi->pll)) {
1719 dev_err(&pdev->dev, "failed to get PLL regulator\n");
1720 return PTR_ERR(hdmi->pll);
1721 }
1722
Thierry Reding88685682014-04-16 10:24:12 +02001723 hdmi->vdd = devm_regulator_get(&pdev->dev, "vdd");
1724 if (IS_ERR(hdmi->vdd)) {
1725 dev_err(&pdev->dev, "failed to get VDD regulator\n");
1726 return PTR_ERR(hdmi->vdd);
1727 }
1728
Thierry Redingedec4af2012-11-15 21:28:23 +00001729 hdmi->output.dev = &pdev->dev;
1730
Thierry Reding59d29c02013-10-14 14:26:42 +02001731 err = tegra_output_probe(&hdmi->output);
Thierry Redingedec4af2012-11-15 21:28:23 +00001732 if (err < 0)
1733 return err;
1734
1735 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Redingd4ed6022013-01-21 11:09:02 +01001736 hdmi->regs = devm_ioremap_resource(&pdev->dev, regs);
1737 if (IS_ERR(hdmi->regs))
1738 return PTR_ERR(hdmi->regs);
Thierry Redingedec4af2012-11-15 21:28:23 +00001739
1740 err = platform_get_irq(pdev, 0);
1741 if (err < 0)
1742 return err;
1743
1744 hdmi->irq = err;
1745
Thierry Reding2ccb3962015-01-15 13:43:18 +01001746 err = devm_request_irq(hdmi->dev, hdmi->irq, tegra_hdmi_irq, 0,
1747 dev_name(hdmi->dev), hdmi);
1748 if (err < 0) {
1749 dev_err(&pdev->dev, "failed to request IRQ#%u: %d\n",
1750 hdmi->irq, err);
1751 return err;
1752 }
1753
Thierry Reding52345492015-08-07 16:00:43 +02001754 platform_set_drvdata(pdev, hdmi);
1755 pm_runtime_enable(&pdev->dev);
1756
Thierry Reding776dc382013-10-14 14:43:22 +02001757 INIT_LIST_HEAD(&hdmi->client.list);
1758 hdmi->client.ops = &hdmi_client_ops;
1759 hdmi->client.dev = &pdev->dev;
Thierry Redingedec4af2012-11-15 21:28:23 +00001760
Thierry Reding776dc382013-10-14 14:43:22 +02001761 err = host1x_client_register(&hdmi->client);
Thierry Redingedec4af2012-11-15 21:28:23 +00001762 if (err < 0) {
1763 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
1764 err);
1765 return err;
1766 }
1767
Thierry Redingedec4af2012-11-15 21:28:23 +00001768 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 Reding52345492015-08-07 16:00:43 +02001776 pm_runtime_disable(&pdev->dev);
1777
Thierry Reding776dc382013-10-14 14:43:22 +02001778 err = host1x_client_unregister(&hdmi->client);
Thierry Redingedec4af2012-11-15 21:28:23 +00001779 if (err < 0) {
1780 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1781 err);
1782 return err;
1783 }
1784
Thierry Reding328ec692014-12-19 15:55:08 +01001785 tegra_output_remove(&hdmi->output);
Thierry Reding59d29c02013-10-14 14:26:42 +02001786
Thierry Reding52345492015-08-07 16:00:43 +02001787 return 0;
1788}
1789
1790#ifdef CONFIG_PM
1791static int tegra_hdmi_suspend(struct device *dev)
1792{
1793 struct tegra_hdmi *hdmi = dev_get_drvdata(dev);
1794 int err;
1795
1796 err = reset_control_assert(hdmi->rst);
1797 if (err < 0) {
1798 dev_err(dev, "failed to assert reset: %d\n", err);
1799 return err;
1800 }
1801
1802 usleep_range(1000, 2000);
1803
Thierry Redingd06e7f82014-04-16 10:43:41 +02001804 clk_disable_unprepare(hdmi->clk);
Thierry Redingedec4af2012-11-15 21:28:23 +00001805
1806 return 0;
1807}
1808
Thierry Reding52345492015-08-07 16:00:43 +02001809static int tegra_hdmi_resume(struct device *dev)
1810{
1811 struct tegra_hdmi *hdmi = dev_get_drvdata(dev);
1812 int err;
1813
1814 err = clk_prepare_enable(hdmi->clk);
1815 if (err < 0) {
1816 dev_err(dev, "failed to enable clock: %d\n", err);
1817 return err;
1818 }
1819
1820 usleep_range(1000, 2000);
1821
1822 err = reset_control_deassert(hdmi->rst);
1823 if (err < 0) {
1824 dev_err(dev, "failed to deassert reset: %d\n", err);
1825 clk_disable_unprepare(hdmi->clk);
1826 return err;
1827 }
1828
1829 return 0;
1830}
1831#endif
1832
1833static const struct dev_pm_ops tegra_hdmi_pm_ops = {
1834 SET_RUNTIME_PM_OPS(tegra_hdmi_suspend, tegra_hdmi_resume, NULL)
1835};
1836
Thierry Redingedec4af2012-11-15 21:28:23 +00001837struct platform_driver tegra_hdmi_driver = {
1838 .driver = {
1839 .name = "tegra-hdmi",
Thierry Redingedec4af2012-11-15 21:28:23 +00001840 .of_match_table = tegra_hdmi_of_match,
Thierry Reding52345492015-08-07 16:00:43 +02001841 .pm = &tegra_hdmi_pm_ops,
Thierry Redingedec4af2012-11-15 21:28:23 +00001842 },
1843 .probe = tegra_hdmi_probe,
1844 .remove = tegra_hdmi_remove,
1845};