blob: a4e9c769fc7a18273e873523d3f9f6c45b8d69e4 [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 Reding5e4acd32017-08-21 18:05:10 +020014#include <linux/of_device.h>
Thierry Reding52345492015-08-07 16:00:43 +020015#include <linux/pm_runtime.h>
Thierry Reding9eb9b222013-09-24 16:32:47 +020016#include <linux/regulator/consumer.h>
Stephen Warrenca480802013-11-06 16:20:54 -070017#include <linux/reset.h>
Thierry Redingac24c222012-11-23 15:14:00 +010018
Thierry Reding4aa3df72014-11-24 16:27:13 +010019#include <drm/drm_atomic_helper.h>
Thierry Reding59682712014-11-28 16:50:59 +010020#include <drm/drm_crtc.h>
21#include <drm/drm_crtc_helper.h>
22
Thierry Reding2ccb3962015-01-15 13:43:18 +010023#include <sound/hda_verbs.h>
24
Thierry Redingedec4af2012-11-15 21:28:23 +000025#include "hdmi.h"
26#include "drm.h"
27#include "dc.h"
Thierry Reding07a8aab2017-08-15 15:41:11 +020028#include "trace.h"
Thierry Redingedec4af2012-11-15 21:28:23 +000029
Thierry Reding2ccb3962015-01-15 13:43:18 +010030#define HDMI_ELD_BUFFER_SIZE 96
31
Thierry Reding59af0592013-10-14 09:43:05 +020032struct tmds_config {
33 unsigned int pclk;
34 u32 pll0;
35 u32 pll1;
36 u32 pe_current;
37 u32 drive_current;
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +020038 u32 peak_current;
Thierry Reding59af0592013-10-14 09:43:05 +020039};
40
41struct tegra_hdmi_config {
42 const struct tmds_config *tmds;
43 unsigned int num_tmds;
44
45 unsigned long fuse_override_offset;
Thierry Reding4ee8cee2014-12-08 16:25:14 +010046 u32 fuse_override_value;
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +020047
48 bool has_sor_io_peak_current;
Thierry Reding2ccb3962015-01-15 13:43:18 +010049 bool has_hda;
50 bool has_hbr;
Thierry Reding59af0592013-10-14 09:43:05 +020051};
52
Thierry Redingedec4af2012-11-15 21:28:23 +000053struct tegra_hdmi {
Thierry Reding776dc382013-10-14 14:43:22 +020054 struct host1x_client client;
Thierry Redingedec4af2012-11-15 21:28:23 +000055 struct tegra_output output;
56 struct device *dev;
57
Thierry Redingfb50a112014-02-28 16:57:34 +010058 struct regulator *hdmi;
Thierry Redingedec4af2012-11-15 21:28:23 +000059 struct regulator *pll;
Thierry Reding88685682014-04-16 10:24:12 +020060 struct regulator *vdd;
Thierry Redingedec4af2012-11-15 21:28:23 +000061
62 void __iomem *regs;
63 unsigned int irq;
64
65 struct clk *clk_parent;
66 struct clk *clk;
Stephen Warrenca480802013-11-06 16:20:54 -070067 struct reset_control *rst;
Thierry Redingedec4af2012-11-15 21:28:23 +000068
Thierry Reding59af0592013-10-14 09:43:05 +020069 const struct tegra_hdmi_config *config;
70
Thierry Redingedec4af2012-11-15 21:28:23 +000071 unsigned int audio_source;
Thierry Reding2ccb3962015-01-15 13:43:18 +010072 unsigned int audio_sample_rate;
73 unsigned int audio_channels;
74
75 unsigned int pixel_clock;
Thierry Redingedec4af2012-11-15 21:28:23 +000076 bool stereo;
77 bool dvi;
78
79 struct drm_info_list *debugfs_files;
80 struct drm_minor *minor;
81 struct dentry *debugfs;
82};
83
84static inline struct tegra_hdmi *
Thierry Reding776dc382013-10-14 14:43:22 +020085host1x_client_to_hdmi(struct host1x_client *client)
Thierry Redingedec4af2012-11-15 21:28:23 +000086{
87 return container_of(client, struct tegra_hdmi, client);
88}
89
90static inline struct tegra_hdmi *to_hdmi(struct tegra_output *output)
91{
92 return container_of(output, struct tegra_hdmi, output);
93}
94
95#define HDMI_AUDIOCLK_FREQ 216000000
96#define HDMI_REKEY_DEFAULT 56
97
98enum {
99 AUTO = 0,
100 SPDIF,
101 HDA,
102};
103
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100104static inline u32 tegra_hdmi_readl(struct tegra_hdmi *hdmi,
Thierry Reding7efe20c2017-08-15 15:41:08 +0200105 unsigned int offset)
Thierry Redingedec4af2012-11-15 21:28:23 +0000106{
Thierry Reding07a8aab2017-08-15 15:41:11 +0200107 u32 value = readl(hdmi->regs + (offset << 2));
108
109 trace_hdmi_readl(hdmi->dev, offset, value);
110
111 return value;
Thierry Redingedec4af2012-11-15 21:28:23 +0000112}
113
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100114static inline void tegra_hdmi_writel(struct tegra_hdmi *hdmi, u32 value,
Thierry Reding7efe20c2017-08-15 15:41:08 +0200115 unsigned int offset)
Thierry Redingedec4af2012-11-15 21:28:23 +0000116{
Thierry Reding07a8aab2017-08-15 15:41:11 +0200117 trace_hdmi_writel(hdmi->dev, offset, value);
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100118 writel(value, hdmi->regs + (offset << 2));
Thierry Redingedec4af2012-11-15 21:28:23 +0000119}
120
121struct tegra_hdmi_audio_config {
122 unsigned int pclk;
123 unsigned int n;
124 unsigned int cts;
125 unsigned int aval;
126};
127
128static const struct tegra_hdmi_audio_config tegra_hdmi_audio_32k[] = {
129 { 25200000, 4096, 25200, 24000 },
130 { 27000000, 4096, 27000, 24000 },
131 { 74250000, 4096, 74250, 24000 },
132 { 148500000, 4096, 148500, 24000 },
133 { 0, 0, 0, 0 },
134};
135
136static const struct tegra_hdmi_audio_config tegra_hdmi_audio_44_1k[] = {
137 { 25200000, 5880, 26250, 25000 },
138 { 27000000, 5880, 28125, 25000 },
139 { 74250000, 4704, 61875, 20000 },
140 { 148500000, 4704, 123750, 20000 },
141 { 0, 0, 0, 0 },
142};
143
144static const struct tegra_hdmi_audio_config tegra_hdmi_audio_48k[] = {
145 { 25200000, 6144, 25200, 24000 },
146 { 27000000, 6144, 27000, 24000 },
147 { 74250000, 6144, 74250, 24000 },
148 { 148500000, 6144, 148500, 24000 },
149 { 0, 0, 0, 0 },
150};
151
152static const struct tegra_hdmi_audio_config tegra_hdmi_audio_88_2k[] = {
153 { 25200000, 11760, 26250, 25000 },
154 { 27000000, 11760, 28125, 25000 },
155 { 74250000, 9408, 61875, 20000 },
156 { 148500000, 9408, 123750, 20000 },
157 { 0, 0, 0, 0 },
158};
159
160static const struct tegra_hdmi_audio_config tegra_hdmi_audio_96k[] = {
161 { 25200000, 12288, 25200, 24000 },
162 { 27000000, 12288, 27000, 24000 },
163 { 74250000, 12288, 74250, 24000 },
164 { 148500000, 12288, 148500, 24000 },
165 { 0, 0, 0, 0 },
166};
167
168static const struct tegra_hdmi_audio_config tegra_hdmi_audio_176_4k[] = {
169 { 25200000, 23520, 26250, 25000 },
170 { 27000000, 23520, 28125, 25000 },
171 { 74250000, 18816, 61875, 20000 },
172 { 148500000, 18816, 123750, 20000 },
173 { 0, 0, 0, 0 },
174};
175
176static const struct tegra_hdmi_audio_config tegra_hdmi_audio_192k[] = {
177 { 25200000, 24576, 25200, 24000 },
178 { 27000000, 24576, 27000, 24000 },
179 { 74250000, 24576, 74250, 24000 },
180 { 148500000, 24576, 148500, 24000 },
181 { 0, 0, 0, 0 },
182};
183
Thierry Redingf27db962013-09-30 15:14:41 +0200184static const struct tmds_config tegra20_tmds_config[] = {
Lucas Stachfa416dd2012-12-19 21:38:55 +0000185 { /* slow pixel clock modes */
Thierry Redingedec4af2012-11-15 21:28:23 +0000186 .pclk = 27000000,
187 .pll0 = SOR_PLL_BG_V17_S(3) | SOR_PLL_ICHPMP(1) |
188 SOR_PLL_RESISTORSEL | SOR_PLL_VCOCAP(0) |
189 SOR_PLL_TX_REG_LOAD(3),
190 .pll1 = SOR_PLL_TMDS_TERM_ENABLE,
191 .pe_current = PE_CURRENT0(PE_CURRENT_0_0_mA) |
192 PE_CURRENT1(PE_CURRENT_0_0_mA) |
193 PE_CURRENT2(PE_CURRENT_0_0_mA) |
194 PE_CURRENT3(PE_CURRENT_0_0_mA),
195 .drive_current = DRIVE_CURRENT_LANE0(DRIVE_CURRENT_7_125_mA) |
196 DRIVE_CURRENT_LANE1(DRIVE_CURRENT_7_125_mA) |
197 DRIVE_CURRENT_LANE2(DRIVE_CURRENT_7_125_mA) |
198 DRIVE_CURRENT_LANE3(DRIVE_CURRENT_7_125_mA),
Lucas Stachfa416dd2012-12-19 21:38:55 +0000199 },
200 { /* high pixel clock modes */
Thierry Redingedec4af2012-11-15 21:28:23 +0000201 .pclk = UINT_MAX,
202 .pll0 = SOR_PLL_BG_V17_S(3) | SOR_PLL_ICHPMP(1) |
203 SOR_PLL_RESISTORSEL | SOR_PLL_VCOCAP(1) |
204 SOR_PLL_TX_REG_LOAD(3),
205 .pll1 = SOR_PLL_TMDS_TERM_ENABLE | SOR_PLL_PE_EN,
206 .pe_current = PE_CURRENT0(PE_CURRENT_6_0_mA) |
207 PE_CURRENT1(PE_CURRENT_6_0_mA) |
208 PE_CURRENT2(PE_CURRENT_6_0_mA) |
209 PE_CURRENT3(PE_CURRENT_6_0_mA),
210 .drive_current = DRIVE_CURRENT_LANE0(DRIVE_CURRENT_7_125_mA) |
211 DRIVE_CURRENT_LANE1(DRIVE_CURRENT_7_125_mA) |
212 DRIVE_CURRENT_LANE2(DRIVE_CURRENT_7_125_mA) |
213 DRIVE_CURRENT_LANE3(DRIVE_CURRENT_7_125_mA),
214 },
215};
216
Thierry Redingf27db962013-09-30 15:14:41 +0200217static const struct tmds_config tegra30_tmds_config[] = {
Thierry Redingedec4af2012-11-15 21:28:23 +0000218 { /* 480p modes */
219 .pclk = 27000000,
220 .pll0 = SOR_PLL_BG_V17_S(3) | SOR_PLL_ICHPMP(1) |
221 SOR_PLL_RESISTORSEL | SOR_PLL_VCOCAP(0) |
222 SOR_PLL_TX_REG_LOAD(0),
223 .pll1 = SOR_PLL_TMDS_TERM_ENABLE,
224 .pe_current = PE_CURRENT0(PE_CURRENT_0_0_mA) |
225 PE_CURRENT1(PE_CURRENT_0_0_mA) |
226 PE_CURRENT2(PE_CURRENT_0_0_mA) |
227 PE_CURRENT3(PE_CURRENT_0_0_mA),
228 .drive_current = DRIVE_CURRENT_LANE0(DRIVE_CURRENT_5_250_mA) |
229 DRIVE_CURRENT_LANE1(DRIVE_CURRENT_5_250_mA) |
230 DRIVE_CURRENT_LANE2(DRIVE_CURRENT_5_250_mA) |
231 DRIVE_CURRENT_LANE3(DRIVE_CURRENT_5_250_mA),
232 }, { /* 720p modes */
233 .pclk = 74250000,
234 .pll0 = SOR_PLL_BG_V17_S(3) | SOR_PLL_ICHPMP(1) |
235 SOR_PLL_RESISTORSEL | SOR_PLL_VCOCAP(1) |
236 SOR_PLL_TX_REG_LOAD(0),
237 .pll1 = SOR_PLL_TMDS_TERM_ENABLE | SOR_PLL_PE_EN,
238 .pe_current = PE_CURRENT0(PE_CURRENT_5_0_mA) |
239 PE_CURRENT1(PE_CURRENT_5_0_mA) |
240 PE_CURRENT2(PE_CURRENT_5_0_mA) |
241 PE_CURRENT3(PE_CURRENT_5_0_mA),
242 .drive_current = DRIVE_CURRENT_LANE0(DRIVE_CURRENT_5_250_mA) |
243 DRIVE_CURRENT_LANE1(DRIVE_CURRENT_5_250_mA) |
244 DRIVE_CURRENT_LANE2(DRIVE_CURRENT_5_250_mA) |
245 DRIVE_CURRENT_LANE3(DRIVE_CURRENT_5_250_mA),
246 }, { /* 1080p modes */
247 .pclk = UINT_MAX,
248 .pll0 = SOR_PLL_BG_V17_S(3) | SOR_PLL_ICHPMP(1) |
249 SOR_PLL_RESISTORSEL | SOR_PLL_VCOCAP(3) |
250 SOR_PLL_TX_REG_LOAD(0),
251 .pll1 = SOR_PLL_TMDS_TERM_ENABLE | SOR_PLL_PE_EN,
252 .pe_current = PE_CURRENT0(PE_CURRENT_5_0_mA) |
253 PE_CURRENT1(PE_CURRENT_5_0_mA) |
254 PE_CURRENT2(PE_CURRENT_5_0_mA) |
255 PE_CURRENT3(PE_CURRENT_5_0_mA),
256 .drive_current = DRIVE_CURRENT_LANE0(DRIVE_CURRENT_5_250_mA) |
257 DRIVE_CURRENT_LANE1(DRIVE_CURRENT_5_250_mA) |
258 DRIVE_CURRENT_LANE2(DRIVE_CURRENT_5_250_mA) |
259 DRIVE_CURRENT_LANE3(DRIVE_CURRENT_5_250_mA),
260 },
261};
262
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +0200263static const struct tmds_config tegra114_tmds_config[] = {
264 { /* 480p/576p / 25.2MHz/27MHz modes */
265 .pclk = 27000000,
266 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
267 SOR_PLL_VCOCAP(0) | SOR_PLL_RESISTORSEL,
268 .pll1 = SOR_PLL_LOADADJ(3) | SOR_PLL_TMDS_TERMADJ(0),
269 .pe_current = PE_CURRENT0(PE_CURRENT_0_mA_T114) |
270 PE_CURRENT1(PE_CURRENT_0_mA_T114) |
271 PE_CURRENT2(PE_CURRENT_0_mA_T114) |
272 PE_CURRENT3(PE_CURRENT_0_mA_T114),
273 .drive_current =
274 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_10_400_mA_T114) |
275 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_10_400_mA_T114) |
276 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_10_400_mA_T114) |
277 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_10_400_mA_T114),
278 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_0_000_mA) |
279 PEAK_CURRENT_LANE1(PEAK_CURRENT_0_000_mA) |
280 PEAK_CURRENT_LANE2(PEAK_CURRENT_0_000_mA) |
281 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_000_mA),
282 }, { /* 720p / 74.25MHz modes */
283 .pclk = 74250000,
284 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
285 SOR_PLL_VCOCAP(1) | SOR_PLL_RESISTORSEL,
286 .pll1 = SOR_PLL_PE_EN | SOR_PLL_LOADADJ(3) |
287 SOR_PLL_TMDS_TERMADJ(0),
288 .pe_current = PE_CURRENT0(PE_CURRENT_15_mA_T114) |
289 PE_CURRENT1(PE_CURRENT_15_mA_T114) |
290 PE_CURRENT2(PE_CURRENT_15_mA_T114) |
291 PE_CURRENT3(PE_CURRENT_15_mA_T114),
292 .drive_current =
293 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_10_400_mA_T114) |
294 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_10_400_mA_T114) |
295 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_10_400_mA_T114) |
296 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_10_400_mA_T114),
297 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_0_000_mA) |
298 PEAK_CURRENT_LANE1(PEAK_CURRENT_0_000_mA) |
299 PEAK_CURRENT_LANE2(PEAK_CURRENT_0_000_mA) |
300 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_000_mA),
301 }, { /* 1080p / 148.5MHz modes */
302 .pclk = 148500000,
303 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
304 SOR_PLL_VCOCAP(3) | SOR_PLL_RESISTORSEL,
305 .pll1 = SOR_PLL_PE_EN | SOR_PLL_LOADADJ(3) |
306 SOR_PLL_TMDS_TERMADJ(0),
307 .pe_current = PE_CURRENT0(PE_CURRENT_10_mA_T114) |
308 PE_CURRENT1(PE_CURRENT_10_mA_T114) |
309 PE_CURRENT2(PE_CURRENT_10_mA_T114) |
310 PE_CURRENT3(PE_CURRENT_10_mA_T114),
311 .drive_current =
312 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_12_400_mA_T114) |
313 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_12_400_mA_T114) |
314 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_12_400_mA_T114) |
315 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_12_400_mA_T114),
316 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_0_000_mA) |
317 PEAK_CURRENT_LANE1(PEAK_CURRENT_0_000_mA) |
318 PEAK_CURRENT_LANE2(PEAK_CURRENT_0_000_mA) |
319 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_000_mA),
320 }, { /* 225/297MHz modes */
321 .pclk = UINT_MAX,
322 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
323 SOR_PLL_VCOCAP(0xf) | SOR_PLL_RESISTORSEL,
324 .pll1 = SOR_PLL_LOADADJ(3) | SOR_PLL_TMDS_TERMADJ(7)
325 | SOR_PLL_TMDS_TERM_ENABLE,
326 .pe_current = PE_CURRENT0(PE_CURRENT_0_mA_T114) |
327 PE_CURRENT1(PE_CURRENT_0_mA_T114) |
328 PE_CURRENT2(PE_CURRENT_0_mA_T114) |
329 PE_CURRENT3(PE_CURRENT_0_mA_T114),
330 .drive_current =
331 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_25_200_mA_T114) |
332 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_25_200_mA_T114) |
333 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_25_200_mA_T114) |
334 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_19_200_mA_T114),
335 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_3_000_mA) |
336 PEAK_CURRENT_LANE1(PEAK_CURRENT_3_000_mA) |
337 PEAK_CURRENT_LANE2(PEAK_CURRENT_3_000_mA) |
338 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_800_mA),
339 },
340};
341
Thierry Redingfb7be702013-11-15 16:07:32 +0100342static const struct tmds_config tegra124_tmds_config[] = {
343 { /* 480p/576p / 25.2MHz/27MHz modes */
344 .pclk = 27000000,
345 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
346 SOR_PLL_VCOCAP(0) | SOR_PLL_RESISTORSEL,
347 .pll1 = SOR_PLL_LOADADJ(3) | SOR_PLL_TMDS_TERMADJ(0),
348 .pe_current = PE_CURRENT0(PE_CURRENT_0_mA_T114) |
349 PE_CURRENT1(PE_CURRENT_0_mA_T114) |
350 PE_CURRENT2(PE_CURRENT_0_mA_T114) |
351 PE_CURRENT3(PE_CURRENT_0_mA_T114),
352 .drive_current =
353 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_10_400_mA_T114) |
354 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_10_400_mA_T114) |
355 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_10_400_mA_T114) |
356 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_10_400_mA_T114),
357 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_0_000_mA) |
358 PEAK_CURRENT_LANE1(PEAK_CURRENT_0_000_mA) |
359 PEAK_CURRENT_LANE2(PEAK_CURRENT_0_000_mA) |
360 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_000_mA),
361 }, { /* 720p / 74.25MHz modes */
362 .pclk = 74250000,
363 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
364 SOR_PLL_VCOCAP(1) | SOR_PLL_RESISTORSEL,
365 .pll1 = SOR_PLL_PE_EN | SOR_PLL_LOADADJ(3) |
366 SOR_PLL_TMDS_TERMADJ(0),
367 .pe_current = PE_CURRENT0(PE_CURRENT_15_mA_T114) |
368 PE_CURRENT1(PE_CURRENT_15_mA_T114) |
369 PE_CURRENT2(PE_CURRENT_15_mA_T114) |
370 PE_CURRENT3(PE_CURRENT_15_mA_T114),
371 .drive_current =
372 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_10_400_mA_T114) |
373 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_10_400_mA_T114) |
374 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_10_400_mA_T114) |
375 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_10_400_mA_T114),
376 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_0_000_mA) |
377 PEAK_CURRENT_LANE1(PEAK_CURRENT_0_000_mA) |
378 PEAK_CURRENT_LANE2(PEAK_CURRENT_0_000_mA) |
379 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_000_mA),
380 }, { /* 1080p / 148.5MHz modes */
381 .pclk = 148500000,
382 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
383 SOR_PLL_VCOCAP(3) | SOR_PLL_RESISTORSEL,
384 .pll1 = SOR_PLL_PE_EN | SOR_PLL_LOADADJ(3) |
385 SOR_PLL_TMDS_TERMADJ(0),
386 .pe_current = PE_CURRENT0(PE_CURRENT_10_mA_T114) |
387 PE_CURRENT1(PE_CURRENT_10_mA_T114) |
388 PE_CURRENT2(PE_CURRENT_10_mA_T114) |
389 PE_CURRENT3(PE_CURRENT_10_mA_T114),
390 .drive_current =
391 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_12_400_mA_T114) |
392 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_12_400_mA_T114) |
393 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_12_400_mA_T114) |
394 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_12_400_mA_T114),
395 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_0_000_mA) |
396 PEAK_CURRENT_LANE1(PEAK_CURRENT_0_000_mA) |
397 PEAK_CURRENT_LANE2(PEAK_CURRENT_0_000_mA) |
398 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_000_mA),
399 }, { /* 225/297MHz modes */
400 .pclk = UINT_MAX,
401 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
402 SOR_PLL_VCOCAP(0xf) | SOR_PLL_RESISTORSEL,
403 .pll1 = SOR_PLL_LOADADJ(3) | SOR_PLL_TMDS_TERMADJ(7)
404 | SOR_PLL_TMDS_TERM_ENABLE,
405 .pe_current = PE_CURRENT0(PE_CURRENT_0_mA_T114) |
406 PE_CURRENT1(PE_CURRENT_0_mA_T114) |
407 PE_CURRENT2(PE_CURRENT_0_mA_T114) |
408 PE_CURRENT3(PE_CURRENT_0_mA_T114),
409 .drive_current =
410 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_25_200_mA_T114) |
411 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_25_200_mA_T114) |
412 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_25_200_mA_T114) |
413 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_19_200_mA_T114),
414 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_3_000_mA) |
415 PEAK_CURRENT_LANE1(PEAK_CURRENT_3_000_mA) |
416 PEAK_CURRENT_LANE2(PEAK_CURRENT_3_000_mA) |
417 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_800_mA),
418 },
419};
420
Thierry Redingedec4af2012-11-15 21:28:23 +0000421static const struct tegra_hdmi_audio_config *
Thierry Reding2ccb3962015-01-15 13:43:18 +0100422tegra_hdmi_get_audio_config(unsigned int sample_rate, unsigned int pclk)
Thierry Redingedec4af2012-11-15 21:28:23 +0000423{
424 const struct tegra_hdmi_audio_config *table;
425
Thierry Reding2ccb3962015-01-15 13:43:18 +0100426 switch (sample_rate) {
Thierry Redingedec4af2012-11-15 21:28:23 +0000427 case 32000:
428 table = tegra_hdmi_audio_32k;
429 break;
430
431 case 44100:
432 table = tegra_hdmi_audio_44_1k;
433 break;
434
435 case 48000:
436 table = tegra_hdmi_audio_48k;
437 break;
438
439 case 88200:
440 table = tegra_hdmi_audio_88_2k;
441 break;
442
443 case 96000:
444 table = tegra_hdmi_audio_96k;
445 break;
446
447 case 176400:
448 table = tegra_hdmi_audio_176_4k;
449 break;
450
451 case 192000:
452 table = tegra_hdmi_audio_192k;
453 break;
454
455 default:
456 return NULL;
457 }
458
459 while (table->pclk) {
460 if (table->pclk == pclk)
461 return table;
462
463 table++;
464 }
465
466 return NULL;
467}
468
469static void tegra_hdmi_setup_audio_fs_tables(struct tegra_hdmi *hdmi)
470{
471 const unsigned int freqs[] = {
472 32000, 44100, 48000, 88200, 96000, 176400, 192000
473 };
474 unsigned int i;
475
476 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
477 unsigned int f = freqs[i];
478 unsigned int eight_half;
Thierry Redingedec4af2012-11-15 21:28:23 +0000479 unsigned int delta;
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100480 u32 value;
Thierry Redingedec4af2012-11-15 21:28:23 +0000481
482 if (f > 96000)
483 delta = 2;
Thierry Reding17a8b6b2013-12-16 10:01:24 +0100484 else if (f > 48000)
Thierry Redingedec4af2012-11-15 21:28:23 +0000485 delta = 6;
486 else
487 delta = 9;
488
489 eight_half = (8 * HDMI_AUDIOCLK_FREQ) / (f * 128);
490 value = AUDIO_FS_LOW(eight_half - delta) |
491 AUDIO_FS_HIGH(eight_half + delta);
492 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_AUDIO_FS(i));
493 }
494}
495
Thierry Reding2ccb3962015-01-15 13:43:18 +0100496static void tegra_hdmi_write_aval(struct tegra_hdmi *hdmi, u32 value)
Thierry Redingedec4af2012-11-15 21:28:23 +0000497{
Thierry Reding2ccb3962015-01-15 13:43:18 +0100498 static const struct {
499 unsigned int sample_rate;
500 unsigned int offset;
501 } regs[] = {
502 { 32000, HDMI_NV_PDISP_SOR_AUDIO_AVAL_0320 },
503 { 44100, HDMI_NV_PDISP_SOR_AUDIO_AVAL_0441 },
504 { 48000, HDMI_NV_PDISP_SOR_AUDIO_AVAL_0480 },
505 { 88200, HDMI_NV_PDISP_SOR_AUDIO_AVAL_0882 },
506 { 96000, HDMI_NV_PDISP_SOR_AUDIO_AVAL_0960 },
507 { 176400, HDMI_NV_PDISP_SOR_AUDIO_AVAL_1764 },
508 { 192000, HDMI_NV_PDISP_SOR_AUDIO_AVAL_1920 },
509 };
510 unsigned int i;
511
512 for (i = 0; i < ARRAY_SIZE(regs); i++) {
513 if (regs[i].sample_rate == hdmi->audio_sample_rate) {
514 tegra_hdmi_writel(hdmi, value, regs[i].offset);
515 break;
516 }
517 }
518}
519
520static int tegra_hdmi_setup_audio(struct tegra_hdmi *hdmi)
521{
Thierry Redingedec4af2012-11-15 21:28:23 +0000522 const struct tegra_hdmi_audio_config *config;
Thierry Reding2ccb3962015-01-15 13:43:18 +0100523 u32 source, value;
Thierry Redingedec4af2012-11-15 21:28:23 +0000524
525 switch (hdmi->audio_source) {
526 case HDA:
Thierry Reding2ccb3962015-01-15 13:43:18 +0100527 if (hdmi->config->has_hda)
528 source = SOR_AUDIO_CNTRL0_SOURCE_SELECT_HDAL;
529 else
530 return -EINVAL;
531
Thierry Redingedec4af2012-11-15 21:28:23 +0000532 break;
533
534 case SPDIF:
Thierry Reding2ccb3962015-01-15 13:43:18 +0100535 if (hdmi->config->has_hda)
536 source = SOR_AUDIO_CNTRL0_SOURCE_SELECT_SPDIF;
537 else
538 source = AUDIO_CNTRL0_SOURCE_SELECT_SPDIF;
Thierry Redingedec4af2012-11-15 21:28:23 +0000539 break;
540
541 default:
Thierry Reding2ccb3962015-01-15 13:43:18 +0100542 if (hdmi->config->has_hda)
543 source = SOR_AUDIO_CNTRL0_SOURCE_SELECT_AUTO;
544 else
545 source = AUDIO_CNTRL0_SOURCE_SELECT_AUTO;
Thierry Redingedec4af2012-11-15 21:28:23 +0000546 break;
547 }
548
Thierry Reding2ccb3962015-01-15 13:43:18 +0100549 /*
550 * Tegra30 and later use a slightly modified version of the register
551 * layout to accomodate for changes related to supporting HDA as the
552 * audio input source for HDMI. The source select field has moved to
553 * the SOR_AUDIO_CNTRL0 register, but the error tolerance and frames
554 * per block fields remain in the AUDIO_CNTRL0 register.
555 */
556 if (hdmi->config->has_hda) {
557 /*
558 * Inject null samples into the audio FIFO for every frame in
559 * which the codec did not receive any samples. This applies
560 * to stereo LPCM only.
561 *
562 * XXX: This seems to be a remnant of MCP days when this was
563 * used to work around issues with monitors not being able to
564 * play back system startup sounds early. It is possibly not
565 * needed on Linux at all.
566 */
567 if (hdmi->audio_channels == 2)
568 value = SOR_AUDIO_CNTRL0_INJECT_NULLSMPL;
569 else
570 value = 0;
Thierry Redingedec4af2012-11-15 21:28:23 +0000571
Thierry Reding2ccb3962015-01-15 13:43:18 +0100572 value |= source;
573
574 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_AUDIO_CNTRL0);
Thierry Redingedec4af2012-11-15 21:28:23 +0000575 }
576
Thierry Reding2ccb3962015-01-15 13:43:18 +0100577 /*
578 * On Tegra20, HDA is not a supported audio source and the source
579 * select field is part of the AUDIO_CNTRL0 register.
580 */
581 value = AUDIO_CNTRL0_FRAMES_PER_BLOCK(0xc0) |
582 AUDIO_CNTRL0_ERROR_TOLERANCE(6);
583
584 if (!hdmi->config->has_hda)
585 value |= source;
586
587 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_AUDIO_CNTRL0);
588
589 /*
590 * Advertise support for High Bit-Rate on Tegra114 and later.
591 */
592 if (hdmi->config->has_hbr) {
593 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_SOR_AUDIO_SPARE0);
594 value |= SOR_AUDIO_SPARE0_HBR_ENABLE;
595 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_AUDIO_SPARE0);
596 }
597
598 config = tegra_hdmi_get_audio_config(hdmi->audio_sample_rate,
599 hdmi->pixel_clock);
Thierry Redingedec4af2012-11-15 21:28:23 +0000600 if (!config) {
Thierry Reding2ccb3962015-01-15 13:43:18 +0100601 dev_err(hdmi->dev,
602 "cannot set audio to %u Hz at %u Hz pixel clock\n",
603 hdmi->audio_sample_rate, hdmi->pixel_clock);
Thierry Redingedec4af2012-11-15 21:28:23 +0000604 return -EINVAL;
605 }
606
607 tegra_hdmi_writel(hdmi, 0, HDMI_NV_PDISP_HDMI_ACR_CTRL);
608
609 value = AUDIO_N_RESETF | AUDIO_N_GENERATE_ALTERNATE |
610 AUDIO_N_VALUE(config->n - 1);
611 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_AUDIO_N);
612
613 tegra_hdmi_writel(hdmi, ACR_SUBPACK_N(config->n) | ACR_ENABLE,
614 HDMI_NV_PDISP_HDMI_ACR_0441_SUBPACK_HIGH);
615
Thierry Reding2ccb3962015-01-15 13:43:18 +0100616 tegra_hdmi_writel(hdmi, ACR_SUBPACK_CTS(config->cts),
617 HDMI_NV_PDISP_HDMI_ACR_0441_SUBPACK_LOW);
Thierry Redingedec4af2012-11-15 21:28:23 +0000618
619 value = SPARE_HW_CTS | SPARE_FORCE_SW_CTS | SPARE_CTS_RESET_VAL(1);
620 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_SPARE);
621
622 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_AUDIO_N);
623 value &= ~AUDIO_N_RESETF;
624 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_AUDIO_N);
625
Thierry Reding2ccb3962015-01-15 13:43:18 +0100626 if (hdmi->config->has_hda)
627 tegra_hdmi_write_aval(hdmi, config->aval);
Thierry Redingedec4af2012-11-15 21:28:23 +0000628
629 tegra_hdmi_setup_audio_fs_tables(hdmi);
630
631 return 0;
632}
633
Thierry Reding2ccb3962015-01-15 13:43:18 +0100634static void tegra_hdmi_disable_audio(struct tegra_hdmi *hdmi)
635{
636 u32 value;
637
638 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
639 value &= ~GENERIC_CTRL_AUDIO;
640 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
641}
642
643static void tegra_hdmi_enable_audio(struct tegra_hdmi *hdmi)
644{
645 u32 value;
646
647 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
648 value |= GENERIC_CTRL_AUDIO;
649 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
650}
651
Thierry Reding52345492015-08-07 16:00:43 +0200652static void tegra_hdmi_write_eld(struct tegra_hdmi *hdmi)
653{
654 size_t length = drm_eld_size(hdmi->output.connector.eld), i;
655 u32 value;
656
657 for (i = 0; i < length; i++)
658 tegra_hdmi_writel(hdmi, i << 8 | hdmi->output.connector.eld[i],
659 HDMI_NV_PDISP_SOR_AUDIO_HDA_ELD_BUFWR);
660
661 /*
662 * The HDA codec will always report an ELD buffer size of 96 bytes and
663 * the HDA codec driver will check that each byte read from the buffer
664 * is valid. Therefore every byte must be written, even if no 96 bytes
665 * were parsed from EDID.
666 */
667 for (i = length; i < HDMI_ELD_BUFFER_SIZE; i++)
668 tegra_hdmi_writel(hdmi, i << 8 | 0,
669 HDMI_NV_PDISP_SOR_AUDIO_HDA_ELD_BUFWR);
670
671 value = SOR_AUDIO_HDA_PRESENSE_VALID | SOR_AUDIO_HDA_PRESENSE_PRESENT;
672 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_AUDIO_HDA_PRESENSE);
673}
674
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100675static inline u32 tegra_hdmi_subpack(const u8 *ptr, size_t size)
Thierry Redingedec4af2012-11-15 21:28:23 +0000676{
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100677 u32 value = 0;
Thierry Redingedec4af2012-11-15 21:28:23 +0000678 size_t i;
Thierry Redingedec4af2012-11-15 21:28:23 +0000679
Thierry Redingac24c222012-11-23 15:14:00 +0100680 for (i = size; i > 0; i--)
681 value = (value << 8) | ptr[i - 1];
Thierry Redingedec4af2012-11-15 21:28:23 +0000682
Thierry Redingac24c222012-11-23 15:14:00 +0100683 return value;
684}
Thierry Redingedec4af2012-11-15 21:28:23 +0000685
Thierry Redingac24c222012-11-23 15:14:00 +0100686static void tegra_hdmi_write_infopack(struct tegra_hdmi *hdmi, const void *data,
687 size_t size)
688{
689 const u8 *ptr = data;
690 unsigned long offset;
Thierry Redingac24c222012-11-23 15:14:00 +0100691 size_t i, j;
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100692 u32 value;
Thierry Redingedec4af2012-11-15 21:28:23 +0000693
Thierry Redingac24c222012-11-23 15:14:00 +0100694 switch (ptr[0]) {
695 case HDMI_INFOFRAME_TYPE_AVI:
696 offset = HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_HEADER;
697 break;
698
699 case HDMI_INFOFRAME_TYPE_AUDIO:
700 offset = HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_HEADER;
701 break;
702
703 case HDMI_INFOFRAME_TYPE_VENDOR:
704 offset = HDMI_NV_PDISP_HDMI_GENERIC_HEADER;
705 break;
706
707 default:
708 dev_err(hdmi->dev, "unsupported infoframe type: %02x\n",
709 ptr[0]);
710 return;
711 }
712
713 value = INFOFRAME_HEADER_TYPE(ptr[0]) |
714 INFOFRAME_HEADER_VERSION(ptr[1]) |
715 INFOFRAME_HEADER_LEN(ptr[2]);
Thierry Redingedec4af2012-11-15 21:28:23 +0000716 tegra_hdmi_writel(hdmi, value, offset);
Thierry Redingac24c222012-11-23 15:14:00 +0100717 offset++;
Thierry Redingedec4af2012-11-15 21:28:23 +0000718
Thierry Redingac24c222012-11-23 15:14:00 +0100719 /*
720 * Each subpack contains 7 bytes, divided into:
721 * - subpack_low: bytes 0 - 3
722 * - subpack_high: bytes 4 - 6 (with byte 7 padded to 0x00)
Thierry Redingedec4af2012-11-15 21:28:23 +0000723 */
Thierry Redingac24c222012-11-23 15:14:00 +0100724 for (i = 3, j = 0; i < size; i += 7, j += 8) {
725 size_t rem = size - i, num = min_t(size_t, rem, 4);
Thierry Redingedec4af2012-11-15 21:28:23 +0000726
Thierry Redingac24c222012-11-23 15:14:00 +0100727 value = tegra_hdmi_subpack(&ptr[i], num);
728 tegra_hdmi_writel(hdmi, value, offset++);
Thierry Redingedec4af2012-11-15 21:28:23 +0000729
Thierry Redingac24c222012-11-23 15:14:00 +0100730 num = min_t(size_t, rem - num, 3);
Thierry Redingedec4af2012-11-15 21:28:23 +0000731
Thierry Redingac24c222012-11-23 15:14:00 +0100732 value = tegra_hdmi_subpack(&ptr[i + 4], num);
733 tegra_hdmi_writel(hdmi, value, offset++);
Thierry Redingedec4af2012-11-15 21:28:23 +0000734 }
735}
736
737static void tegra_hdmi_setup_avi_infoframe(struct tegra_hdmi *hdmi,
738 struct drm_display_mode *mode)
739{
740 struct hdmi_avi_infoframe frame;
Thierry Redingac24c222012-11-23 15:14:00 +0100741 u8 buffer[17];
742 ssize_t err;
Thierry Redingedec4af2012-11-15 21:28:23 +0000743
Shashank Sharma0c1f5282017-07-13 21:03:07 +0530744 err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode, false);
Thierry Redingac24c222012-11-23 15:14:00 +0100745 if (err < 0) {
746 dev_err(hdmi->dev, "failed to setup AVI infoframe: %zd\n", err);
747 return;
Thierry Redingedec4af2012-11-15 21:28:23 +0000748 }
749
Thierry Redingac24c222012-11-23 15:14:00 +0100750 err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
751 if (err < 0) {
752 dev_err(hdmi->dev, "failed to pack AVI infoframe: %zd\n", err);
753 return;
754 }
755
756 tegra_hdmi_write_infopack(hdmi, buffer, err);
Thierry Reding2ccb3962015-01-15 13:43:18 +0100757}
Thierry Redingedec4af2012-11-15 21:28:23 +0000758
Thierry Reding2ccb3962015-01-15 13:43:18 +0100759static void tegra_hdmi_disable_avi_infoframe(struct tegra_hdmi *hdmi)
760{
761 u32 value;
762
763 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_CTRL);
764 value &= ~INFOFRAME_CTRL_ENABLE;
765 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_CTRL);
766}
767
768static void tegra_hdmi_enable_avi_infoframe(struct tegra_hdmi *hdmi)
769{
770 u32 value;
771
772 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_CTRL);
773 value |= INFOFRAME_CTRL_ENABLE;
774 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_CTRL);
Thierry Redingedec4af2012-11-15 21:28:23 +0000775}
776
777static void tegra_hdmi_setup_audio_infoframe(struct tegra_hdmi *hdmi)
778{
779 struct hdmi_audio_infoframe frame;
Thierry Redingac24c222012-11-23 15:14:00 +0100780 u8 buffer[14];
781 ssize_t err;
Thierry Redingedec4af2012-11-15 21:28:23 +0000782
Thierry Redingac24c222012-11-23 15:14:00 +0100783 err = hdmi_audio_infoframe_init(&frame);
784 if (err < 0) {
Thierry Redingef284c72013-10-16 19:51:22 +0200785 dev_err(hdmi->dev, "failed to setup audio infoframe: %zd\n",
Thierry Redingac24c222012-11-23 15:14:00 +0100786 err);
787 return;
788 }
Thierry Redingedec4af2012-11-15 21:28:23 +0000789
Thierry Reding2ccb3962015-01-15 13:43:18 +0100790 frame.channels = hdmi->audio_channels;
Thierry Redingac24c222012-11-23 15:14:00 +0100791
792 err = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
793 if (err < 0) {
794 dev_err(hdmi->dev, "failed to pack audio infoframe: %zd\n",
795 err);
796 return;
797 }
798
799 /*
800 * The audio infoframe has only one set of subpack registers, so the
801 * infoframe needs to be truncated. One set of subpack registers can
802 * contain 7 bytes. Including the 3 byte header only the first 10
803 * bytes can be programmed.
804 */
Thierry Redingef284c72013-10-16 19:51:22 +0200805 tegra_hdmi_write_infopack(hdmi, buffer, min_t(size_t, 10, err));
Thierry Reding2ccb3962015-01-15 13:43:18 +0100806}
Thierry Redingedec4af2012-11-15 21:28:23 +0000807
Thierry Reding2ccb3962015-01-15 13:43:18 +0100808static void tegra_hdmi_disable_audio_infoframe(struct tegra_hdmi *hdmi)
809{
810 u32 value;
811
812 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_CTRL);
813 value &= ~INFOFRAME_CTRL_ENABLE;
814 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_CTRL);
815}
816
817static void tegra_hdmi_enable_audio_infoframe(struct tegra_hdmi *hdmi)
818{
819 u32 value;
820
821 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_CTRL);
822 value |= INFOFRAME_CTRL_ENABLE;
823 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_CTRL);
Thierry Redingedec4af2012-11-15 21:28:23 +0000824}
825
826static void tegra_hdmi_setup_stereo_infoframe(struct tegra_hdmi *hdmi)
827{
Lespiau, Damienae84b902013-08-19 16:59:02 +0100828 struct hdmi_vendor_infoframe frame;
Thierry Redingac24c222012-11-23 15:14:00 +0100829 u8 buffer[10];
830 ssize_t err;
Thierry Redingedec4af2012-11-15 21:28:23 +0000831
Lespiau, Damienae84b902013-08-19 16:59:02 +0100832 hdmi_vendor_infoframe_init(&frame);
Lespiau, Damiena26a58e82013-08-19 16:58:59 +0100833 frame.s3d_struct = HDMI_3D_STRUCTURE_FRAME_PACKING;
Thierry Redingac24c222012-11-23 15:14:00 +0100834
Lespiau, Damienae84b902013-08-19 16:59:02 +0100835 err = hdmi_vendor_infoframe_pack(&frame, buffer, sizeof(buffer));
Thierry Redingac24c222012-11-23 15:14:00 +0100836 if (err < 0) {
837 dev_err(hdmi->dev, "failed to pack vendor infoframe: %zd\n",
838 err);
839 return;
840 }
841
842 tegra_hdmi_write_infopack(hdmi, buffer, err);
Thierry Reding2ccb3962015-01-15 13:43:18 +0100843}
844
845static void tegra_hdmi_disable_stereo_infoframe(struct tegra_hdmi *hdmi)
846{
847 u32 value;
848
849 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
850 value &= ~GENERIC_CTRL_ENABLE;
851 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
852}
853
854static void tegra_hdmi_enable_stereo_infoframe(struct tegra_hdmi *hdmi)
855{
856 u32 value;
Thierry Redingedec4af2012-11-15 21:28:23 +0000857
858 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
859 value |= GENERIC_CTRL_ENABLE;
860 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
861}
862
863static void tegra_hdmi_setup_tmds(struct tegra_hdmi *hdmi,
864 const struct tmds_config *tmds)
865{
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100866 u32 value;
Thierry Redingedec4af2012-11-15 21:28:23 +0000867
868 tegra_hdmi_writel(hdmi, tmds->pll0, HDMI_NV_PDISP_SOR_PLL0);
869 tegra_hdmi_writel(hdmi, tmds->pll1, HDMI_NV_PDISP_SOR_PLL1);
870 tegra_hdmi_writel(hdmi, tmds->pe_current, HDMI_NV_PDISP_PE_CURRENT);
871
Thierry Reding59af0592013-10-14 09:43:05 +0200872 tegra_hdmi_writel(hdmi, tmds->drive_current,
873 HDMI_NV_PDISP_SOR_LANE_DRIVE_CURRENT);
874
875 value = tegra_hdmi_readl(hdmi, hdmi->config->fuse_override_offset);
876 value |= hdmi->config->fuse_override_value;
877 tegra_hdmi_writel(hdmi, value, hdmi->config->fuse_override_offset);
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +0200878
879 if (hdmi->config->has_sor_io_peak_current)
880 tegra_hdmi_writel(hdmi, tmds->peak_current,
881 HDMI_NV_PDISP_SOR_IO_PEAK_CURRENT);
Thierry Redingedec4af2012-11-15 21:28:23 +0000882}
883
Mikko Perttunen9f159122013-08-28 18:48:38 +0300884static bool tegra_output_is_hdmi(struct tegra_output *output)
885{
886 struct edid *edid;
887
888 if (!output->connector.edid_blob_ptr)
889 return false;
890
891 edid = (struct edid *)output->connector.edid_blob_ptr->data;
892
893 return drm_detect_hdmi_monitor(edid);
894}
895
Thierry Reding2ccb3962015-01-15 13:43:18 +0100896static enum drm_connector_status
897tegra_hdmi_connector_detect(struct drm_connector *connector, bool force)
898{
899 struct tegra_output *output = connector_to_output(connector);
900 struct tegra_hdmi *hdmi = to_hdmi(output);
901 enum drm_connector_status status;
902
903 status = tegra_output_connector_detect(connector, force);
904 if (status == connector_status_connected)
905 return status;
906
907 tegra_hdmi_writel(hdmi, 0, HDMI_NV_PDISP_SOR_AUDIO_HDA_PRESENSE);
908 return status;
909}
910
Thierry Reding59682712014-11-28 16:50:59 +0100911static const struct drm_connector_funcs tegra_hdmi_connector_funcs = {
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 Redingc03bf1b2015-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 Redingedec4af2012-11-15 21:28:23 +00001667 struct tegra_hdmi *hdmi;
1668 struct resource *regs;
1669 int err;
1670
1671 hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
1672 if (!hdmi)
1673 return -ENOMEM;
1674
Thierry Reding5e4acd32017-08-21 18:05:10 +02001675 hdmi->config = of_device_get_match_data(&pdev->dev);
Thierry Redingedec4af2012-11-15 21:28:23 +00001676 hdmi->dev = &pdev->dev;
Thierry Reding2ccb3962015-01-15 13:43:18 +01001677
Thierry Redingedec4af2012-11-15 21:28:23 +00001678 hdmi->audio_source = AUTO;
Thierry Reding2ccb3962015-01-15 13:43:18 +01001679 hdmi->audio_sample_rate = 48000;
1680 hdmi->audio_channels = 2;
Thierry Redingedec4af2012-11-15 21:28:23 +00001681 hdmi->stereo = false;
1682 hdmi->dvi = false;
1683
1684 hdmi->clk = devm_clk_get(&pdev->dev, NULL);
1685 if (IS_ERR(hdmi->clk)) {
1686 dev_err(&pdev->dev, "failed to get clock\n");
1687 return PTR_ERR(hdmi->clk);
1688 }
1689
Stephen Warrenca480802013-11-06 16:20:54 -07001690 hdmi->rst = devm_reset_control_get(&pdev->dev, "hdmi");
1691 if (IS_ERR(hdmi->rst)) {
1692 dev_err(&pdev->dev, "failed to get reset\n");
1693 return PTR_ERR(hdmi->rst);
1694 }
1695
Thierry Redingedec4af2012-11-15 21:28:23 +00001696 hdmi->clk_parent = devm_clk_get(&pdev->dev, "parent");
1697 if (IS_ERR(hdmi->clk_parent))
1698 return PTR_ERR(hdmi->clk_parent);
1699
Thierry Redingedec4af2012-11-15 21:28:23 +00001700 err = clk_set_parent(hdmi->clk, hdmi->clk_parent);
1701 if (err < 0) {
1702 dev_err(&pdev->dev, "failed to setup clocks: %d\n", err);
1703 return err;
1704 }
1705
Thierry Redingfb50a112014-02-28 16:57:34 +01001706 hdmi->hdmi = devm_regulator_get(&pdev->dev, "hdmi");
1707 if (IS_ERR(hdmi->hdmi)) {
1708 dev_err(&pdev->dev, "failed to get HDMI regulator\n");
1709 return PTR_ERR(hdmi->hdmi);
1710 }
1711
Thierry Redingedec4af2012-11-15 21:28:23 +00001712 hdmi->pll = devm_regulator_get(&pdev->dev, "pll");
1713 if (IS_ERR(hdmi->pll)) {
1714 dev_err(&pdev->dev, "failed to get PLL regulator\n");
1715 return PTR_ERR(hdmi->pll);
1716 }
1717
Thierry Reding88685682014-04-16 10:24:12 +02001718 hdmi->vdd = devm_regulator_get(&pdev->dev, "vdd");
1719 if (IS_ERR(hdmi->vdd)) {
1720 dev_err(&pdev->dev, "failed to get VDD regulator\n");
1721 return PTR_ERR(hdmi->vdd);
1722 }
1723
Thierry Redingedec4af2012-11-15 21:28:23 +00001724 hdmi->output.dev = &pdev->dev;
1725
Thierry Reding59d29c02013-10-14 14:26:42 +02001726 err = tegra_output_probe(&hdmi->output);
Thierry Redingedec4af2012-11-15 21:28:23 +00001727 if (err < 0)
1728 return err;
1729
1730 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Redingd4ed6022013-01-21 11:09:02 +01001731 hdmi->regs = devm_ioremap_resource(&pdev->dev, regs);
1732 if (IS_ERR(hdmi->regs))
1733 return PTR_ERR(hdmi->regs);
Thierry Redingedec4af2012-11-15 21:28:23 +00001734
1735 err = platform_get_irq(pdev, 0);
1736 if (err < 0)
1737 return err;
1738
1739 hdmi->irq = err;
1740
Thierry Reding2ccb3962015-01-15 13:43:18 +01001741 err = devm_request_irq(hdmi->dev, hdmi->irq, tegra_hdmi_irq, 0,
1742 dev_name(hdmi->dev), hdmi);
1743 if (err < 0) {
1744 dev_err(&pdev->dev, "failed to request IRQ#%u: %d\n",
1745 hdmi->irq, err);
1746 return err;
1747 }
1748
Thierry Reding52345492015-08-07 16:00:43 +02001749 platform_set_drvdata(pdev, hdmi);
1750 pm_runtime_enable(&pdev->dev);
1751
Thierry Reding776dc382013-10-14 14:43:22 +02001752 INIT_LIST_HEAD(&hdmi->client.list);
1753 hdmi->client.ops = &hdmi_client_ops;
1754 hdmi->client.dev = &pdev->dev;
Thierry Redingedec4af2012-11-15 21:28:23 +00001755
Thierry Reding776dc382013-10-14 14:43:22 +02001756 err = host1x_client_register(&hdmi->client);
Thierry Redingedec4af2012-11-15 21:28:23 +00001757 if (err < 0) {
1758 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
1759 err);
1760 return err;
1761 }
1762
Thierry Redingedec4af2012-11-15 21:28:23 +00001763 return 0;
1764}
1765
1766static int tegra_hdmi_remove(struct platform_device *pdev)
1767{
Thierry Redingedec4af2012-11-15 21:28:23 +00001768 struct tegra_hdmi *hdmi = platform_get_drvdata(pdev);
1769 int err;
1770
Thierry Reding52345492015-08-07 16:00:43 +02001771 pm_runtime_disable(&pdev->dev);
1772
Thierry Reding776dc382013-10-14 14:43:22 +02001773 err = host1x_client_unregister(&hdmi->client);
Thierry Redingedec4af2012-11-15 21:28:23 +00001774 if (err < 0) {
1775 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1776 err);
1777 return err;
1778 }
1779
Thierry Reding328ec692014-12-19 15:55:08 +01001780 tegra_output_remove(&hdmi->output);
Thierry Reding59d29c02013-10-14 14:26:42 +02001781
Thierry Reding52345492015-08-07 16:00:43 +02001782 return 0;
1783}
1784
1785#ifdef CONFIG_PM
1786static int tegra_hdmi_suspend(struct device *dev)
1787{
1788 struct tegra_hdmi *hdmi = dev_get_drvdata(dev);
1789 int err;
1790
1791 err = reset_control_assert(hdmi->rst);
1792 if (err < 0) {
1793 dev_err(dev, "failed to assert reset: %d\n", err);
1794 return err;
1795 }
1796
1797 usleep_range(1000, 2000);
1798
Thierry Redingd06e7f82014-04-16 10:43:41 +02001799 clk_disable_unprepare(hdmi->clk);
Thierry Redingedec4af2012-11-15 21:28:23 +00001800
1801 return 0;
1802}
1803
Thierry Reding52345492015-08-07 16:00:43 +02001804static int tegra_hdmi_resume(struct device *dev)
1805{
1806 struct tegra_hdmi *hdmi = dev_get_drvdata(dev);
1807 int err;
1808
1809 err = clk_prepare_enable(hdmi->clk);
1810 if (err < 0) {
1811 dev_err(dev, "failed to enable clock: %d\n", err);
1812 return err;
1813 }
1814
1815 usleep_range(1000, 2000);
1816
1817 err = reset_control_deassert(hdmi->rst);
1818 if (err < 0) {
1819 dev_err(dev, "failed to deassert reset: %d\n", err);
1820 clk_disable_unprepare(hdmi->clk);
1821 return err;
1822 }
1823
1824 return 0;
1825}
1826#endif
1827
1828static const struct dev_pm_ops tegra_hdmi_pm_ops = {
1829 SET_RUNTIME_PM_OPS(tegra_hdmi_suspend, tegra_hdmi_resume, NULL)
1830};
1831
Thierry Redingedec4af2012-11-15 21:28:23 +00001832struct platform_driver tegra_hdmi_driver = {
1833 .driver = {
1834 .name = "tegra-hdmi",
Thierry Redingedec4af2012-11-15 21:28:23 +00001835 .of_match_table = tegra_hdmi_of_match,
Thierry Reding52345492015-08-07 16:00:43 +02001836 .pm = &tegra_hdmi_pm_ops,
Thierry Redingedec4af2012-11-15 21:28:23 +00001837 },
1838 .probe = tegra_hdmi_probe,
1839 .remove = tegra_hdmi_remove,
1840};