blob: 3ce551911548613eb4bad1965852b0f693483b59 [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>
Alban Bedeldb5adf42016-12-14 18:20:39 +010014#include <linux/math64.h>
Thierry Reding5e4acd32017-08-21 18:05:10 +020015#include <linux/of_device.h>
Thierry Reding52345492015-08-07 16:00:43 +020016#include <linux/pm_runtime.h>
Thierry Reding9eb9b222013-09-24 16:32:47 +020017#include <linux/regulator/consumer.h>
Stephen Warrenca480802013-11-06 16:20:54 -070018#include <linux/reset.h>
Thierry Redingac24c222012-11-23 15:14:00 +010019
Thierry Reding4aa3df72014-11-24 16:27:13 +010020#include <drm/drm_atomic_helper.h>
Thierry Reding59682712014-11-28 16:50:59 +010021#include <drm/drm_crtc.h>
22#include <drm/drm_crtc_helper.h>
23
Thierry Redinge3c702d2019-01-03 15:23:17 +010024#include "hda.h"
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 Redinge3c702d2019-01-03 15:23:17 +010072 struct tegra_hda_format format;
Thierry Reding2ccb3962015-01-15 13:43:18 +010073
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;
Thierry Redingedec4af2012-11-15 21:28:23 +000079};
80
81static inline struct tegra_hdmi *
Thierry Reding776dc382013-10-14 14:43:22 +020082host1x_client_to_hdmi(struct host1x_client *client)
Thierry Redingedec4af2012-11-15 21:28:23 +000083{
84 return container_of(client, struct tegra_hdmi, client);
85}
86
87static inline struct tegra_hdmi *to_hdmi(struct tegra_output *output)
88{
89 return container_of(output, struct tegra_hdmi, output);
90}
91
92#define HDMI_AUDIOCLK_FREQ 216000000
93#define HDMI_REKEY_DEFAULT 56
94
95enum {
96 AUTO = 0,
97 SPDIF,
98 HDA,
99};
100
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100101static inline u32 tegra_hdmi_readl(struct tegra_hdmi *hdmi,
Thierry Reding7efe20c2017-08-15 15:41:08 +0200102 unsigned int offset)
Thierry Redingedec4af2012-11-15 21:28:23 +0000103{
Thierry Reding07a8aab2017-08-15 15:41:11 +0200104 u32 value = readl(hdmi->regs + (offset << 2));
105
106 trace_hdmi_readl(hdmi->dev, offset, value);
107
108 return value;
Thierry Redingedec4af2012-11-15 21:28:23 +0000109}
110
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100111static inline void tegra_hdmi_writel(struct tegra_hdmi *hdmi, u32 value,
Thierry Reding7efe20c2017-08-15 15:41:08 +0200112 unsigned int offset)
Thierry Redingedec4af2012-11-15 21:28:23 +0000113{
Thierry Reding07a8aab2017-08-15 15:41:11 +0200114 trace_hdmi_writel(hdmi->dev, offset, value);
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100115 writel(value, hdmi->regs + (offset << 2));
Thierry Redingedec4af2012-11-15 21:28:23 +0000116}
117
118struct tegra_hdmi_audio_config {
Thierry Redingedec4af2012-11-15 21:28:23 +0000119 unsigned int n;
120 unsigned int cts;
121 unsigned int aval;
122};
123
Thierry Redingf27db962013-09-30 15:14:41 +0200124static const struct tmds_config tegra20_tmds_config[] = {
Lucas Stachfa416dd2012-12-19 21:38:55 +0000125 { /* slow pixel clock modes */
Thierry Redingedec4af2012-11-15 21:28:23 +0000126 .pclk = 27000000,
127 .pll0 = SOR_PLL_BG_V17_S(3) | SOR_PLL_ICHPMP(1) |
128 SOR_PLL_RESISTORSEL | SOR_PLL_VCOCAP(0) |
129 SOR_PLL_TX_REG_LOAD(3),
130 .pll1 = SOR_PLL_TMDS_TERM_ENABLE,
131 .pe_current = PE_CURRENT0(PE_CURRENT_0_0_mA) |
132 PE_CURRENT1(PE_CURRENT_0_0_mA) |
133 PE_CURRENT2(PE_CURRENT_0_0_mA) |
134 PE_CURRENT3(PE_CURRENT_0_0_mA),
135 .drive_current = DRIVE_CURRENT_LANE0(DRIVE_CURRENT_7_125_mA) |
136 DRIVE_CURRENT_LANE1(DRIVE_CURRENT_7_125_mA) |
137 DRIVE_CURRENT_LANE2(DRIVE_CURRENT_7_125_mA) |
138 DRIVE_CURRENT_LANE3(DRIVE_CURRENT_7_125_mA),
Lucas Stachfa416dd2012-12-19 21:38:55 +0000139 },
140 { /* high pixel clock modes */
Thierry Redingedec4af2012-11-15 21:28:23 +0000141 .pclk = UINT_MAX,
142 .pll0 = SOR_PLL_BG_V17_S(3) | SOR_PLL_ICHPMP(1) |
143 SOR_PLL_RESISTORSEL | SOR_PLL_VCOCAP(1) |
144 SOR_PLL_TX_REG_LOAD(3),
145 .pll1 = SOR_PLL_TMDS_TERM_ENABLE | SOR_PLL_PE_EN,
146 .pe_current = PE_CURRENT0(PE_CURRENT_6_0_mA) |
147 PE_CURRENT1(PE_CURRENT_6_0_mA) |
148 PE_CURRENT2(PE_CURRENT_6_0_mA) |
149 PE_CURRENT3(PE_CURRENT_6_0_mA),
150 .drive_current = DRIVE_CURRENT_LANE0(DRIVE_CURRENT_7_125_mA) |
151 DRIVE_CURRENT_LANE1(DRIVE_CURRENT_7_125_mA) |
152 DRIVE_CURRENT_LANE2(DRIVE_CURRENT_7_125_mA) |
153 DRIVE_CURRENT_LANE3(DRIVE_CURRENT_7_125_mA),
154 },
155};
156
Thierry Redingf27db962013-09-30 15:14:41 +0200157static const struct tmds_config tegra30_tmds_config[] = {
Thierry Redingedec4af2012-11-15 21:28:23 +0000158 { /* 480p modes */
159 .pclk = 27000000,
160 .pll0 = SOR_PLL_BG_V17_S(3) | SOR_PLL_ICHPMP(1) |
161 SOR_PLL_RESISTORSEL | SOR_PLL_VCOCAP(0) |
162 SOR_PLL_TX_REG_LOAD(0),
163 .pll1 = SOR_PLL_TMDS_TERM_ENABLE,
164 .pe_current = PE_CURRENT0(PE_CURRENT_0_0_mA) |
165 PE_CURRENT1(PE_CURRENT_0_0_mA) |
166 PE_CURRENT2(PE_CURRENT_0_0_mA) |
167 PE_CURRENT3(PE_CURRENT_0_0_mA),
168 .drive_current = DRIVE_CURRENT_LANE0(DRIVE_CURRENT_5_250_mA) |
169 DRIVE_CURRENT_LANE1(DRIVE_CURRENT_5_250_mA) |
170 DRIVE_CURRENT_LANE2(DRIVE_CURRENT_5_250_mA) |
171 DRIVE_CURRENT_LANE3(DRIVE_CURRENT_5_250_mA),
172 }, { /* 720p modes */
173 .pclk = 74250000,
174 .pll0 = SOR_PLL_BG_V17_S(3) | SOR_PLL_ICHPMP(1) |
175 SOR_PLL_RESISTORSEL | SOR_PLL_VCOCAP(1) |
176 SOR_PLL_TX_REG_LOAD(0),
177 .pll1 = SOR_PLL_TMDS_TERM_ENABLE | SOR_PLL_PE_EN,
178 .pe_current = PE_CURRENT0(PE_CURRENT_5_0_mA) |
179 PE_CURRENT1(PE_CURRENT_5_0_mA) |
180 PE_CURRENT2(PE_CURRENT_5_0_mA) |
181 PE_CURRENT3(PE_CURRENT_5_0_mA),
182 .drive_current = DRIVE_CURRENT_LANE0(DRIVE_CURRENT_5_250_mA) |
183 DRIVE_CURRENT_LANE1(DRIVE_CURRENT_5_250_mA) |
184 DRIVE_CURRENT_LANE2(DRIVE_CURRENT_5_250_mA) |
185 DRIVE_CURRENT_LANE3(DRIVE_CURRENT_5_250_mA),
186 }, { /* 1080p modes */
187 .pclk = UINT_MAX,
188 .pll0 = SOR_PLL_BG_V17_S(3) | SOR_PLL_ICHPMP(1) |
189 SOR_PLL_RESISTORSEL | SOR_PLL_VCOCAP(3) |
190 SOR_PLL_TX_REG_LOAD(0),
191 .pll1 = SOR_PLL_TMDS_TERM_ENABLE | SOR_PLL_PE_EN,
192 .pe_current = PE_CURRENT0(PE_CURRENT_5_0_mA) |
193 PE_CURRENT1(PE_CURRENT_5_0_mA) |
194 PE_CURRENT2(PE_CURRENT_5_0_mA) |
195 PE_CURRENT3(PE_CURRENT_5_0_mA),
196 .drive_current = DRIVE_CURRENT_LANE0(DRIVE_CURRENT_5_250_mA) |
197 DRIVE_CURRENT_LANE1(DRIVE_CURRENT_5_250_mA) |
198 DRIVE_CURRENT_LANE2(DRIVE_CURRENT_5_250_mA) |
199 DRIVE_CURRENT_LANE3(DRIVE_CURRENT_5_250_mA),
200 },
201};
202
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +0200203static const struct tmds_config tegra114_tmds_config[] = {
204 { /* 480p/576p / 25.2MHz/27MHz modes */
205 .pclk = 27000000,
206 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
207 SOR_PLL_VCOCAP(0) | SOR_PLL_RESISTORSEL,
208 .pll1 = SOR_PLL_LOADADJ(3) | SOR_PLL_TMDS_TERMADJ(0),
209 .pe_current = PE_CURRENT0(PE_CURRENT_0_mA_T114) |
210 PE_CURRENT1(PE_CURRENT_0_mA_T114) |
211 PE_CURRENT2(PE_CURRENT_0_mA_T114) |
212 PE_CURRENT3(PE_CURRENT_0_mA_T114),
213 .drive_current =
214 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_10_400_mA_T114) |
215 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_10_400_mA_T114) |
216 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_10_400_mA_T114) |
217 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_10_400_mA_T114),
218 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_0_000_mA) |
219 PEAK_CURRENT_LANE1(PEAK_CURRENT_0_000_mA) |
220 PEAK_CURRENT_LANE2(PEAK_CURRENT_0_000_mA) |
221 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_000_mA),
222 }, { /* 720p / 74.25MHz modes */
223 .pclk = 74250000,
224 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
225 SOR_PLL_VCOCAP(1) | SOR_PLL_RESISTORSEL,
226 .pll1 = SOR_PLL_PE_EN | SOR_PLL_LOADADJ(3) |
227 SOR_PLL_TMDS_TERMADJ(0),
228 .pe_current = PE_CURRENT0(PE_CURRENT_15_mA_T114) |
229 PE_CURRENT1(PE_CURRENT_15_mA_T114) |
230 PE_CURRENT2(PE_CURRENT_15_mA_T114) |
231 PE_CURRENT3(PE_CURRENT_15_mA_T114),
232 .drive_current =
233 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_10_400_mA_T114) |
234 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_10_400_mA_T114) |
235 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_10_400_mA_T114) |
236 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_10_400_mA_T114),
237 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_0_000_mA) |
238 PEAK_CURRENT_LANE1(PEAK_CURRENT_0_000_mA) |
239 PEAK_CURRENT_LANE2(PEAK_CURRENT_0_000_mA) |
240 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_000_mA),
241 }, { /* 1080p / 148.5MHz modes */
242 .pclk = 148500000,
243 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
244 SOR_PLL_VCOCAP(3) | SOR_PLL_RESISTORSEL,
245 .pll1 = SOR_PLL_PE_EN | SOR_PLL_LOADADJ(3) |
246 SOR_PLL_TMDS_TERMADJ(0),
247 .pe_current = PE_CURRENT0(PE_CURRENT_10_mA_T114) |
248 PE_CURRENT1(PE_CURRENT_10_mA_T114) |
249 PE_CURRENT2(PE_CURRENT_10_mA_T114) |
250 PE_CURRENT3(PE_CURRENT_10_mA_T114),
251 .drive_current =
252 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_12_400_mA_T114) |
253 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_12_400_mA_T114) |
254 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_12_400_mA_T114) |
255 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_12_400_mA_T114),
256 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_0_000_mA) |
257 PEAK_CURRENT_LANE1(PEAK_CURRENT_0_000_mA) |
258 PEAK_CURRENT_LANE2(PEAK_CURRENT_0_000_mA) |
259 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_000_mA),
260 }, { /* 225/297MHz modes */
261 .pclk = UINT_MAX,
262 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
263 SOR_PLL_VCOCAP(0xf) | SOR_PLL_RESISTORSEL,
264 .pll1 = SOR_PLL_LOADADJ(3) | SOR_PLL_TMDS_TERMADJ(7)
265 | SOR_PLL_TMDS_TERM_ENABLE,
266 .pe_current = PE_CURRENT0(PE_CURRENT_0_mA_T114) |
267 PE_CURRENT1(PE_CURRENT_0_mA_T114) |
268 PE_CURRENT2(PE_CURRENT_0_mA_T114) |
269 PE_CURRENT3(PE_CURRENT_0_mA_T114),
270 .drive_current =
271 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_25_200_mA_T114) |
272 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_25_200_mA_T114) |
273 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_25_200_mA_T114) |
274 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_19_200_mA_T114),
275 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_3_000_mA) |
276 PEAK_CURRENT_LANE1(PEAK_CURRENT_3_000_mA) |
277 PEAK_CURRENT_LANE2(PEAK_CURRENT_3_000_mA) |
278 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_800_mA),
279 },
280};
281
Thierry Redingfb7be702013-11-15 16:07:32 +0100282static const struct tmds_config tegra124_tmds_config[] = {
283 { /* 480p/576p / 25.2MHz/27MHz modes */
284 .pclk = 27000000,
285 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
286 SOR_PLL_VCOCAP(0) | SOR_PLL_RESISTORSEL,
287 .pll1 = SOR_PLL_LOADADJ(3) | SOR_PLL_TMDS_TERMADJ(0),
288 .pe_current = PE_CURRENT0(PE_CURRENT_0_mA_T114) |
289 PE_CURRENT1(PE_CURRENT_0_mA_T114) |
290 PE_CURRENT2(PE_CURRENT_0_mA_T114) |
291 PE_CURRENT3(PE_CURRENT_0_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 }, { /* 720p / 74.25MHz modes */
302 .pclk = 74250000,
303 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
304 SOR_PLL_VCOCAP(1) | 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_15_mA_T114) |
308 PE_CURRENT1(PE_CURRENT_15_mA_T114) |
309 PE_CURRENT2(PE_CURRENT_15_mA_T114) |
310 PE_CURRENT3(PE_CURRENT_15_mA_T114),
311 .drive_current =
312 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_10_400_mA_T114) |
313 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_10_400_mA_T114) |
314 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_10_400_mA_T114) |
315 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_10_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 }, { /* 1080p / 148.5MHz modes */
321 .pclk = 148500000,
322 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
323 SOR_PLL_VCOCAP(3) | SOR_PLL_RESISTORSEL,
324 .pll1 = SOR_PLL_PE_EN | SOR_PLL_LOADADJ(3) |
325 SOR_PLL_TMDS_TERMADJ(0),
326 .pe_current = PE_CURRENT0(PE_CURRENT_10_mA_T114) |
327 PE_CURRENT1(PE_CURRENT_10_mA_T114) |
328 PE_CURRENT2(PE_CURRENT_10_mA_T114) |
329 PE_CURRENT3(PE_CURRENT_10_mA_T114),
330 .drive_current =
331 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_12_400_mA_T114) |
332 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_12_400_mA_T114) |
333 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_12_400_mA_T114) |
334 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_12_400_mA_T114),
335 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_0_000_mA) |
336 PEAK_CURRENT_LANE1(PEAK_CURRENT_0_000_mA) |
337 PEAK_CURRENT_LANE2(PEAK_CURRENT_0_000_mA) |
338 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_000_mA),
339 }, { /* 225/297MHz modes */
340 .pclk = UINT_MAX,
341 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
342 SOR_PLL_VCOCAP(0xf) | SOR_PLL_RESISTORSEL,
343 .pll1 = SOR_PLL_LOADADJ(3) | SOR_PLL_TMDS_TERMADJ(7)
344 | SOR_PLL_TMDS_TERM_ENABLE,
345 .pe_current = PE_CURRENT0(PE_CURRENT_0_mA_T114) |
346 PE_CURRENT1(PE_CURRENT_0_mA_T114) |
347 PE_CURRENT2(PE_CURRENT_0_mA_T114) |
348 PE_CURRENT3(PE_CURRENT_0_mA_T114),
349 .drive_current =
350 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_25_200_mA_T114) |
351 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_25_200_mA_T114) |
352 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_25_200_mA_T114) |
353 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_19_200_mA_T114),
354 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_3_000_mA) |
355 PEAK_CURRENT_LANE1(PEAK_CURRENT_3_000_mA) |
356 PEAK_CURRENT_LANE2(PEAK_CURRENT_3_000_mA) |
357 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_800_mA),
358 },
359};
360
Alban Bedeldb5adf42016-12-14 18:20:39 +0100361static int
362tegra_hdmi_get_audio_config(unsigned int audio_freq, unsigned int pix_clock,
363 struct tegra_hdmi_audio_config *config)
Thierry Redingedec4af2012-11-15 21:28:23 +0000364{
Alban Bedeldb5adf42016-12-14 18:20:39 +0100365 const unsigned int afreq = 128 * audio_freq;
366 const unsigned int min_n = afreq / 1500;
367 const unsigned int max_n = afreq / 300;
368 const unsigned int ideal_n = afreq / 1000;
369 int64_t min_err = (uint64_t)-1 >> 1;
370 unsigned int min_delta = -1;
371 int n;
Thierry Redingedec4af2012-11-15 21:28:23 +0000372
Alban Bedeldb5adf42016-12-14 18:20:39 +0100373 memset(config, 0, sizeof(*config));
374 config->n = -1;
Thierry Redingedec4af2012-11-15 21:28:23 +0000375
Alban Bedeldb5adf42016-12-14 18:20:39 +0100376 for (n = min_n; n <= max_n; n++) {
377 uint64_t cts_f, aval_f;
378 unsigned int delta;
379 int64_t cts, err;
Thierry Redingedec4af2012-11-15 21:28:23 +0000380
Alban Bedeldb5adf42016-12-14 18:20:39 +0100381 /* compute aval in 48.16 fixed point */
382 aval_f = ((int64_t)24000000 << 16) * n;
383 do_div(aval_f, afreq);
384 /* It should round without any rest */
385 if (aval_f & 0xFFFF)
386 continue;
Thierry Redingedec4af2012-11-15 21:28:23 +0000387
Alban Bedeldb5adf42016-12-14 18:20:39 +0100388 /* Compute cts in 48.16 fixed point */
389 cts_f = ((int64_t)pix_clock << 16) * n;
390 do_div(cts_f, afreq);
391 /* Round it to the nearest integer */
392 cts = (cts_f & ~0xFFFF) + ((cts_f & BIT(15)) << 1);
Thierry Redingedec4af2012-11-15 21:28:23 +0000393
Alban Bedeldb5adf42016-12-14 18:20:39 +0100394 delta = abs(n - ideal_n);
Thierry Redingedec4af2012-11-15 21:28:23 +0000395
Alban Bedeldb5adf42016-12-14 18:20:39 +0100396 /* Compute the absolute error */
397 err = abs((int64_t)cts_f - cts);
398 if (err < min_err || (err == min_err && delta < min_delta)) {
399 config->n = n;
400 config->cts = cts >> 16;
401 config->aval = aval_f >> 16;
402 min_delta = delta;
403 min_err = err;
404 }
Thierry Redingedec4af2012-11-15 21:28:23 +0000405 }
406
Alban Bedeldb5adf42016-12-14 18:20:39 +0100407 return config->n != -1 ? 0 : -EINVAL;
Thierry Redingedec4af2012-11-15 21:28:23 +0000408}
409
410static void tegra_hdmi_setup_audio_fs_tables(struct tegra_hdmi *hdmi)
411{
412 const unsigned int freqs[] = {
413 32000, 44100, 48000, 88200, 96000, 176400, 192000
414 };
415 unsigned int i;
416
417 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
418 unsigned int f = freqs[i];
419 unsigned int eight_half;
Thierry Redingedec4af2012-11-15 21:28:23 +0000420 unsigned int delta;
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100421 u32 value;
Thierry Redingedec4af2012-11-15 21:28:23 +0000422
423 if (f > 96000)
424 delta = 2;
Thierry Reding17a8b6b2013-12-16 10:01:24 +0100425 else if (f > 48000)
Thierry Redingedec4af2012-11-15 21:28:23 +0000426 delta = 6;
427 else
428 delta = 9;
429
430 eight_half = (8 * HDMI_AUDIOCLK_FREQ) / (f * 128);
431 value = AUDIO_FS_LOW(eight_half - delta) |
432 AUDIO_FS_HIGH(eight_half + delta);
433 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_AUDIO_FS(i));
434 }
435}
436
Thierry Reding2ccb3962015-01-15 13:43:18 +0100437static void tegra_hdmi_write_aval(struct tegra_hdmi *hdmi, u32 value)
Thierry Redingedec4af2012-11-15 21:28:23 +0000438{
Thierry Reding2ccb3962015-01-15 13:43:18 +0100439 static const struct {
440 unsigned int sample_rate;
441 unsigned int offset;
442 } regs[] = {
443 { 32000, HDMI_NV_PDISP_SOR_AUDIO_AVAL_0320 },
444 { 44100, HDMI_NV_PDISP_SOR_AUDIO_AVAL_0441 },
445 { 48000, HDMI_NV_PDISP_SOR_AUDIO_AVAL_0480 },
446 { 88200, HDMI_NV_PDISP_SOR_AUDIO_AVAL_0882 },
447 { 96000, HDMI_NV_PDISP_SOR_AUDIO_AVAL_0960 },
448 { 176400, HDMI_NV_PDISP_SOR_AUDIO_AVAL_1764 },
449 { 192000, HDMI_NV_PDISP_SOR_AUDIO_AVAL_1920 },
450 };
451 unsigned int i;
452
453 for (i = 0; i < ARRAY_SIZE(regs); i++) {
Thierry Redinge3c702d2019-01-03 15:23:17 +0100454 if (regs[i].sample_rate == hdmi->format.sample_rate) {
Thierry Reding2ccb3962015-01-15 13:43:18 +0100455 tegra_hdmi_writel(hdmi, value, regs[i].offset);
456 break;
457 }
458 }
459}
460
461static int tegra_hdmi_setup_audio(struct tegra_hdmi *hdmi)
462{
Alban Bedeldb5adf42016-12-14 18:20:39 +0100463 struct tegra_hdmi_audio_config config;
Thierry Reding2ccb3962015-01-15 13:43:18 +0100464 u32 source, value;
Alban Bedeldb5adf42016-12-14 18:20:39 +0100465 int err;
Thierry Redingedec4af2012-11-15 21:28:23 +0000466
467 switch (hdmi->audio_source) {
468 case HDA:
Thierry Reding2ccb3962015-01-15 13:43:18 +0100469 if (hdmi->config->has_hda)
470 source = SOR_AUDIO_CNTRL0_SOURCE_SELECT_HDAL;
471 else
472 return -EINVAL;
473
Thierry Redingedec4af2012-11-15 21:28:23 +0000474 break;
475
476 case SPDIF:
Thierry Reding2ccb3962015-01-15 13:43:18 +0100477 if (hdmi->config->has_hda)
478 source = SOR_AUDIO_CNTRL0_SOURCE_SELECT_SPDIF;
479 else
480 source = AUDIO_CNTRL0_SOURCE_SELECT_SPDIF;
Thierry Redingedec4af2012-11-15 21:28:23 +0000481 break;
482
483 default:
Thierry Reding2ccb3962015-01-15 13:43:18 +0100484 if (hdmi->config->has_hda)
485 source = SOR_AUDIO_CNTRL0_SOURCE_SELECT_AUTO;
486 else
487 source = AUDIO_CNTRL0_SOURCE_SELECT_AUTO;
Thierry Redingedec4af2012-11-15 21:28:23 +0000488 break;
489 }
490
Thierry Reding2ccb3962015-01-15 13:43:18 +0100491 /*
492 * Tegra30 and later use a slightly modified version of the register
493 * layout to accomodate for changes related to supporting HDA as the
494 * audio input source for HDMI. The source select field has moved to
495 * the SOR_AUDIO_CNTRL0 register, but the error tolerance and frames
496 * per block fields remain in the AUDIO_CNTRL0 register.
497 */
498 if (hdmi->config->has_hda) {
499 /*
500 * Inject null samples into the audio FIFO for every frame in
501 * which the codec did not receive any samples. This applies
502 * to stereo LPCM only.
503 *
504 * XXX: This seems to be a remnant of MCP days when this was
505 * used to work around issues with monitors not being able to
506 * play back system startup sounds early. It is possibly not
507 * needed on Linux at all.
508 */
Thierry Redinge3c702d2019-01-03 15:23:17 +0100509 if (hdmi->format.channels == 2)
Thierry Reding2ccb3962015-01-15 13:43:18 +0100510 value = SOR_AUDIO_CNTRL0_INJECT_NULLSMPL;
511 else
512 value = 0;
Thierry Redingedec4af2012-11-15 21:28:23 +0000513
Thierry Reding2ccb3962015-01-15 13:43:18 +0100514 value |= source;
515
516 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_AUDIO_CNTRL0);
Thierry Redingedec4af2012-11-15 21:28:23 +0000517 }
518
Thierry Reding2ccb3962015-01-15 13:43:18 +0100519 /*
520 * On Tegra20, HDA is not a supported audio source and the source
521 * select field is part of the AUDIO_CNTRL0 register.
522 */
523 value = AUDIO_CNTRL0_FRAMES_PER_BLOCK(0xc0) |
524 AUDIO_CNTRL0_ERROR_TOLERANCE(6);
525
526 if (!hdmi->config->has_hda)
527 value |= source;
528
529 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_AUDIO_CNTRL0);
530
531 /*
532 * Advertise support for High Bit-Rate on Tegra114 and later.
533 */
534 if (hdmi->config->has_hbr) {
535 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_SOR_AUDIO_SPARE0);
536 value |= SOR_AUDIO_SPARE0_HBR_ENABLE;
537 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_AUDIO_SPARE0);
538 }
539
Alban Bedeldb5adf42016-12-14 18:20:39 +0100540 err = tegra_hdmi_get_audio_config(hdmi->format.sample_rate,
541 hdmi->pixel_clock, &config);
542 if (err < 0) {
Thierry Reding2ccb3962015-01-15 13:43:18 +0100543 dev_err(hdmi->dev,
544 "cannot set audio to %u Hz at %u Hz pixel clock\n",
Thierry Redinge3c702d2019-01-03 15:23:17 +0100545 hdmi->format.sample_rate, hdmi->pixel_clock);
Alban Bedeldb5adf42016-12-14 18:20:39 +0100546 return err;
Thierry Redingedec4af2012-11-15 21:28:23 +0000547 }
548
Alban Bedeldb5adf42016-12-14 18:20:39 +0100549 dev_dbg(hdmi->dev, "audio: pixclk=%u, n=%u, cts=%u, aval=%u\n",
550 hdmi->pixel_clock, config.n, config.cts, config.aval);
551
Thierry Redingedec4af2012-11-15 21:28:23 +0000552 tegra_hdmi_writel(hdmi, 0, HDMI_NV_PDISP_HDMI_ACR_CTRL);
553
554 value = AUDIO_N_RESETF | AUDIO_N_GENERATE_ALTERNATE |
Alban Bedeldb5adf42016-12-14 18:20:39 +0100555 AUDIO_N_VALUE(config.n - 1);
Thierry Redingedec4af2012-11-15 21:28:23 +0000556 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_AUDIO_N);
557
Alban Bedeldb5adf42016-12-14 18:20:39 +0100558 tegra_hdmi_writel(hdmi, ACR_SUBPACK_N(config.n) | ACR_ENABLE,
Thierry Redingedec4af2012-11-15 21:28:23 +0000559 HDMI_NV_PDISP_HDMI_ACR_0441_SUBPACK_HIGH);
560
Alban Bedeldb5adf42016-12-14 18:20:39 +0100561 tegra_hdmi_writel(hdmi, ACR_SUBPACK_CTS(config.cts),
Thierry Reding2ccb3962015-01-15 13:43:18 +0100562 HDMI_NV_PDISP_HDMI_ACR_0441_SUBPACK_LOW);
Thierry Redingedec4af2012-11-15 21:28:23 +0000563
564 value = SPARE_HW_CTS | SPARE_FORCE_SW_CTS | SPARE_CTS_RESET_VAL(1);
565 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_SPARE);
566
567 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_AUDIO_N);
568 value &= ~AUDIO_N_RESETF;
569 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_AUDIO_N);
570
Thierry Reding2ccb3962015-01-15 13:43:18 +0100571 if (hdmi->config->has_hda)
Alban Bedeldb5adf42016-12-14 18:20:39 +0100572 tegra_hdmi_write_aval(hdmi, config.aval);
Thierry Redingedec4af2012-11-15 21:28:23 +0000573
574 tegra_hdmi_setup_audio_fs_tables(hdmi);
575
576 return 0;
577}
578
Thierry Reding2ccb3962015-01-15 13:43:18 +0100579static void tegra_hdmi_disable_audio(struct tegra_hdmi *hdmi)
580{
581 u32 value;
582
583 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
584 value &= ~GENERIC_CTRL_AUDIO;
585 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
586}
587
588static void tegra_hdmi_enable_audio(struct tegra_hdmi *hdmi)
589{
590 u32 value;
591
592 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
593 value |= GENERIC_CTRL_AUDIO;
594 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
595}
596
Thierry Reding52345492015-08-07 16:00:43 +0200597static void tegra_hdmi_write_eld(struct tegra_hdmi *hdmi)
598{
599 size_t length = drm_eld_size(hdmi->output.connector.eld), i;
600 u32 value;
601
602 for (i = 0; i < length; i++)
603 tegra_hdmi_writel(hdmi, i << 8 | hdmi->output.connector.eld[i],
604 HDMI_NV_PDISP_SOR_AUDIO_HDA_ELD_BUFWR);
605
606 /*
607 * The HDA codec will always report an ELD buffer size of 96 bytes and
608 * the HDA codec driver will check that each byte read from the buffer
609 * is valid. Therefore every byte must be written, even if no 96 bytes
610 * were parsed from EDID.
611 */
612 for (i = length; i < HDMI_ELD_BUFFER_SIZE; i++)
613 tegra_hdmi_writel(hdmi, i << 8 | 0,
614 HDMI_NV_PDISP_SOR_AUDIO_HDA_ELD_BUFWR);
615
616 value = SOR_AUDIO_HDA_PRESENSE_VALID | SOR_AUDIO_HDA_PRESENSE_PRESENT;
617 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_AUDIO_HDA_PRESENSE);
618}
619
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100620static inline u32 tegra_hdmi_subpack(const u8 *ptr, size_t size)
Thierry Redingedec4af2012-11-15 21:28:23 +0000621{
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100622 u32 value = 0;
Thierry Redingedec4af2012-11-15 21:28:23 +0000623 size_t i;
Thierry Redingedec4af2012-11-15 21:28:23 +0000624
Thierry Redingac24c222012-11-23 15:14:00 +0100625 for (i = size; i > 0; i--)
626 value = (value << 8) | ptr[i - 1];
Thierry Redingedec4af2012-11-15 21:28:23 +0000627
Thierry Redingac24c222012-11-23 15:14:00 +0100628 return value;
629}
Thierry Redingedec4af2012-11-15 21:28:23 +0000630
Thierry Redingac24c222012-11-23 15:14:00 +0100631static void tegra_hdmi_write_infopack(struct tegra_hdmi *hdmi, const void *data,
632 size_t size)
633{
634 const u8 *ptr = data;
635 unsigned long offset;
Thierry Redingac24c222012-11-23 15:14:00 +0100636 size_t i, j;
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100637 u32 value;
Thierry Redingedec4af2012-11-15 21:28:23 +0000638
Thierry Redingac24c222012-11-23 15:14:00 +0100639 switch (ptr[0]) {
640 case HDMI_INFOFRAME_TYPE_AVI:
641 offset = HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_HEADER;
642 break;
643
644 case HDMI_INFOFRAME_TYPE_AUDIO:
645 offset = HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_HEADER;
646 break;
647
648 case HDMI_INFOFRAME_TYPE_VENDOR:
649 offset = HDMI_NV_PDISP_HDMI_GENERIC_HEADER;
650 break;
651
652 default:
653 dev_err(hdmi->dev, "unsupported infoframe type: %02x\n",
654 ptr[0]);
655 return;
656 }
657
658 value = INFOFRAME_HEADER_TYPE(ptr[0]) |
659 INFOFRAME_HEADER_VERSION(ptr[1]) |
660 INFOFRAME_HEADER_LEN(ptr[2]);
Thierry Redingedec4af2012-11-15 21:28:23 +0000661 tegra_hdmi_writel(hdmi, value, offset);
Thierry Redingac24c222012-11-23 15:14:00 +0100662 offset++;
Thierry Redingedec4af2012-11-15 21:28:23 +0000663
Thierry Redingac24c222012-11-23 15:14:00 +0100664 /*
665 * Each subpack contains 7 bytes, divided into:
666 * - subpack_low: bytes 0 - 3
667 * - subpack_high: bytes 4 - 6 (with byte 7 padded to 0x00)
Thierry Redingedec4af2012-11-15 21:28:23 +0000668 */
Thierry Redingac24c222012-11-23 15:14:00 +0100669 for (i = 3, j = 0; i < size; i += 7, j += 8) {
670 size_t rem = size - i, num = min_t(size_t, rem, 4);
Thierry Redingedec4af2012-11-15 21:28:23 +0000671
Thierry Redingac24c222012-11-23 15:14:00 +0100672 value = tegra_hdmi_subpack(&ptr[i], num);
673 tegra_hdmi_writel(hdmi, value, offset++);
Thierry Redingedec4af2012-11-15 21:28:23 +0000674
Thierry Redingac24c222012-11-23 15:14:00 +0100675 num = min_t(size_t, rem - num, 3);
Thierry Redingedec4af2012-11-15 21:28:23 +0000676
Thierry Redingac24c222012-11-23 15:14:00 +0100677 value = tegra_hdmi_subpack(&ptr[i + 4], num);
678 tegra_hdmi_writel(hdmi, value, offset++);
Thierry Redingedec4af2012-11-15 21:28:23 +0000679 }
680}
681
682static void tegra_hdmi_setup_avi_infoframe(struct tegra_hdmi *hdmi,
683 struct drm_display_mode *mode)
684{
685 struct hdmi_avi_infoframe frame;
Thierry Redingac24c222012-11-23 15:14:00 +0100686 u8 buffer[17];
687 ssize_t err;
Thierry Redingedec4af2012-11-15 21:28:23 +0000688
Shashank Sharma0c1f5282017-07-13 21:03:07 +0530689 err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode, false);
Thierry Redingac24c222012-11-23 15:14:00 +0100690 if (err < 0) {
691 dev_err(hdmi->dev, "failed to setup AVI infoframe: %zd\n", err);
692 return;
Thierry Redingedec4af2012-11-15 21:28:23 +0000693 }
694
Thierry Redingac24c222012-11-23 15:14:00 +0100695 err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
696 if (err < 0) {
697 dev_err(hdmi->dev, "failed to pack AVI infoframe: %zd\n", err);
698 return;
699 }
700
701 tegra_hdmi_write_infopack(hdmi, buffer, err);
Thierry Reding2ccb3962015-01-15 13:43:18 +0100702}
Thierry Redingedec4af2012-11-15 21:28:23 +0000703
Thierry Reding2ccb3962015-01-15 13:43:18 +0100704static void tegra_hdmi_disable_avi_infoframe(struct tegra_hdmi *hdmi)
705{
706 u32 value;
707
708 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_CTRL);
709 value &= ~INFOFRAME_CTRL_ENABLE;
710 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_CTRL);
711}
712
713static void tegra_hdmi_enable_avi_infoframe(struct tegra_hdmi *hdmi)
714{
715 u32 value;
716
717 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_CTRL);
718 value |= INFOFRAME_CTRL_ENABLE;
719 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_CTRL);
Thierry Redingedec4af2012-11-15 21:28:23 +0000720}
721
722static void tegra_hdmi_setup_audio_infoframe(struct tegra_hdmi *hdmi)
723{
724 struct hdmi_audio_infoframe frame;
Thierry Redingac24c222012-11-23 15:14:00 +0100725 u8 buffer[14];
726 ssize_t err;
Thierry Redingedec4af2012-11-15 21:28:23 +0000727
Thierry Redingac24c222012-11-23 15:14:00 +0100728 err = hdmi_audio_infoframe_init(&frame);
729 if (err < 0) {
Thierry Redingef284c72013-10-16 19:51:22 +0200730 dev_err(hdmi->dev, "failed to setup audio infoframe: %zd\n",
Thierry Redingac24c222012-11-23 15:14:00 +0100731 err);
732 return;
733 }
Thierry Redingedec4af2012-11-15 21:28:23 +0000734
Thierry Redinge3c702d2019-01-03 15:23:17 +0100735 frame.channels = hdmi->format.channels;
Thierry Redingac24c222012-11-23 15:14:00 +0100736
737 err = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
738 if (err < 0) {
739 dev_err(hdmi->dev, "failed to pack audio infoframe: %zd\n",
740 err);
741 return;
742 }
743
744 /*
745 * The audio infoframe has only one set of subpack registers, so the
746 * infoframe needs to be truncated. One set of subpack registers can
747 * contain 7 bytes. Including the 3 byte header only the first 10
748 * bytes can be programmed.
749 */
Thierry Redingef284c72013-10-16 19:51:22 +0200750 tegra_hdmi_write_infopack(hdmi, buffer, min_t(size_t, 10, err));
Thierry Reding2ccb3962015-01-15 13:43:18 +0100751}
Thierry Redingedec4af2012-11-15 21:28:23 +0000752
Thierry Reding2ccb3962015-01-15 13:43:18 +0100753static void tegra_hdmi_disable_audio_infoframe(struct tegra_hdmi *hdmi)
754{
755 u32 value;
756
757 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_CTRL);
758 value &= ~INFOFRAME_CTRL_ENABLE;
759 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_CTRL);
760}
761
762static void tegra_hdmi_enable_audio_infoframe(struct tegra_hdmi *hdmi)
763{
764 u32 value;
765
766 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_CTRL);
767 value |= INFOFRAME_CTRL_ENABLE;
768 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_CTRL);
Thierry Redingedec4af2012-11-15 21:28:23 +0000769}
770
771static void tegra_hdmi_setup_stereo_infoframe(struct tegra_hdmi *hdmi)
772{
Lespiau, Damienae84b902013-08-19 16:59:02 +0100773 struct hdmi_vendor_infoframe frame;
Thierry Redingac24c222012-11-23 15:14:00 +0100774 u8 buffer[10];
775 ssize_t err;
Thierry Redingedec4af2012-11-15 21:28:23 +0000776
Lespiau, Damienae84b902013-08-19 16:59:02 +0100777 hdmi_vendor_infoframe_init(&frame);
Lespiau, Damiena26a58e82013-08-19 16:58:59 +0100778 frame.s3d_struct = HDMI_3D_STRUCTURE_FRAME_PACKING;
Thierry Redingac24c222012-11-23 15:14:00 +0100779
Lespiau, Damienae84b902013-08-19 16:59:02 +0100780 err = hdmi_vendor_infoframe_pack(&frame, buffer, sizeof(buffer));
Thierry Redingac24c222012-11-23 15:14:00 +0100781 if (err < 0) {
782 dev_err(hdmi->dev, "failed to pack vendor infoframe: %zd\n",
783 err);
784 return;
785 }
786
787 tegra_hdmi_write_infopack(hdmi, buffer, err);
Thierry Reding2ccb3962015-01-15 13:43:18 +0100788}
789
790static void tegra_hdmi_disable_stereo_infoframe(struct tegra_hdmi *hdmi)
791{
792 u32 value;
793
794 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
795 value &= ~GENERIC_CTRL_ENABLE;
796 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
797}
798
799static void tegra_hdmi_enable_stereo_infoframe(struct tegra_hdmi *hdmi)
800{
801 u32 value;
Thierry Redingedec4af2012-11-15 21:28:23 +0000802
803 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
804 value |= GENERIC_CTRL_ENABLE;
805 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
806}
807
808static void tegra_hdmi_setup_tmds(struct tegra_hdmi *hdmi,
809 const struct tmds_config *tmds)
810{
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100811 u32 value;
Thierry Redingedec4af2012-11-15 21:28:23 +0000812
813 tegra_hdmi_writel(hdmi, tmds->pll0, HDMI_NV_PDISP_SOR_PLL0);
814 tegra_hdmi_writel(hdmi, tmds->pll1, HDMI_NV_PDISP_SOR_PLL1);
815 tegra_hdmi_writel(hdmi, tmds->pe_current, HDMI_NV_PDISP_PE_CURRENT);
816
Thierry Reding59af0592013-10-14 09:43:05 +0200817 tegra_hdmi_writel(hdmi, tmds->drive_current,
818 HDMI_NV_PDISP_SOR_LANE_DRIVE_CURRENT);
819
820 value = tegra_hdmi_readl(hdmi, hdmi->config->fuse_override_offset);
821 value |= hdmi->config->fuse_override_value;
822 tegra_hdmi_writel(hdmi, value, hdmi->config->fuse_override_offset);
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +0200823
824 if (hdmi->config->has_sor_io_peak_current)
825 tegra_hdmi_writel(hdmi, tmds->peak_current,
826 HDMI_NV_PDISP_SOR_IO_PEAK_CURRENT);
Thierry Redingedec4af2012-11-15 21:28:23 +0000827}
828
Mikko Perttunen9f159122013-08-28 18:48:38 +0300829static bool tegra_output_is_hdmi(struct tegra_output *output)
830{
831 struct edid *edid;
832
833 if (!output->connector.edid_blob_ptr)
834 return false;
835
836 edid = (struct edid *)output->connector.edid_blob_ptr->data;
837
838 return drm_detect_hdmi_monitor(edid);
839}
840
Thierry Reding2ccb3962015-01-15 13:43:18 +0100841static enum drm_connector_status
842tegra_hdmi_connector_detect(struct drm_connector *connector, bool force)
843{
844 struct tegra_output *output = connector_to_output(connector);
845 struct tegra_hdmi *hdmi = to_hdmi(output);
846 enum drm_connector_status status;
847
848 status = tegra_output_connector_detect(connector, force);
849 if (status == connector_status_connected)
850 return status;
851
852 tegra_hdmi_writel(hdmi, 0, HDMI_NV_PDISP_SOR_AUDIO_HDA_PRESENSE);
853 return status;
854}
855
Thierry Reding1d600472017-11-08 13:18:31 +0100856#define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
857
858static const struct debugfs_reg32 tegra_hdmi_regs[] = {
859 DEBUGFS_REG32(HDMI_CTXSW),
860 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_STATE0),
861 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_STATE1),
862 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_STATE2),
863 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_AN_MSB),
864 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_AN_LSB),
865 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_CN_MSB),
866 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_CN_LSB),
867 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_AKSV_MSB),
868 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_AKSV_LSB),
869 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_BKSV_MSB),
870 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_BKSV_LSB),
871 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_CKSV_MSB),
872 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_CKSV_LSB),
873 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_DKSV_MSB),
874 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_DKSV_LSB),
875 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_CTRL),
876 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_CMODE),
877 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_MPRIME_MSB),
878 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_MPRIME_LSB),
879 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_SPRIME_MSB),
880 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_SPRIME_LSB2),
881 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_SPRIME_LSB1),
882 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_RI),
883 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_CS_MSB),
884 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_CS_LSB),
885 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AUDIO_EMU0),
886 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AUDIO_EMU_RDATA0),
887 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AUDIO_EMU1),
888 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AUDIO_EMU2),
889 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_CTRL),
890 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_STATUS),
891 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_HEADER),
892 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_SUBPACK0_LOW),
893 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_SUBPACK0_HIGH),
894 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_CTRL),
895 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_STATUS),
896 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_HEADER),
897 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_SUBPACK0_LOW),
898 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_SUBPACK0_HIGH),
899 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_SUBPACK1_LOW),
900 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_SUBPACK1_HIGH),
901 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_GENERIC_CTRL),
902 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_GENERIC_STATUS),
903 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_GENERIC_HEADER),
904 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK0_LOW),
905 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK0_HIGH),
906 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK1_LOW),
907 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK1_HIGH),
908 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK2_LOW),
909 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK2_HIGH),
910 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK3_LOW),
911 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK3_HIGH),
912 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_ACR_CTRL),
913 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_ACR_0320_SUBPACK_LOW),
914 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_ACR_0320_SUBPACK_HIGH),
915 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_ACR_0441_SUBPACK_LOW),
916 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_ACR_0441_SUBPACK_HIGH),
917 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_ACR_0882_SUBPACK_LOW),
918 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_ACR_0882_SUBPACK_HIGH),
919 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_ACR_1764_SUBPACK_LOW),
920 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_ACR_1764_SUBPACK_HIGH),
921 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_ACR_0480_SUBPACK_LOW),
922 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_ACR_0480_SUBPACK_HIGH),
923 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_ACR_0960_SUBPACK_LOW),
924 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_ACR_0960_SUBPACK_HIGH),
925 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_ACR_1920_SUBPACK_LOW),
926 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_ACR_1920_SUBPACK_HIGH),
927 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_CTRL),
928 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_VSYNC_KEEPOUT),
929 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_VSYNC_WINDOW),
930 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_GCP_CTRL),
931 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_GCP_STATUS),
932 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_GCP_SUBPACK),
933 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_CHANNEL_STATUS1),
934 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_CHANNEL_STATUS2),
935 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_EMU0),
936 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_EMU1),
937 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_EMU1_RDATA),
938 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_SPARE),
939 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_SPDIF_CHN_STATUS1),
940 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_SPDIF_CHN_STATUS2),
941 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_HDCPRIF_ROM_CTRL),
942 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_CAP),
943 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_PWR),
944 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_TEST),
945 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_PLL0),
946 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_PLL1),
947 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_PLL2),
948 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_CSTM),
949 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_LVDS),
950 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_CRCA),
951 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_CRCB),
952 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_BLANK),
953 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_CTL),
954 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(0)),
955 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(1)),
956 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(2)),
957 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(3)),
958 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(4)),
959 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(5)),
960 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(6)),
961 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(7)),
962 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(8)),
963 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(9)),
964 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(10)),
965 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(11)),
966 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(12)),
967 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(13)),
968 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(14)),
969 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(15)),
970 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_VCRCA0),
971 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_VCRCA1),
972 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_CCRCA0),
973 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_CCRCA1),
974 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_EDATAA0),
975 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_EDATAA1),
976 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_COUNTA0),
977 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_COUNTA1),
978 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_DEBUGA0),
979 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_DEBUGA1),
980 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_TRIG),
981 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_MSCHECK),
982 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_LANE_DRIVE_CURRENT),
983 DEBUGFS_REG32(HDMI_NV_PDISP_AUDIO_DEBUG0),
984 DEBUGFS_REG32(HDMI_NV_PDISP_AUDIO_DEBUG1),
985 DEBUGFS_REG32(HDMI_NV_PDISP_AUDIO_DEBUG2),
986 DEBUGFS_REG32(HDMI_NV_PDISP_AUDIO_FS(0)),
987 DEBUGFS_REG32(HDMI_NV_PDISP_AUDIO_FS(1)),
988 DEBUGFS_REG32(HDMI_NV_PDISP_AUDIO_FS(2)),
989 DEBUGFS_REG32(HDMI_NV_PDISP_AUDIO_FS(3)),
990 DEBUGFS_REG32(HDMI_NV_PDISP_AUDIO_FS(4)),
991 DEBUGFS_REG32(HDMI_NV_PDISP_AUDIO_FS(5)),
992 DEBUGFS_REG32(HDMI_NV_PDISP_AUDIO_FS(6)),
993 DEBUGFS_REG32(HDMI_NV_PDISP_AUDIO_PULSE_WIDTH),
994 DEBUGFS_REG32(HDMI_NV_PDISP_AUDIO_THRESHOLD),
995 DEBUGFS_REG32(HDMI_NV_PDISP_AUDIO_CNTRL0),
996 DEBUGFS_REG32(HDMI_NV_PDISP_AUDIO_N),
997 DEBUGFS_REG32(HDMI_NV_PDISP_HDCPRIF_ROM_TIMING),
998 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_REFCLK),
999 DEBUGFS_REG32(HDMI_NV_PDISP_CRC_CONTROL),
1000 DEBUGFS_REG32(HDMI_NV_PDISP_INPUT_CONTROL),
1001 DEBUGFS_REG32(HDMI_NV_PDISP_SCRATCH),
1002 DEBUGFS_REG32(HDMI_NV_PDISP_PE_CURRENT),
1003 DEBUGFS_REG32(HDMI_NV_PDISP_KEY_CTRL),
1004 DEBUGFS_REG32(HDMI_NV_PDISP_KEY_DEBUG0),
1005 DEBUGFS_REG32(HDMI_NV_PDISP_KEY_DEBUG1),
1006 DEBUGFS_REG32(HDMI_NV_PDISP_KEY_DEBUG2),
1007 DEBUGFS_REG32(HDMI_NV_PDISP_KEY_HDCP_KEY_0),
1008 DEBUGFS_REG32(HDMI_NV_PDISP_KEY_HDCP_KEY_1),
1009 DEBUGFS_REG32(HDMI_NV_PDISP_KEY_HDCP_KEY_2),
1010 DEBUGFS_REG32(HDMI_NV_PDISP_KEY_HDCP_KEY_3),
1011 DEBUGFS_REG32(HDMI_NV_PDISP_KEY_HDCP_KEY_TRIG),
1012 DEBUGFS_REG32(HDMI_NV_PDISP_KEY_SKEY_INDEX),
1013 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_AUDIO_CNTRL0),
1014 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_AUDIO_SPARE0),
1015 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_AUDIO_HDA_CODEC_SCRATCH0),
1016 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_AUDIO_HDA_CODEC_SCRATCH1),
1017 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_AUDIO_HDA_ELD_BUFWR),
1018 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_AUDIO_HDA_PRESENSE),
1019 DEBUGFS_REG32(HDMI_NV_PDISP_INT_STATUS),
1020 DEBUGFS_REG32(HDMI_NV_PDISP_INT_MASK),
1021 DEBUGFS_REG32(HDMI_NV_PDISP_INT_ENABLE),
1022 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_IO_PEAK_CURRENT),
1023};
1024
1025static int tegra_hdmi_show_regs(struct seq_file *s, void *data)
1026{
1027 struct drm_info_node *node = s->private;
1028 struct tegra_hdmi *hdmi = node->info_ent->data;
1029 struct drm_crtc *crtc = hdmi->output.encoder.crtc;
1030 struct drm_device *drm = node->minor->dev;
1031 unsigned int i;
1032 int err = 0;
1033
1034 drm_modeset_lock_all(drm);
1035
1036 if (!crtc || !crtc->state->active) {
1037 err = -EBUSY;
1038 goto unlock;
1039 }
1040
1041 for (i = 0; i < ARRAY_SIZE(tegra_hdmi_regs); i++) {
1042 unsigned int offset = tegra_hdmi_regs[i].offset;
1043
1044 seq_printf(s, "%-56s %#05x %08x\n", tegra_hdmi_regs[i].name,
1045 offset, tegra_hdmi_readl(hdmi, offset));
1046 }
1047
1048unlock:
1049 drm_modeset_unlock_all(drm);
1050 return err;
1051}
1052
1053static struct drm_info_list debugfs_files[] = {
1054 { "regs", tegra_hdmi_show_regs, 0, NULL },
1055};
1056
1057static int tegra_hdmi_late_register(struct drm_connector *connector)
1058{
1059 struct tegra_output *output = connector_to_output(connector);
1060 unsigned int i, count = ARRAY_SIZE(debugfs_files);
1061 struct drm_minor *minor = connector->dev->primary;
1062 struct dentry *root = connector->debugfs_entry;
1063 struct tegra_hdmi *hdmi = to_hdmi(output);
1064 int err;
1065
1066 hdmi->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1067 GFP_KERNEL);
1068 if (!hdmi->debugfs_files)
1069 return -ENOMEM;
1070
1071 for (i = 0; i < count; i++)
1072 hdmi->debugfs_files[i].data = hdmi;
1073
1074 err = drm_debugfs_create_files(hdmi->debugfs_files, count, root, minor);
1075 if (err < 0)
1076 goto free;
1077
1078 return 0;
1079
1080free:
1081 kfree(hdmi->debugfs_files);
1082 hdmi->debugfs_files = NULL;
1083
1084 return err;
1085}
1086
1087static void tegra_hdmi_early_unregister(struct drm_connector *connector)
1088{
1089 struct tegra_output *output = connector_to_output(connector);
1090 struct drm_minor *minor = connector->dev->primary;
1091 unsigned int count = ARRAY_SIZE(debugfs_files);
1092 struct tegra_hdmi *hdmi = to_hdmi(output);
1093
1094 drm_debugfs_remove_files(hdmi->debugfs_files, count, minor);
1095 kfree(hdmi->debugfs_files);
1096 hdmi->debugfs_files = NULL;
1097}
1098
Thierry Reding59682712014-11-28 16:50:59 +01001099static const struct drm_connector_funcs tegra_hdmi_connector_funcs = {
Thierry Reding9d441892014-11-24 17:02:53 +01001100 .reset = drm_atomic_helper_connector_reset,
Thierry Reding2ccb3962015-01-15 13:43:18 +01001101 .detect = tegra_hdmi_connector_detect,
Thierry Reding59682712014-11-28 16:50:59 +01001102 .fill_modes = drm_helper_probe_single_connector_modes,
1103 .destroy = tegra_output_connector_destroy,
Thierry Reding9d441892014-11-24 17:02:53 +01001104 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
Thierry Reding4aa3df72014-11-24 16:27:13 +01001105 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Thierry Reding1d600472017-11-08 13:18:31 +01001106 .late_register = tegra_hdmi_late_register,
1107 .early_unregister = tegra_hdmi_early_unregister,
Thierry Reding59682712014-11-28 16:50:59 +01001108};
1109
1110static enum drm_mode_status
1111tegra_hdmi_connector_mode_valid(struct drm_connector *connector,
1112 struct drm_display_mode *mode)
1113{
1114 struct tegra_output *output = connector_to_output(connector);
1115 struct tegra_hdmi *hdmi = to_hdmi(output);
1116 unsigned long pclk = mode->clock * 1000;
1117 enum drm_mode_status status = MODE_OK;
1118 struct clk *parent;
1119 long err;
1120
1121 parent = clk_get_parent(hdmi->clk_parent);
1122
1123 err = clk_round_rate(parent, pclk * 4);
1124 if (err <= 0)
1125 status = MODE_NOCLOCK;
1126
1127 return status;
1128}
1129
1130static const struct drm_connector_helper_funcs
1131tegra_hdmi_connector_helper_funcs = {
1132 .get_modes = tegra_output_connector_get_modes,
1133 .mode_valid = tegra_hdmi_connector_mode_valid,
Thierry Reding59682712014-11-28 16:50:59 +01001134};
1135
1136static const struct drm_encoder_funcs tegra_hdmi_encoder_funcs = {
1137 .destroy = tegra_output_encoder_destroy,
1138};
1139
Thierry Reding29871b22015-07-29 09:46:40 +02001140static void tegra_hdmi_encoder_disable(struct drm_encoder *encoder)
Thierry Reding59682712014-11-28 16:50:59 +01001141{
Thierry Reding2ccb3962015-01-15 13:43:18 +01001142 struct tegra_output *output = encoder_to_output(encoder);
Thierry Reding29871b22015-07-29 09:46:40 +02001143 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
Thierry Reding2ccb3962015-01-15 13:43:18 +01001144 struct tegra_hdmi *hdmi = to_hdmi(output);
Thierry Reding29871b22015-07-29 09:46:40 +02001145 u32 value;
1146
1147 /*
1148 * The following accesses registers of the display controller, so make
1149 * sure it's only executed when the output is attached to one.
1150 */
1151 if (dc) {
1152 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1153 value &= ~HDMI_ENABLE;
1154 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1155
1156 tegra_dc_commit(dc);
1157 }
Thierry Reding2ccb3962015-01-15 13:43:18 +01001158
1159 if (!hdmi->dvi) {
1160 if (hdmi->stereo)
1161 tegra_hdmi_disable_stereo_infoframe(hdmi);
1162
1163 tegra_hdmi_disable_audio_infoframe(hdmi);
1164 tegra_hdmi_disable_avi_infoframe(hdmi);
1165 tegra_hdmi_disable_audio(hdmi);
1166 }
Thierry Reding2ccb3962015-01-15 13:43:18 +01001167
Thierry Reding52345492015-08-07 16:00:43 +02001168 tegra_hdmi_writel(hdmi, 0, HDMI_NV_PDISP_INT_ENABLE);
1169 tegra_hdmi_writel(hdmi, 0, HDMI_NV_PDISP_INT_MASK);
Thierry Reding2ccb3962015-01-15 13:43:18 +01001170
Thierry Reding52345492015-08-07 16:00:43 +02001171 pm_runtime_put(hdmi->dev);
Thierry Reding59682712014-11-28 16:50:59 +01001172}
1173
Thierry Reding29871b22015-07-29 09:46:40 +02001174static void tegra_hdmi_encoder_enable(struct drm_encoder *encoder)
Thierry Reding59682712014-11-28 16:50:59 +01001175{
Thierry Reding29871b22015-07-29 09:46:40 +02001176 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
Thierry Redingedec4af2012-11-15 21:28:23 +00001177 unsigned int h_sync_width, h_front_porch, h_back_porch, i, rekey;
Thierry Reding59682712014-11-28 16:50:59 +01001178 struct tegra_output *output = encoder_to_output(encoder);
1179 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
Thierry Redingedec4af2012-11-15 21:28:23 +00001180 struct tegra_hdmi *hdmi = to_hdmi(output);
Thierry Reding2ccb3962015-01-15 13:43:18 +01001181 unsigned int pulse_start, div82;
Thierry Redingedec4af2012-11-15 21:28:23 +00001182 int retries = 1000;
Thierry Reding4ee8cee2014-12-08 16:25:14 +01001183 u32 value;
Thierry Redingedec4af2012-11-15 21:28:23 +00001184 int err;
1185
Thierry Reding52345492015-08-07 16:00:43 +02001186 pm_runtime_get_sync(hdmi->dev);
Mikko Perttunen9f159122013-08-28 18:48:38 +03001187
Thierry Reding52345492015-08-07 16:00:43 +02001188 /*
1189 * Enable and unmask the HDA codec SCRATCH0 register interrupt. This
1190 * is used for interoperability between the HDA codec driver and the
1191 * HDMI driver.
1192 */
1193 tegra_hdmi_writel(hdmi, INT_CODEC_SCRATCH0, HDMI_NV_PDISP_INT_ENABLE);
1194 tegra_hdmi_writel(hdmi, INT_CODEC_SCRATCH0, HDMI_NV_PDISP_INT_MASK);
1195
Thierry Reding2ccb3962015-01-15 13:43:18 +01001196 hdmi->pixel_clock = mode->clock * 1000;
Thierry Redingedec4af2012-11-15 21:28:23 +00001197 h_sync_width = mode->hsync_end - mode->hsync_start;
Lucas Stach40495082012-12-19 21:38:52 +00001198 h_back_porch = mode->htotal - mode->hsync_end;
1199 h_front_porch = mode->hsync_start - mode->hdisplay;
Thierry Redingedec4af2012-11-15 21:28:23 +00001200
Thierry Reding2ccb3962015-01-15 13:43:18 +01001201 err = clk_set_rate(hdmi->clk, hdmi->pixel_clock);
Thierry Redingc03bf1bf2015-02-18 10:34:08 +01001202 if (err < 0) {
1203 dev_err(hdmi->dev, "failed to set HDMI clock frequency: %d\n",
1204 err);
1205 }
1206
1207 DRM_DEBUG_KMS("HDMI clock rate: %lu Hz\n", clk_get_rate(hdmi->clk));
1208
Thierry Reding8c8282c2014-04-16 10:46:24 +02001209 /* power up sequence */
1210 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_SOR_PLL0);
1211 value &= ~SOR_PLL_PDBG;
1212 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_PLL0);
1213
1214 usleep_range(10, 20);
1215
1216 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_SOR_PLL0);
1217 value &= ~SOR_PLL_PWR;
1218 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_PLL0);
1219
Thierry Redingedec4af2012-11-15 21:28:23 +00001220 tegra_dc_writel(dc, VSYNC_H_POSITION(1),
1221 DC_DISP_DISP_TIMING_OPTIONS);
Thierry Reding472a6d12015-08-05 16:39:55 +02001222 tegra_dc_writel(dc, DITHER_CONTROL_DISABLE | BASE_COLOR_SIZE_888,
Thierry Redingedec4af2012-11-15 21:28:23 +00001223 DC_DISP_DISP_COLOR_CONTROL);
1224
1225 /* video_preamble uses h_pulse2 */
1226 pulse_start = 1 + h_sync_width + h_back_porch - 10;
1227
Thierry Reding8fd3ffa2015-04-27 14:48:35 +02001228 tegra_dc_writel(dc, H_PULSE2_ENABLE, DC_DISP_DISP_SIGNAL_OPTIONS0);
Thierry Redingedec4af2012-11-15 21:28:23 +00001229
1230 value = PULSE_MODE_NORMAL | PULSE_POLARITY_HIGH | PULSE_QUAL_VACTIVE |
1231 PULSE_LAST_END_A;
1232 tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_CONTROL);
1233
1234 value = PULSE_START(pulse_start) | PULSE_END(pulse_start + 8);
1235 tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_POSITION_A);
1236
1237 value = VSYNC_WINDOW_END(0x210) | VSYNC_WINDOW_START(0x200) |
1238 VSYNC_WINDOW_ENABLE;
1239 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_VSYNC_WINDOW);
1240
1241 if (dc->pipe)
1242 value = HDMI_SRC_DISPLAYB;
1243 else
1244 value = HDMI_SRC_DISPLAYA;
1245
1246 if ((mode->hdisplay == 720) && ((mode->vdisplay == 480) ||
1247 (mode->vdisplay == 576)))
1248 tegra_hdmi_writel(hdmi,
1249 value | ARM_VIDEO_RANGE_FULL,
1250 HDMI_NV_PDISP_INPUT_CONTROL);
1251 else
1252 tegra_hdmi_writel(hdmi,
1253 value | ARM_VIDEO_RANGE_LIMITED,
1254 HDMI_NV_PDISP_INPUT_CONTROL);
1255
1256 div82 = clk_get_rate(hdmi->clk) / 1000000 * 4;
1257 value = SOR_REFCLK_DIV_INT(div82 >> 2) | SOR_REFCLK_DIV_FRAC(div82);
1258 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_REFCLK);
1259
Thierry Reding2ccb3962015-01-15 13:43:18 +01001260 hdmi->dvi = !tegra_output_is_hdmi(output);
Thierry Redingedec4af2012-11-15 21:28:23 +00001261 if (!hdmi->dvi) {
Thierry Reding2ccb3962015-01-15 13:43:18 +01001262 err = tegra_hdmi_setup_audio(hdmi);
Thierry Redingedec4af2012-11-15 21:28:23 +00001263 if (err < 0)
1264 hdmi->dvi = true;
1265 }
1266
Thierry Reding2ccb3962015-01-15 13:43:18 +01001267 if (hdmi->config->has_hda)
1268 tegra_hdmi_write_eld(hdmi);
Thierry Redingedec4af2012-11-15 21:28:23 +00001269
1270 rekey = HDMI_REKEY_DEFAULT;
1271 value = HDMI_CTRL_REKEY(rekey);
1272 value |= HDMI_CTRL_MAX_AC_PACKET((h_sync_width + h_back_porch +
1273 h_front_porch - rekey - 18) / 32);
1274
1275 if (!hdmi->dvi)
1276 value |= HDMI_CTRL_ENABLE;
1277
1278 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_CTRL);
1279
Thierry Reding2ccb3962015-01-15 13:43:18 +01001280 if (!hdmi->dvi) {
1281 tegra_hdmi_setup_avi_infoframe(hdmi, mode);
1282 tegra_hdmi_setup_audio_infoframe(hdmi);
Thierry Redingedec4af2012-11-15 21:28:23 +00001283
Thierry Reding2ccb3962015-01-15 13:43:18 +01001284 if (hdmi->stereo)
1285 tegra_hdmi_setup_stereo_infoframe(hdmi);
1286 }
Thierry Redingedec4af2012-11-15 21:28:23 +00001287
1288 /* TMDS CONFIG */
Thierry Reding59af0592013-10-14 09:43:05 +02001289 for (i = 0; i < hdmi->config->num_tmds; i++) {
Thierry Reding2ccb3962015-01-15 13:43:18 +01001290 if (hdmi->pixel_clock <= hdmi->config->tmds[i].pclk) {
Thierry Reding59af0592013-10-14 09:43:05 +02001291 tegra_hdmi_setup_tmds(hdmi, &hdmi->config->tmds[i]);
Thierry Redingedec4af2012-11-15 21:28:23 +00001292 break;
1293 }
1294 }
1295
1296 tegra_hdmi_writel(hdmi,
Thierry Reding5c1c0712015-01-28 16:32:52 +01001297 SOR_SEQ_PU_PC(0) |
Thierry Redingedec4af2012-11-15 21:28:23 +00001298 SOR_SEQ_PU_PC_ALT(0) |
1299 SOR_SEQ_PD_PC(8) |
1300 SOR_SEQ_PD_PC_ALT(8),
1301 HDMI_NV_PDISP_SOR_SEQ_CTL);
1302
1303 value = SOR_SEQ_INST_WAIT_TIME(1) |
1304 SOR_SEQ_INST_WAIT_UNITS_VSYNC |
1305 SOR_SEQ_INST_HALT |
1306 SOR_SEQ_INST_PIN_A_LOW |
1307 SOR_SEQ_INST_PIN_B_LOW |
1308 SOR_SEQ_INST_DRIVE_PWM_OUT_LO;
1309
1310 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_SEQ_INST(0));
1311 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_SEQ_INST(8));
1312
Thierry Reding9cbfc732014-04-16 10:47:36 +02001313 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_SOR_CSTM);
Thierry Redingedec4af2012-11-15 21:28:23 +00001314 value &= ~SOR_CSTM_ROTCLK(~0);
1315 value |= SOR_CSTM_ROTCLK(2);
Thierry Reding9cbfc732014-04-16 10:47:36 +02001316 value |= SOR_CSTM_PLLDIV;
1317 value &= ~SOR_CSTM_LVDS_ENABLE;
1318 value &= ~SOR_CSTM_MODE_MASK;
1319 value |= SOR_CSTM_MODE_TMDS;
Thierry Redingedec4af2012-11-15 21:28:23 +00001320 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_CSTM);
1321
Thierry Redingedec4af2012-11-15 21:28:23 +00001322 /* start SOR */
1323 tegra_hdmi_writel(hdmi,
1324 SOR_PWR_NORMAL_STATE_PU |
1325 SOR_PWR_NORMAL_START_NORMAL |
1326 SOR_PWR_SAFE_STATE_PD |
1327 SOR_PWR_SETTING_NEW_TRIGGER,
1328 HDMI_NV_PDISP_SOR_PWR);
1329 tegra_hdmi_writel(hdmi,
1330 SOR_PWR_NORMAL_STATE_PU |
1331 SOR_PWR_NORMAL_START_NORMAL |
1332 SOR_PWR_SAFE_STATE_PD |
1333 SOR_PWR_SETTING_NEW_DONE,
1334 HDMI_NV_PDISP_SOR_PWR);
1335
1336 do {
1337 BUG_ON(--retries < 0);
1338 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_SOR_PWR);
1339 } while (value & SOR_PWR_SETTING_NEW_PENDING);
1340
1341 value = SOR_STATE_ASY_CRCMODE_COMPLETE |
1342 SOR_STATE_ASY_OWNER_HEAD0 |
1343 SOR_STATE_ASY_SUBOWNER_BOTH |
1344 SOR_STATE_ASY_PROTOCOL_SINGLE_TMDS_A |
1345 SOR_STATE_ASY_DEPOL_POS;
1346
1347 /* setup sync polarities */
1348 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
1349 value |= SOR_STATE_ASY_HSYNCPOL_POS;
1350
1351 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1352 value |= SOR_STATE_ASY_HSYNCPOL_NEG;
1353
1354 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
1355 value |= SOR_STATE_ASY_VSYNCPOL_POS;
1356
1357 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1358 value |= SOR_STATE_ASY_VSYNCPOL_NEG;
1359
1360 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_STATE2);
1361
1362 value = SOR_STATE_ASY_HEAD_OPMODE_AWAKE | SOR_STATE_ASY_ORMODE_NORMAL;
1363 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_STATE1);
1364
1365 tegra_hdmi_writel(hdmi, 0, HDMI_NV_PDISP_SOR_STATE0);
1366 tegra_hdmi_writel(hdmi, SOR_STATE_UPDATE, HDMI_NV_PDISP_SOR_STATE0);
1367 tegra_hdmi_writel(hdmi, value | SOR_STATE_ATTACHED,
1368 HDMI_NV_PDISP_SOR_STATE1);
1369 tegra_hdmi_writel(hdmi, 0, HDMI_NV_PDISP_SOR_STATE0);
1370
Thierry Reding72d30282013-12-12 11:06:55 +01001371 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1372 value |= HDMI_ENABLE;
1373 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
Thierry Redingedec4af2012-11-15 21:28:23 +00001374
Thierry Reding62b9e062014-11-21 17:33:33 +01001375 tegra_dc_commit(dc);
Thierry Redingedec4af2012-11-15 21:28:23 +00001376
Thierry Reding2ccb3962015-01-15 13:43:18 +01001377 if (!hdmi->dvi) {
1378 tegra_hdmi_enable_avi_infoframe(hdmi);
1379 tegra_hdmi_enable_audio_infoframe(hdmi);
1380 tegra_hdmi_enable_audio(hdmi);
1381
1382 if (hdmi->stereo)
1383 tegra_hdmi_enable_stereo_infoframe(hdmi);
1384 }
1385
Thierry Redingedec4af2012-11-15 21:28:23 +00001386 /* TODO: add HDCP support */
Thierry Redingedec4af2012-11-15 21:28:23 +00001387}
1388
Thierry Redinga9825a62014-12-08 16:33:03 +01001389static int
1390tegra_hdmi_encoder_atomic_check(struct drm_encoder *encoder,
1391 struct drm_crtc_state *crtc_state,
1392 struct drm_connector_state *conn_state)
1393{
1394 struct tegra_output *output = encoder_to_output(encoder);
1395 struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
1396 unsigned long pclk = crtc_state->mode.clock * 1000;
1397 struct tegra_hdmi *hdmi = to_hdmi(output);
1398 int err;
1399
1400 err = tegra_dc_state_setup_clock(dc, crtc_state, hdmi->clk_parent,
1401 pclk, 0);
1402 if (err < 0) {
1403 dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
1404 return err;
1405 }
1406
1407 return err;
1408}
1409
Thierry Reding59682712014-11-28 16:50:59 +01001410static const struct drm_encoder_helper_funcs tegra_hdmi_encoder_helper_funcs = {
Thierry Reding59682712014-11-28 16:50:59 +01001411 .disable = tegra_hdmi_encoder_disable,
Thierry Reding29871b22015-07-29 09:46:40 +02001412 .enable = tegra_hdmi_encoder_enable,
Thierry Redinga9825a62014-12-08 16:33:03 +01001413 .atomic_check = tegra_hdmi_encoder_atomic_check,
Thierry Redingedec4af2012-11-15 21:28:23 +00001414};
1415
Thierry Reding53fa7f72013-09-24 15:35:40 +02001416static int tegra_hdmi_init(struct host1x_client *client)
Thierry Redingedec4af2012-11-15 21:28:23 +00001417{
Thierry Reding9910f5c2014-05-22 09:57:15 +02001418 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Reding776dc382013-10-14 14:43:22 +02001419 struct tegra_hdmi *hdmi = host1x_client_to_hdmi(client);
Thierry Redingedec4af2012-11-15 21:28:23 +00001420 int err;
1421
Thierry Redingedec4af2012-11-15 21:28:23 +00001422 hdmi->output.dev = client->dev;
Thierry Redingedec4af2012-11-15 21:28:23 +00001423
Thierry Reding59682712014-11-28 16:50:59 +01001424 drm_connector_init(drm, &hdmi->output.connector,
1425 &tegra_hdmi_connector_funcs,
1426 DRM_MODE_CONNECTOR_HDMIA);
1427 drm_connector_helper_add(&hdmi->output.connector,
1428 &tegra_hdmi_connector_helper_funcs);
1429 hdmi->output.connector.dpms = DRM_MODE_DPMS_OFF;
1430
1431 drm_encoder_init(drm, &hdmi->output.encoder, &tegra_hdmi_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001432 DRM_MODE_ENCODER_TMDS, NULL);
Thierry Reding59682712014-11-28 16:50:59 +01001433 drm_encoder_helper_add(&hdmi->output.encoder,
1434 &tegra_hdmi_encoder_helper_funcs);
1435
Daniel Vettercde4c442018-07-09 10:40:07 +02001436 drm_connector_attach_encoder(&hdmi->output.connector,
Thierry Reding59682712014-11-28 16:50:59 +01001437 &hdmi->output.encoder);
1438 drm_connector_register(&hdmi->output.connector);
1439
Thierry Redingea130b22014-12-19 15:51:35 +01001440 err = tegra_output_init(drm, &hdmi->output);
1441 if (err < 0) {
1442 dev_err(client->dev, "failed to initialize output: %d\n", err);
1443 return err;
1444 }
Thierry Reding59682712014-11-28 16:50:59 +01001445
Thierry Redingea130b22014-12-19 15:51:35 +01001446 hdmi->output.encoder.possible_crtcs = 0x3;
Thierry Redingedec4af2012-11-15 21:28:23 +00001447
Thierry Redingfb50a112014-02-28 16:57:34 +01001448 err = regulator_enable(hdmi->hdmi);
1449 if (err < 0) {
1450 dev_err(client->dev, "failed to enable HDMI regulator: %d\n",
1451 err);
1452 return err;
1453 }
1454
Thierry Reding59682712014-11-28 16:50:59 +01001455 err = regulator_enable(hdmi->pll);
1456 if (err < 0) {
1457 dev_err(hdmi->dev, "failed to enable PLL regulator: %d\n", err);
1458 return err;
1459 }
1460
1461 err = regulator_enable(hdmi->vdd);
1462 if (err < 0) {
1463 dev_err(hdmi->dev, "failed to enable VDD regulator: %d\n", err);
1464 return err;
1465 }
1466
Thierry Redingedec4af2012-11-15 21:28:23 +00001467 return 0;
1468}
1469
Thierry Reding53fa7f72013-09-24 15:35:40 +02001470static int tegra_hdmi_exit(struct host1x_client *client)
Thierry Redingedec4af2012-11-15 21:28:23 +00001471{
Thierry Reding776dc382013-10-14 14:43:22 +02001472 struct tegra_hdmi *hdmi = host1x_client_to_hdmi(client);
Thierry Redingedec4af2012-11-15 21:28:23 +00001473
Thierry Reding59682712014-11-28 16:50:59 +01001474 tegra_output_exit(&hdmi->output);
1475
Thierry Reding59682712014-11-28 16:50:59 +01001476 regulator_disable(hdmi->vdd);
1477 regulator_disable(hdmi->pll);
Thierry Redingfb50a112014-02-28 16:57:34 +01001478 regulator_disable(hdmi->hdmi);
1479
Thierry Redingedec4af2012-11-15 21:28:23 +00001480 return 0;
1481}
1482
1483static const struct host1x_client_ops hdmi_client_ops = {
Thierry Reding53fa7f72013-09-24 15:35:40 +02001484 .init = tegra_hdmi_init,
1485 .exit = tegra_hdmi_exit,
Thierry Redingedec4af2012-11-15 21:28:23 +00001486};
1487
Thierry Reding59af0592013-10-14 09:43:05 +02001488static const struct tegra_hdmi_config tegra20_hdmi_config = {
1489 .tmds = tegra20_tmds_config,
1490 .num_tmds = ARRAY_SIZE(tegra20_tmds_config),
1491 .fuse_override_offset = HDMI_NV_PDISP_SOR_LANE_DRIVE_CURRENT,
1492 .fuse_override_value = 1 << 31,
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +02001493 .has_sor_io_peak_current = false,
Thierry Reding2ccb3962015-01-15 13:43:18 +01001494 .has_hda = false,
1495 .has_hbr = false,
Thierry Reding59af0592013-10-14 09:43:05 +02001496};
1497
1498static const struct tegra_hdmi_config tegra30_hdmi_config = {
1499 .tmds = tegra30_tmds_config,
1500 .num_tmds = ARRAY_SIZE(tegra30_tmds_config),
1501 .fuse_override_offset = HDMI_NV_PDISP_SOR_LANE_DRIVE_CURRENT,
1502 .fuse_override_value = 1 << 31,
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +02001503 .has_sor_io_peak_current = false,
Thierry Reding2ccb3962015-01-15 13:43:18 +01001504 .has_hda = true,
1505 .has_hbr = false,
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +02001506};
1507
1508static const struct tegra_hdmi_config tegra114_hdmi_config = {
1509 .tmds = tegra114_tmds_config,
1510 .num_tmds = ARRAY_SIZE(tegra114_tmds_config),
1511 .fuse_override_offset = HDMI_NV_PDISP_SOR_PAD_CTLS0,
1512 .fuse_override_value = 1 << 31,
1513 .has_sor_io_peak_current = true,
Thierry Reding2ccb3962015-01-15 13:43:18 +01001514 .has_hda = true,
1515 .has_hbr = true,
Thierry Reding59af0592013-10-14 09:43:05 +02001516};
1517
Thierry Redingfb7be702013-11-15 16:07:32 +01001518static const struct tegra_hdmi_config tegra124_hdmi_config = {
1519 .tmds = tegra124_tmds_config,
1520 .num_tmds = ARRAY_SIZE(tegra124_tmds_config),
1521 .fuse_override_offset = HDMI_NV_PDISP_SOR_PAD_CTLS0,
1522 .fuse_override_value = 1 << 31,
1523 .has_sor_io_peak_current = true,
Thierry Reding2ccb3962015-01-15 13:43:18 +01001524 .has_hda = true,
1525 .has_hbr = true,
Thierry Redingfb7be702013-11-15 16:07:32 +01001526};
1527
Thierry Reding59af0592013-10-14 09:43:05 +02001528static const struct of_device_id tegra_hdmi_of_match[] = {
Thierry Redingfb7be702013-11-15 16:07:32 +01001529 { .compatible = "nvidia,tegra124-hdmi", .data = &tegra124_hdmi_config },
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +02001530 { .compatible = "nvidia,tegra114-hdmi", .data = &tegra114_hdmi_config },
Thierry Reding59af0592013-10-14 09:43:05 +02001531 { .compatible = "nvidia,tegra30-hdmi", .data = &tegra30_hdmi_config },
1532 { .compatible = "nvidia,tegra20-hdmi", .data = &tegra20_hdmi_config },
1533 { },
1534};
Stephen Warrenef707282014-06-18 16:21:55 -06001535MODULE_DEVICE_TABLE(of, tegra_hdmi_of_match);
Thierry Reding59af0592013-10-14 09:43:05 +02001536
Thierry Reding2ccb3962015-01-15 13:43:18 +01001537static irqreturn_t tegra_hdmi_irq(int irq, void *data)
1538{
1539 struct tegra_hdmi *hdmi = data;
1540 u32 value;
1541 int err;
1542
1543 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_INT_STATUS);
1544 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_INT_STATUS);
1545
1546 if (value & INT_CODEC_SCRATCH0) {
1547 unsigned int format;
1548 u32 value;
1549
1550 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_SOR_AUDIO_HDA_CODEC_SCRATCH0);
1551
1552 if (value & SOR_AUDIO_HDA_CODEC_SCRATCH0_VALID) {
Thierry Reding2ccb3962015-01-15 13:43:18 +01001553 format = value & SOR_AUDIO_HDA_CODEC_SCRATCH0_FMT_MASK;
1554
Thierry Redinge3c702d2019-01-03 15:23:17 +01001555 tegra_hda_parse_format(format, &hdmi->format);
Thierry Reding2ccb3962015-01-15 13:43:18 +01001556
1557 err = tegra_hdmi_setup_audio(hdmi);
1558 if (err < 0) {
1559 tegra_hdmi_disable_audio_infoframe(hdmi);
1560 tegra_hdmi_disable_audio(hdmi);
1561 } else {
1562 tegra_hdmi_setup_audio_infoframe(hdmi);
1563 tegra_hdmi_enable_audio_infoframe(hdmi);
1564 tegra_hdmi_enable_audio(hdmi);
1565 }
1566 } else {
1567 tegra_hdmi_disable_audio_infoframe(hdmi);
1568 tegra_hdmi_disable_audio(hdmi);
1569 }
1570 }
1571
1572 return IRQ_HANDLED;
1573}
1574
Thierry Redingedec4af2012-11-15 21:28:23 +00001575static int tegra_hdmi_probe(struct platform_device *pdev)
1576{
Thierry Redingedec4af2012-11-15 21:28:23 +00001577 struct tegra_hdmi *hdmi;
1578 struct resource *regs;
1579 int err;
1580
1581 hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
1582 if (!hdmi)
1583 return -ENOMEM;
1584
Thierry Reding5e4acd32017-08-21 18:05:10 +02001585 hdmi->config = of_device_get_match_data(&pdev->dev);
Thierry Redingedec4af2012-11-15 21:28:23 +00001586 hdmi->dev = &pdev->dev;
Thierry Reding2ccb3962015-01-15 13:43:18 +01001587
Thierry Redingedec4af2012-11-15 21:28:23 +00001588 hdmi->audio_source = AUTO;
Thierry Redingedec4af2012-11-15 21:28:23 +00001589 hdmi->stereo = false;
1590 hdmi->dvi = false;
1591
1592 hdmi->clk = devm_clk_get(&pdev->dev, NULL);
1593 if (IS_ERR(hdmi->clk)) {
1594 dev_err(&pdev->dev, "failed to get clock\n");
1595 return PTR_ERR(hdmi->clk);
1596 }
1597
Stephen Warrenca480802013-11-06 16:20:54 -07001598 hdmi->rst = devm_reset_control_get(&pdev->dev, "hdmi");
1599 if (IS_ERR(hdmi->rst)) {
1600 dev_err(&pdev->dev, "failed to get reset\n");
1601 return PTR_ERR(hdmi->rst);
1602 }
1603
Thierry Redingedec4af2012-11-15 21:28:23 +00001604 hdmi->clk_parent = devm_clk_get(&pdev->dev, "parent");
1605 if (IS_ERR(hdmi->clk_parent))
1606 return PTR_ERR(hdmi->clk_parent);
1607
Thierry Redingedec4af2012-11-15 21:28:23 +00001608 err = clk_set_parent(hdmi->clk, hdmi->clk_parent);
1609 if (err < 0) {
1610 dev_err(&pdev->dev, "failed to setup clocks: %d\n", err);
1611 return err;
1612 }
1613
Thierry Redingfb50a112014-02-28 16:57:34 +01001614 hdmi->hdmi = devm_regulator_get(&pdev->dev, "hdmi");
1615 if (IS_ERR(hdmi->hdmi)) {
1616 dev_err(&pdev->dev, "failed to get HDMI regulator\n");
1617 return PTR_ERR(hdmi->hdmi);
1618 }
1619
Thierry Redingedec4af2012-11-15 21:28:23 +00001620 hdmi->pll = devm_regulator_get(&pdev->dev, "pll");
1621 if (IS_ERR(hdmi->pll)) {
1622 dev_err(&pdev->dev, "failed to get PLL regulator\n");
1623 return PTR_ERR(hdmi->pll);
1624 }
1625
Thierry Reding88685682014-04-16 10:24:12 +02001626 hdmi->vdd = devm_regulator_get(&pdev->dev, "vdd");
1627 if (IS_ERR(hdmi->vdd)) {
1628 dev_err(&pdev->dev, "failed to get VDD regulator\n");
1629 return PTR_ERR(hdmi->vdd);
1630 }
1631
Thierry Redingedec4af2012-11-15 21:28:23 +00001632 hdmi->output.dev = &pdev->dev;
1633
Thierry Reding59d29c02013-10-14 14:26:42 +02001634 err = tegra_output_probe(&hdmi->output);
Thierry Redingedec4af2012-11-15 21:28:23 +00001635 if (err < 0)
1636 return err;
1637
1638 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Redingd4ed6022013-01-21 11:09:02 +01001639 hdmi->regs = devm_ioremap_resource(&pdev->dev, regs);
1640 if (IS_ERR(hdmi->regs))
1641 return PTR_ERR(hdmi->regs);
Thierry Redingedec4af2012-11-15 21:28:23 +00001642
1643 err = platform_get_irq(pdev, 0);
1644 if (err < 0)
1645 return err;
1646
1647 hdmi->irq = err;
1648
Thierry Reding2ccb3962015-01-15 13:43:18 +01001649 err = devm_request_irq(hdmi->dev, hdmi->irq, tegra_hdmi_irq, 0,
1650 dev_name(hdmi->dev), hdmi);
1651 if (err < 0) {
1652 dev_err(&pdev->dev, "failed to request IRQ#%u: %d\n",
1653 hdmi->irq, err);
1654 return err;
1655 }
1656
Thierry Reding52345492015-08-07 16:00:43 +02001657 platform_set_drvdata(pdev, hdmi);
1658 pm_runtime_enable(&pdev->dev);
1659
Thierry Reding776dc382013-10-14 14:43:22 +02001660 INIT_LIST_HEAD(&hdmi->client.list);
1661 hdmi->client.ops = &hdmi_client_ops;
1662 hdmi->client.dev = &pdev->dev;
Thierry Redingedec4af2012-11-15 21:28:23 +00001663
Thierry Reding776dc382013-10-14 14:43:22 +02001664 err = host1x_client_register(&hdmi->client);
Thierry Redingedec4af2012-11-15 21:28:23 +00001665 if (err < 0) {
1666 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
1667 err);
1668 return err;
1669 }
1670
Thierry Redingedec4af2012-11-15 21:28:23 +00001671 return 0;
1672}
1673
1674static int tegra_hdmi_remove(struct platform_device *pdev)
1675{
Thierry Redingedec4af2012-11-15 21:28:23 +00001676 struct tegra_hdmi *hdmi = platform_get_drvdata(pdev);
1677 int err;
1678
Thierry Reding52345492015-08-07 16:00:43 +02001679 pm_runtime_disable(&pdev->dev);
1680
Thierry Reding776dc382013-10-14 14:43:22 +02001681 err = host1x_client_unregister(&hdmi->client);
Thierry Redingedec4af2012-11-15 21:28:23 +00001682 if (err < 0) {
1683 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1684 err);
1685 return err;
1686 }
1687
Thierry Reding328ec692014-12-19 15:55:08 +01001688 tegra_output_remove(&hdmi->output);
Thierry Reding59d29c02013-10-14 14:26:42 +02001689
Thierry Reding52345492015-08-07 16:00:43 +02001690 return 0;
1691}
1692
1693#ifdef CONFIG_PM
1694static int tegra_hdmi_suspend(struct device *dev)
1695{
1696 struct tegra_hdmi *hdmi = dev_get_drvdata(dev);
1697 int err;
1698
1699 err = reset_control_assert(hdmi->rst);
1700 if (err < 0) {
1701 dev_err(dev, "failed to assert reset: %d\n", err);
1702 return err;
1703 }
1704
1705 usleep_range(1000, 2000);
1706
Thierry Redingd06e7f82014-04-16 10:43:41 +02001707 clk_disable_unprepare(hdmi->clk);
Thierry Redingedec4af2012-11-15 21:28:23 +00001708
1709 return 0;
1710}
1711
Thierry Reding52345492015-08-07 16:00:43 +02001712static int tegra_hdmi_resume(struct device *dev)
1713{
1714 struct tegra_hdmi *hdmi = dev_get_drvdata(dev);
1715 int err;
1716
1717 err = clk_prepare_enable(hdmi->clk);
1718 if (err < 0) {
1719 dev_err(dev, "failed to enable clock: %d\n", err);
1720 return err;
1721 }
1722
1723 usleep_range(1000, 2000);
1724
1725 err = reset_control_deassert(hdmi->rst);
1726 if (err < 0) {
1727 dev_err(dev, "failed to deassert reset: %d\n", err);
1728 clk_disable_unprepare(hdmi->clk);
1729 return err;
1730 }
1731
1732 return 0;
1733}
1734#endif
1735
1736static const struct dev_pm_ops tegra_hdmi_pm_ops = {
1737 SET_RUNTIME_PM_OPS(tegra_hdmi_suspend, tegra_hdmi_resume, NULL)
1738};
1739
Thierry Redingedec4af2012-11-15 21:28:23 +00001740struct platform_driver tegra_hdmi_driver = {
1741 .driver = {
1742 .name = "tegra-hdmi",
Thierry Redingedec4af2012-11-15 21:28:23 +00001743 .of_match_table = tegra_hdmi_of_match,
Thierry Reding52345492015-08-07 16:00:43 +02001744 .pm = &tegra_hdmi_pm_ops,
Thierry Redingedec4af2012-11-15 21:28:23 +00001745 },
1746 .probe = tegra_hdmi_probe,
1747 .remove = tegra_hdmi_remove,
1748};