blob: 334c4d7d238b44b3f52c86ea2b601cfded075367 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Thierry Redingedec4af2012-11-15 21:28:23 +00002/*
3 * Copyright (C) 2012 Avionic Design GmbH
4 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
Thierry Redingedec4af2012-11-15 21:28:23 +00005 */
6
7#include <linux/clk.h>
Thierry Reding9eb9b222013-09-24 16:32:47 +02008#include <linux/debugfs.h>
Thierry Reding59682712014-11-28 16:50:59 +01009#include <linux/gpio.h>
Thierry Reding9eb9b222013-09-24 16:32:47 +020010#include <linux/hdmi.h>
Alban Bedeldb5adf42016-12-14 18:20:39 +010011#include <linux/math64.h>
Thierry Reding5e4acd32017-08-21 18:05:10 +020012#include <linux/of_device.h>
Thierry Reding52345492015-08-07 16:00:43 +020013#include <linux/pm_runtime.h>
Thierry Reding9eb9b222013-09-24 16:32:47 +020014#include <linux/regulator/consumer.h>
Stephen Warrenca480802013-11-06 16:20:54 -070015#include <linux/reset.h>
Thierry Redingac24c222012-11-23 15:14:00 +010016
Thierry Reding4aa3df72014-11-24 16:27:13 +010017#include <drm/drm_atomic_helper.h>
Thierry Reding59682712014-11-28 16:50:59 +010018#include <drm/drm_crtc.h>
Daniel Vetterfcd70cd2019-01-17 22:03:34 +010019#include <drm/drm_probe_helper.h>
Thierry Reding59682712014-11-28 16:50:59 +010020
Thierry Redinge3c702d2019-01-03 15:23:17 +010021#include "hda.h"
Thierry Redingedec4af2012-11-15 21:28:23 +000022#include "hdmi.h"
23#include "drm.h"
24#include "dc.h"
Thierry Reding07a8aab2017-08-15 15:41:11 +020025#include "trace.h"
Thierry Redingedec4af2012-11-15 21:28:23 +000026
Thierry Reding2ccb3962015-01-15 13:43:18 +010027#define HDMI_ELD_BUFFER_SIZE 96
28
Thierry Reding59af0592013-10-14 09:43:05 +020029struct tmds_config {
30 unsigned int pclk;
31 u32 pll0;
32 u32 pll1;
33 u32 pe_current;
34 u32 drive_current;
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +020035 u32 peak_current;
Thierry Reding59af0592013-10-14 09:43:05 +020036};
37
38struct tegra_hdmi_config {
39 const struct tmds_config *tmds;
40 unsigned int num_tmds;
41
42 unsigned long fuse_override_offset;
Thierry Reding4ee8cee2014-12-08 16:25:14 +010043 u32 fuse_override_value;
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +020044
45 bool has_sor_io_peak_current;
Thierry Reding2ccb3962015-01-15 13:43:18 +010046 bool has_hda;
47 bool has_hbr;
Thierry Reding59af0592013-10-14 09:43:05 +020048};
49
Thierry Redingedec4af2012-11-15 21:28:23 +000050struct tegra_hdmi {
Thierry Reding776dc382013-10-14 14:43:22 +020051 struct host1x_client client;
Thierry Redingedec4af2012-11-15 21:28:23 +000052 struct tegra_output output;
53 struct device *dev;
54
Thierry Redingfb50a112014-02-28 16:57:34 +010055 struct regulator *hdmi;
Thierry Redingedec4af2012-11-15 21:28:23 +000056 struct regulator *pll;
Thierry Reding88685682014-04-16 10:24:12 +020057 struct regulator *vdd;
Thierry Redingedec4af2012-11-15 21:28:23 +000058
59 void __iomem *regs;
60 unsigned int irq;
61
62 struct clk *clk_parent;
63 struct clk *clk;
Stephen Warrenca480802013-11-06 16:20:54 -070064 struct reset_control *rst;
Thierry Redingedec4af2012-11-15 21:28:23 +000065
Thierry Reding59af0592013-10-14 09:43:05 +020066 const struct tegra_hdmi_config *config;
67
Thierry Redingedec4af2012-11-15 21:28:23 +000068 unsigned int audio_source;
Thierry Redinge3c702d2019-01-03 15:23:17 +010069 struct tegra_hda_format format;
Thierry Reding2ccb3962015-01-15 13:43:18 +010070
71 unsigned int pixel_clock;
Thierry Redingedec4af2012-11-15 21:28:23 +000072 bool stereo;
73 bool dvi;
74
75 struct drm_info_list *debugfs_files;
Thierry Redingedec4af2012-11-15 21:28:23 +000076};
77
78static inline struct tegra_hdmi *
Thierry Reding776dc382013-10-14 14:43:22 +020079host1x_client_to_hdmi(struct host1x_client *client)
Thierry Redingedec4af2012-11-15 21:28:23 +000080{
81 return container_of(client, struct tegra_hdmi, client);
82}
83
84static inline struct tegra_hdmi *to_hdmi(struct tegra_output *output)
85{
86 return container_of(output, struct tegra_hdmi, output);
87}
88
89#define HDMI_AUDIOCLK_FREQ 216000000
90#define HDMI_REKEY_DEFAULT 56
91
92enum {
93 AUTO = 0,
94 SPDIF,
95 HDA,
96};
97
Thierry Reding4ee8cee2014-12-08 16:25:14 +010098static inline u32 tegra_hdmi_readl(struct tegra_hdmi *hdmi,
Thierry Reding7efe20c2017-08-15 15:41:08 +020099 unsigned int offset)
Thierry Redingedec4af2012-11-15 21:28:23 +0000100{
Thierry Reding07a8aab2017-08-15 15:41:11 +0200101 u32 value = readl(hdmi->regs + (offset << 2));
102
103 trace_hdmi_readl(hdmi->dev, offset, value);
104
105 return value;
Thierry Redingedec4af2012-11-15 21:28:23 +0000106}
107
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100108static inline void tegra_hdmi_writel(struct tegra_hdmi *hdmi, u32 value,
Thierry Reding7efe20c2017-08-15 15:41:08 +0200109 unsigned int offset)
Thierry Redingedec4af2012-11-15 21:28:23 +0000110{
Thierry Reding07a8aab2017-08-15 15:41:11 +0200111 trace_hdmi_writel(hdmi->dev, offset, value);
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100112 writel(value, hdmi->regs + (offset << 2));
Thierry Redingedec4af2012-11-15 21:28:23 +0000113}
114
115struct tegra_hdmi_audio_config {
Thierry Redingedec4af2012-11-15 21:28:23 +0000116 unsigned int n;
117 unsigned int cts;
118 unsigned int aval;
119};
120
Thierry Redingf27db962013-09-30 15:14:41 +0200121static const struct tmds_config tegra20_tmds_config[] = {
Lucas Stachfa416dd2012-12-19 21:38:55 +0000122 { /* slow pixel clock modes */
Thierry Redingedec4af2012-11-15 21:28:23 +0000123 .pclk = 27000000,
124 .pll0 = SOR_PLL_BG_V17_S(3) | SOR_PLL_ICHPMP(1) |
125 SOR_PLL_RESISTORSEL | SOR_PLL_VCOCAP(0) |
126 SOR_PLL_TX_REG_LOAD(3),
127 .pll1 = SOR_PLL_TMDS_TERM_ENABLE,
128 .pe_current = PE_CURRENT0(PE_CURRENT_0_0_mA) |
129 PE_CURRENT1(PE_CURRENT_0_0_mA) |
130 PE_CURRENT2(PE_CURRENT_0_0_mA) |
131 PE_CURRENT3(PE_CURRENT_0_0_mA),
132 .drive_current = DRIVE_CURRENT_LANE0(DRIVE_CURRENT_7_125_mA) |
133 DRIVE_CURRENT_LANE1(DRIVE_CURRENT_7_125_mA) |
134 DRIVE_CURRENT_LANE2(DRIVE_CURRENT_7_125_mA) |
135 DRIVE_CURRENT_LANE3(DRIVE_CURRENT_7_125_mA),
Lucas Stachfa416dd2012-12-19 21:38:55 +0000136 },
137 { /* high pixel clock modes */
Thierry Redingedec4af2012-11-15 21:28:23 +0000138 .pclk = UINT_MAX,
139 .pll0 = SOR_PLL_BG_V17_S(3) | SOR_PLL_ICHPMP(1) |
140 SOR_PLL_RESISTORSEL | SOR_PLL_VCOCAP(1) |
141 SOR_PLL_TX_REG_LOAD(3),
142 .pll1 = SOR_PLL_TMDS_TERM_ENABLE | SOR_PLL_PE_EN,
143 .pe_current = PE_CURRENT0(PE_CURRENT_6_0_mA) |
144 PE_CURRENT1(PE_CURRENT_6_0_mA) |
145 PE_CURRENT2(PE_CURRENT_6_0_mA) |
146 PE_CURRENT3(PE_CURRENT_6_0_mA),
147 .drive_current = DRIVE_CURRENT_LANE0(DRIVE_CURRENT_7_125_mA) |
148 DRIVE_CURRENT_LANE1(DRIVE_CURRENT_7_125_mA) |
149 DRIVE_CURRENT_LANE2(DRIVE_CURRENT_7_125_mA) |
150 DRIVE_CURRENT_LANE3(DRIVE_CURRENT_7_125_mA),
151 },
152};
153
Thierry Redingf27db962013-09-30 15:14:41 +0200154static const struct tmds_config tegra30_tmds_config[] = {
Thierry Redingedec4af2012-11-15 21:28:23 +0000155 { /* 480p modes */
156 .pclk = 27000000,
157 .pll0 = SOR_PLL_BG_V17_S(3) | SOR_PLL_ICHPMP(1) |
158 SOR_PLL_RESISTORSEL | SOR_PLL_VCOCAP(0) |
159 SOR_PLL_TX_REG_LOAD(0),
160 .pll1 = SOR_PLL_TMDS_TERM_ENABLE,
161 .pe_current = PE_CURRENT0(PE_CURRENT_0_0_mA) |
162 PE_CURRENT1(PE_CURRENT_0_0_mA) |
163 PE_CURRENT2(PE_CURRENT_0_0_mA) |
164 PE_CURRENT3(PE_CURRENT_0_0_mA),
165 .drive_current = DRIVE_CURRENT_LANE0(DRIVE_CURRENT_5_250_mA) |
166 DRIVE_CURRENT_LANE1(DRIVE_CURRENT_5_250_mA) |
167 DRIVE_CURRENT_LANE2(DRIVE_CURRENT_5_250_mA) |
168 DRIVE_CURRENT_LANE3(DRIVE_CURRENT_5_250_mA),
169 }, { /* 720p modes */
170 .pclk = 74250000,
171 .pll0 = SOR_PLL_BG_V17_S(3) | SOR_PLL_ICHPMP(1) |
172 SOR_PLL_RESISTORSEL | SOR_PLL_VCOCAP(1) |
173 SOR_PLL_TX_REG_LOAD(0),
174 .pll1 = SOR_PLL_TMDS_TERM_ENABLE | SOR_PLL_PE_EN,
175 .pe_current = PE_CURRENT0(PE_CURRENT_5_0_mA) |
176 PE_CURRENT1(PE_CURRENT_5_0_mA) |
177 PE_CURRENT2(PE_CURRENT_5_0_mA) |
178 PE_CURRENT3(PE_CURRENT_5_0_mA),
179 .drive_current = DRIVE_CURRENT_LANE0(DRIVE_CURRENT_5_250_mA) |
180 DRIVE_CURRENT_LANE1(DRIVE_CURRENT_5_250_mA) |
181 DRIVE_CURRENT_LANE2(DRIVE_CURRENT_5_250_mA) |
182 DRIVE_CURRENT_LANE3(DRIVE_CURRENT_5_250_mA),
183 }, { /* 1080p modes */
184 .pclk = UINT_MAX,
185 .pll0 = SOR_PLL_BG_V17_S(3) | SOR_PLL_ICHPMP(1) |
186 SOR_PLL_RESISTORSEL | SOR_PLL_VCOCAP(3) |
187 SOR_PLL_TX_REG_LOAD(0),
188 .pll1 = SOR_PLL_TMDS_TERM_ENABLE | SOR_PLL_PE_EN,
189 .pe_current = PE_CURRENT0(PE_CURRENT_5_0_mA) |
190 PE_CURRENT1(PE_CURRENT_5_0_mA) |
191 PE_CURRENT2(PE_CURRENT_5_0_mA) |
192 PE_CURRENT3(PE_CURRENT_5_0_mA),
193 .drive_current = DRIVE_CURRENT_LANE0(DRIVE_CURRENT_5_250_mA) |
194 DRIVE_CURRENT_LANE1(DRIVE_CURRENT_5_250_mA) |
195 DRIVE_CURRENT_LANE2(DRIVE_CURRENT_5_250_mA) |
196 DRIVE_CURRENT_LANE3(DRIVE_CURRENT_5_250_mA),
197 },
198};
199
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +0200200static const struct tmds_config tegra114_tmds_config[] = {
201 { /* 480p/576p / 25.2MHz/27MHz modes */
202 .pclk = 27000000,
203 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
204 SOR_PLL_VCOCAP(0) | SOR_PLL_RESISTORSEL,
205 .pll1 = SOR_PLL_LOADADJ(3) | SOR_PLL_TMDS_TERMADJ(0),
206 .pe_current = PE_CURRENT0(PE_CURRENT_0_mA_T114) |
207 PE_CURRENT1(PE_CURRENT_0_mA_T114) |
208 PE_CURRENT2(PE_CURRENT_0_mA_T114) |
209 PE_CURRENT3(PE_CURRENT_0_mA_T114),
210 .drive_current =
211 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_10_400_mA_T114) |
212 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_10_400_mA_T114) |
213 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_10_400_mA_T114) |
214 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_10_400_mA_T114),
215 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_0_000_mA) |
216 PEAK_CURRENT_LANE1(PEAK_CURRENT_0_000_mA) |
217 PEAK_CURRENT_LANE2(PEAK_CURRENT_0_000_mA) |
218 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_000_mA),
219 }, { /* 720p / 74.25MHz modes */
220 .pclk = 74250000,
221 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
222 SOR_PLL_VCOCAP(1) | SOR_PLL_RESISTORSEL,
223 .pll1 = SOR_PLL_PE_EN | SOR_PLL_LOADADJ(3) |
224 SOR_PLL_TMDS_TERMADJ(0),
225 .pe_current = PE_CURRENT0(PE_CURRENT_15_mA_T114) |
226 PE_CURRENT1(PE_CURRENT_15_mA_T114) |
227 PE_CURRENT2(PE_CURRENT_15_mA_T114) |
228 PE_CURRENT3(PE_CURRENT_15_mA_T114),
229 .drive_current =
230 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_10_400_mA_T114) |
231 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_10_400_mA_T114) |
232 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_10_400_mA_T114) |
233 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_10_400_mA_T114),
234 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_0_000_mA) |
235 PEAK_CURRENT_LANE1(PEAK_CURRENT_0_000_mA) |
236 PEAK_CURRENT_LANE2(PEAK_CURRENT_0_000_mA) |
237 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_000_mA),
238 }, { /* 1080p / 148.5MHz modes */
239 .pclk = 148500000,
240 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
241 SOR_PLL_VCOCAP(3) | SOR_PLL_RESISTORSEL,
242 .pll1 = SOR_PLL_PE_EN | SOR_PLL_LOADADJ(3) |
243 SOR_PLL_TMDS_TERMADJ(0),
244 .pe_current = PE_CURRENT0(PE_CURRENT_10_mA_T114) |
245 PE_CURRENT1(PE_CURRENT_10_mA_T114) |
246 PE_CURRENT2(PE_CURRENT_10_mA_T114) |
247 PE_CURRENT3(PE_CURRENT_10_mA_T114),
248 .drive_current =
249 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_12_400_mA_T114) |
250 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_12_400_mA_T114) |
251 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_12_400_mA_T114) |
252 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_12_400_mA_T114),
253 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_0_000_mA) |
254 PEAK_CURRENT_LANE1(PEAK_CURRENT_0_000_mA) |
255 PEAK_CURRENT_LANE2(PEAK_CURRENT_0_000_mA) |
256 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_000_mA),
257 }, { /* 225/297MHz modes */
258 .pclk = UINT_MAX,
259 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
260 SOR_PLL_VCOCAP(0xf) | SOR_PLL_RESISTORSEL,
261 .pll1 = SOR_PLL_LOADADJ(3) | SOR_PLL_TMDS_TERMADJ(7)
262 | SOR_PLL_TMDS_TERM_ENABLE,
263 .pe_current = PE_CURRENT0(PE_CURRENT_0_mA_T114) |
264 PE_CURRENT1(PE_CURRENT_0_mA_T114) |
265 PE_CURRENT2(PE_CURRENT_0_mA_T114) |
266 PE_CURRENT3(PE_CURRENT_0_mA_T114),
267 .drive_current =
268 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_25_200_mA_T114) |
269 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_25_200_mA_T114) |
270 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_25_200_mA_T114) |
271 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_19_200_mA_T114),
272 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_3_000_mA) |
273 PEAK_CURRENT_LANE1(PEAK_CURRENT_3_000_mA) |
274 PEAK_CURRENT_LANE2(PEAK_CURRENT_3_000_mA) |
275 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_800_mA),
276 },
277};
278
Thierry Redingfb7be702013-11-15 16:07:32 +0100279static const struct tmds_config tegra124_tmds_config[] = {
280 { /* 480p/576p / 25.2MHz/27MHz modes */
281 .pclk = 27000000,
282 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
283 SOR_PLL_VCOCAP(0) | SOR_PLL_RESISTORSEL,
284 .pll1 = SOR_PLL_LOADADJ(3) | SOR_PLL_TMDS_TERMADJ(0),
285 .pe_current = PE_CURRENT0(PE_CURRENT_0_mA_T114) |
286 PE_CURRENT1(PE_CURRENT_0_mA_T114) |
287 PE_CURRENT2(PE_CURRENT_0_mA_T114) |
288 PE_CURRENT3(PE_CURRENT_0_mA_T114),
289 .drive_current =
290 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_10_400_mA_T114) |
291 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_10_400_mA_T114) |
292 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_10_400_mA_T114) |
293 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_10_400_mA_T114),
294 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_0_000_mA) |
295 PEAK_CURRENT_LANE1(PEAK_CURRENT_0_000_mA) |
296 PEAK_CURRENT_LANE2(PEAK_CURRENT_0_000_mA) |
297 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_000_mA),
298 }, { /* 720p / 74.25MHz modes */
299 .pclk = 74250000,
300 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
301 SOR_PLL_VCOCAP(1) | SOR_PLL_RESISTORSEL,
302 .pll1 = SOR_PLL_PE_EN | SOR_PLL_LOADADJ(3) |
303 SOR_PLL_TMDS_TERMADJ(0),
304 .pe_current = PE_CURRENT0(PE_CURRENT_15_mA_T114) |
305 PE_CURRENT1(PE_CURRENT_15_mA_T114) |
306 PE_CURRENT2(PE_CURRENT_15_mA_T114) |
307 PE_CURRENT3(PE_CURRENT_15_mA_T114),
308 .drive_current =
309 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_10_400_mA_T114) |
310 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_10_400_mA_T114) |
311 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_10_400_mA_T114) |
312 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_10_400_mA_T114),
313 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_0_000_mA) |
314 PEAK_CURRENT_LANE1(PEAK_CURRENT_0_000_mA) |
315 PEAK_CURRENT_LANE2(PEAK_CURRENT_0_000_mA) |
316 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_000_mA),
317 }, { /* 1080p / 148.5MHz modes */
318 .pclk = 148500000,
319 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
320 SOR_PLL_VCOCAP(3) | SOR_PLL_RESISTORSEL,
321 .pll1 = SOR_PLL_PE_EN | SOR_PLL_LOADADJ(3) |
322 SOR_PLL_TMDS_TERMADJ(0),
323 .pe_current = PE_CURRENT0(PE_CURRENT_10_mA_T114) |
324 PE_CURRENT1(PE_CURRENT_10_mA_T114) |
325 PE_CURRENT2(PE_CURRENT_10_mA_T114) |
326 PE_CURRENT3(PE_CURRENT_10_mA_T114),
327 .drive_current =
328 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_12_400_mA_T114) |
329 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_12_400_mA_T114) |
330 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_12_400_mA_T114) |
331 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_12_400_mA_T114),
332 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_0_000_mA) |
333 PEAK_CURRENT_LANE1(PEAK_CURRENT_0_000_mA) |
334 PEAK_CURRENT_LANE2(PEAK_CURRENT_0_000_mA) |
335 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_000_mA),
336 }, { /* 225/297MHz modes */
337 .pclk = UINT_MAX,
338 .pll0 = SOR_PLL_ICHPMP(1) | SOR_PLL_BG_V17_S(3) |
339 SOR_PLL_VCOCAP(0xf) | SOR_PLL_RESISTORSEL,
340 .pll1 = SOR_PLL_LOADADJ(3) | SOR_PLL_TMDS_TERMADJ(7)
341 | SOR_PLL_TMDS_TERM_ENABLE,
342 .pe_current = PE_CURRENT0(PE_CURRENT_0_mA_T114) |
343 PE_CURRENT1(PE_CURRENT_0_mA_T114) |
344 PE_CURRENT2(PE_CURRENT_0_mA_T114) |
345 PE_CURRENT3(PE_CURRENT_0_mA_T114),
346 .drive_current =
347 DRIVE_CURRENT_LANE0_T114(DRIVE_CURRENT_25_200_mA_T114) |
348 DRIVE_CURRENT_LANE1_T114(DRIVE_CURRENT_25_200_mA_T114) |
349 DRIVE_CURRENT_LANE2_T114(DRIVE_CURRENT_25_200_mA_T114) |
350 DRIVE_CURRENT_LANE3_T114(DRIVE_CURRENT_19_200_mA_T114),
351 .peak_current = PEAK_CURRENT_LANE0(PEAK_CURRENT_3_000_mA) |
352 PEAK_CURRENT_LANE1(PEAK_CURRENT_3_000_mA) |
353 PEAK_CURRENT_LANE2(PEAK_CURRENT_3_000_mA) |
354 PEAK_CURRENT_LANE3(PEAK_CURRENT_0_800_mA),
355 },
356};
357
Alban Bedeldb5adf42016-12-14 18:20:39 +0100358static int
359tegra_hdmi_get_audio_config(unsigned int audio_freq, unsigned int pix_clock,
360 struct tegra_hdmi_audio_config *config)
Thierry Redingedec4af2012-11-15 21:28:23 +0000361{
Alban Bedeldb5adf42016-12-14 18:20:39 +0100362 const unsigned int afreq = 128 * audio_freq;
363 const unsigned int min_n = afreq / 1500;
364 const unsigned int max_n = afreq / 300;
365 const unsigned int ideal_n = afreq / 1000;
366 int64_t min_err = (uint64_t)-1 >> 1;
367 unsigned int min_delta = -1;
368 int n;
Thierry Redingedec4af2012-11-15 21:28:23 +0000369
Alban Bedeldb5adf42016-12-14 18:20:39 +0100370 memset(config, 0, sizeof(*config));
371 config->n = -1;
Thierry Redingedec4af2012-11-15 21:28:23 +0000372
Alban Bedeldb5adf42016-12-14 18:20:39 +0100373 for (n = min_n; n <= max_n; n++) {
374 uint64_t cts_f, aval_f;
375 unsigned int delta;
376 int64_t cts, err;
Thierry Redingedec4af2012-11-15 21:28:23 +0000377
Alban Bedeldb5adf42016-12-14 18:20:39 +0100378 /* compute aval in 48.16 fixed point */
379 aval_f = ((int64_t)24000000 << 16) * n;
380 do_div(aval_f, afreq);
381 /* It should round without any rest */
382 if (aval_f & 0xFFFF)
383 continue;
Thierry Redingedec4af2012-11-15 21:28:23 +0000384
Alban Bedeldb5adf42016-12-14 18:20:39 +0100385 /* Compute cts in 48.16 fixed point */
386 cts_f = ((int64_t)pix_clock << 16) * n;
387 do_div(cts_f, afreq);
388 /* Round it to the nearest integer */
389 cts = (cts_f & ~0xFFFF) + ((cts_f & BIT(15)) << 1);
Thierry Redingedec4af2012-11-15 21:28:23 +0000390
Alban Bedeldb5adf42016-12-14 18:20:39 +0100391 delta = abs(n - ideal_n);
Thierry Redingedec4af2012-11-15 21:28:23 +0000392
Alban Bedeldb5adf42016-12-14 18:20:39 +0100393 /* Compute the absolute error */
394 err = abs((int64_t)cts_f - cts);
395 if (err < min_err || (err == min_err && delta < min_delta)) {
396 config->n = n;
397 config->cts = cts >> 16;
398 config->aval = aval_f >> 16;
399 min_delta = delta;
400 min_err = err;
401 }
Thierry Redingedec4af2012-11-15 21:28:23 +0000402 }
403
Alban Bedeldb5adf42016-12-14 18:20:39 +0100404 return config->n != -1 ? 0 : -EINVAL;
Thierry Redingedec4af2012-11-15 21:28:23 +0000405}
406
407static void tegra_hdmi_setup_audio_fs_tables(struct tegra_hdmi *hdmi)
408{
409 const unsigned int freqs[] = {
410 32000, 44100, 48000, 88200, 96000, 176400, 192000
411 };
412 unsigned int i;
413
414 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
415 unsigned int f = freqs[i];
416 unsigned int eight_half;
Thierry Redingedec4af2012-11-15 21:28:23 +0000417 unsigned int delta;
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100418 u32 value;
Thierry Redingedec4af2012-11-15 21:28:23 +0000419
420 if (f > 96000)
421 delta = 2;
Thierry Reding17a8b6b2013-12-16 10:01:24 +0100422 else if (f > 48000)
Thierry Redingedec4af2012-11-15 21:28:23 +0000423 delta = 6;
424 else
425 delta = 9;
426
427 eight_half = (8 * HDMI_AUDIOCLK_FREQ) / (f * 128);
428 value = AUDIO_FS_LOW(eight_half - delta) |
429 AUDIO_FS_HIGH(eight_half + delta);
430 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_AUDIO_FS(i));
431 }
432}
433
Thierry Reding2ccb3962015-01-15 13:43:18 +0100434static void tegra_hdmi_write_aval(struct tegra_hdmi *hdmi, u32 value)
Thierry Redingedec4af2012-11-15 21:28:23 +0000435{
Thierry Reding2ccb3962015-01-15 13:43:18 +0100436 static const struct {
437 unsigned int sample_rate;
438 unsigned int offset;
439 } regs[] = {
440 { 32000, HDMI_NV_PDISP_SOR_AUDIO_AVAL_0320 },
441 { 44100, HDMI_NV_PDISP_SOR_AUDIO_AVAL_0441 },
442 { 48000, HDMI_NV_PDISP_SOR_AUDIO_AVAL_0480 },
443 { 88200, HDMI_NV_PDISP_SOR_AUDIO_AVAL_0882 },
444 { 96000, HDMI_NV_PDISP_SOR_AUDIO_AVAL_0960 },
445 { 176400, HDMI_NV_PDISP_SOR_AUDIO_AVAL_1764 },
446 { 192000, HDMI_NV_PDISP_SOR_AUDIO_AVAL_1920 },
447 };
448 unsigned int i;
449
450 for (i = 0; i < ARRAY_SIZE(regs); i++) {
Thierry Redinge3c702d2019-01-03 15:23:17 +0100451 if (regs[i].sample_rate == hdmi->format.sample_rate) {
Thierry Reding2ccb3962015-01-15 13:43:18 +0100452 tegra_hdmi_writel(hdmi, value, regs[i].offset);
453 break;
454 }
455 }
456}
457
458static int tegra_hdmi_setup_audio(struct tegra_hdmi *hdmi)
459{
Alban Bedeldb5adf42016-12-14 18:20:39 +0100460 struct tegra_hdmi_audio_config config;
Thierry Reding2ccb3962015-01-15 13:43:18 +0100461 u32 source, value;
Alban Bedeldb5adf42016-12-14 18:20:39 +0100462 int err;
Thierry Redingedec4af2012-11-15 21:28:23 +0000463
464 switch (hdmi->audio_source) {
465 case HDA:
Thierry Reding2ccb3962015-01-15 13:43:18 +0100466 if (hdmi->config->has_hda)
467 source = SOR_AUDIO_CNTRL0_SOURCE_SELECT_HDAL;
468 else
469 return -EINVAL;
470
Thierry Redingedec4af2012-11-15 21:28:23 +0000471 break;
472
473 case SPDIF:
Thierry Reding2ccb3962015-01-15 13:43:18 +0100474 if (hdmi->config->has_hda)
475 source = SOR_AUDIO_CNTRL0_SOURCE_SELECT_SPDIF;
476 else
477 source = AUDIO_CNTRL0_SOURCE_SELECT_SPDIF;
Thierry Redingedec4af2012-11-15 21:28:23 +0000478 break;
479
480 default:
Thierry Reding2ccb3962015-01-15 13:43:18 +0100481 if (hdmi->config->has_hda)
482 source = SOR_AUDIO_CNTRL0_SOURCE_SELECT_AUTO;
483 else
484 source = AUDIO_CNTRL0_SOURCE_SELECT_AUTO;
Thierry Redingedec4af2012-11-15 21:28:23 +0000485 break;
486 }
487
Thierry Reding2ccb3962015-01-15 13:43:18 +0100488 /*
489 * Tegra30 and later use a slightly modified version of the register
490 * layout to accomodate for changes related to supporting HDA as the
491 * audio input source for HDMI. The source select field has moved to
492 * the SOR_AUDIO_CNTRL0 register, but the error tolerance and frames
493 * per block fields remain in the AUDIO_CNTRL0 register.
494 */
495 if (hdmi->config->has_hda) {
496 /*
497 * Inject null samples into the audio FIFO for every frame in
498 * which the codec did not receive any samples. This applies
499 * to stereo LPCM only.
500 *
501 * XXX: This seems to be a remnant of MCP days when this was
502 * used to work around issues with monitors not being able to
503 * play back system startup sounds early. It is possibly not
504 * needed on Linux at all.
505 */
Thierry Redinge3c702d2019-01-03 15:23:17 +0100506 if (hdmi->format.channels == 2)
Thierry Reding2ccb3962015-01-15 13:43:18 +0100507 value = SOR_AUDIO_CNTRL0_INJECT_NULLSMPL;
508 else
509 value = 0;
Thierry Redingedec4af2012-11-15 21:28:23 +0000510
Thierry Reding2ccb3962015-01-15 13:43:18 +0100511 value |= source;
512
513 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_AUDIO_CNTRL0);
Thierry Redingedec4af2012-11-15 21:28:23 +0000514 }
515
Thierry Reding2ccb3962015-01-15 13:43:18 +0100516 /*
517 * On Tegra20, HDA is not a supported audio source and the source
518 * select field is part of the AUDIO_CNTRL0 register.
519 */
520 value = AUDIO_CNTRL0_FRAMES_PER_BLOCK(0xc0) |
521 AUDIO_CNTRL0_ERROR_TOLERANCE(6);
522
523 if (!hdmi->config->has_hda)
524 value |= source;
525
526 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_AUDIO_CNTRL0);
527
528 /*
529 * Advertise support for High Bit-Rate on Tegra114 and later.
530 */
531 if (hdmi->config->has_hbr) {
532 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_SOR_AUDIO_SPARE0);
533 value |= SOR_AUDIO_SPARE0_HBR_ENABLE;
534 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_AUDIO_SPARE0);
535 }
536
Alban Bedeldb5adf42016-12-14 18:20:39 +0100537 err = tegra_hdmi_get_audio_config(hdmi->format.sample_rate,
538 hdmi->pixel_clock, &config);
539 if (err < 0) {
Thierry Reding2ccb3962015-01-15 13:43:18 +0100540 dev_err(hdmi->dev,
541 "cannot set audio to %u Hz at %u Hz pixel clock\n",
Thierry Redinge3c702d2019-01-03 15:23:17 +0100542 hdmi->format.sample_rate, hdmi->pixel_clock);
Alban Bedeldb5adf42016-12-14 18:20:39 +0100543 return err;
Thierry Redingedec4af2012-11-15 21:28:23 +0000544 }
545
Alban Bedeldb5adf42016-12-14 18:20:39 +0100546 dev_dbg(hdmi->dev, "audio: pixclk=%u, n=%u, cts=%u, aval=%u\n",
547 hdmi->pixel_clock, config.n, config.cts, config.aval);
548
Thierry Redingedec4af2012-11-15 21:28:23 +0000549 tegra_hdmi_writel(hdmi, 0, HDMI_NV_PDISP_HDMI_ACR_CTRL);
550
551 value = AUDIO_N_RESETF | AUDIO_N_GENERATE_ALTERNATE |
Alban Bedeldb5adf42016-12-14 18:20:39 +0100552 AUDIO_N_VALUE(config.n - 1);
Thierry Redingedec4af2012-11-15 21:28:23 +0000553 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_AUDIO_N);
554
Alban Bedeldb5adf42016-12-14 18:20:39 +0100555 tegra_hdmi_writel(hdmi, ACR_SUBPACK_N(config.n) | ACR_ENABLE,
Thierry Redingedec4af2012-11-15 21:28:23 +0000556 HDMI_NV_PDISP_HDMI_ACR_0441_SUBPACK_HIGH);
557
Alban Bedeldb5adf42016-12-14 18:20:39 +0100558 tegra_hdmi_writel(hdmi, ACR_SUBPACK_CTS(config.cts),
Thierry Reding2ccb3962015-01-15 13:43:18 +0100559 HDMI_NV_PDISP_HDMI_ACR_0441_SUBPACK_LOW);
Thierry Redingedec4af2012-11-15 21:28:23 +0000560
561 value = SPARE_HW_CTS | SPARE_FORCE_SW_CTS | SPARE_CTS_RESET_VAL(1);
562 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_SPARE);
563
564 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_AUDIO_N);
565 value &= ~AUDIO_N_RESETF;
566 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_AUDIO_N);
567
Thierry Reding2ccb3962015-01-15 13:43:18 +0100568 if (hdmi->config->has_hda)
Alban Bedeldb5adf42016-12-14 18:20:39 +0100569 tegra_hdmi_write_aval(hdmi, config.aval);
Thierry Redingedec4af2012-11-15 21:28:23 +0000570
571 tegra_hdmi_setup_audio_fs_tables(hdmi);
572
573 return 0;
574}
575
Thierry Reding2ccb3962015-01-15 13:43:18 +0100576static void tegra_hdmi_disable_audio(struct tegra_hdmi *hdmi)
577{
578 u32 value;
579
580 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
581 value &= ~GENERIC_CTRL_AUDIO;
582 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
583}
584
585static void tegra_hdmi_enable_audio(struct tegra_hdmi *hdmi)
586{
587 u32 value;
588
589 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
590 value |= GENERIC_CTRL_AUDIO;
591 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
592}
593
Thierry Reding52345492015-08-07 16:00:43 +0200594static void tegra_hdmi_write_eld(struct tegra_hdmi *hdmi)
595{
596 size_t length = drm_eld_size(hdmi->output.connector.eld), i;
597 u32 value;
598
599 for (i = 0; i < length; i++)
600 tegra_hdmi_writel(hdmi, i << 8 | hdmi->output.connector.eld[i],
601 HDMI_NV_PDISP_SOR_AUDIO_HDA_ELD_BUFWR);
602
603 /*
604 * The HDA codec will always report an ELD buffer size of 96 bytes and
605 * the HDA codec driver will check that each byte read from the buffer
606 * is valid. Therefore every byte must be written, even if no 96 bytes
607 * were parsed from EDID.
608 */
609 for (i = length; i < HDMI_ELD_BUFFER_SIZE; i++)
610 tegra_hdmi_writel(hdmi, i << 8 | 0,
611 HDMI_NV_PDISP_SOR_AUDIO_HDA_ELD_BUFWR);
612
613 value = SOR_AUDIO_HDA_PRESENSE_VALID | SOR_AUDIO_HDA_PRESENSE_PRESENT;
614 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_AUDIO_HDA_PRESENSE);
615}
616
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100617static inline u32 tegra_hdmi_subpack(const u8 *ptr, size_t size)
Thierry Redingedec4af2012-11-15 21:28:23 +0000618{
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100619 u32 value = 0;
Thierry Redingedec4af2012-11-15 21:28:23 +0000620 size_t i;
Thierry Redingedec4af2012-11-15 21:28:23 +0000621
Thierry Redingac24c222012-11-23 15:14:00 +0100622 for (i = size; i > 0; i--)
623 value = (value << 8) | ptr[i - 1];
Thierry Redingedec4af2012-11-15 21:28:23 +0000624
Thierry Redingac24c222012-11-23 15:14:00 +0100625 return value;
626}
Thierry Redingedec4af2012-11-15 21:28:23 +0000627
Thierry Redingac24c222012-11-23 15:14:00 +0100628static void tegra_hdmi_write_infopack(struct tegra_hdmi *hdmi, const void *data,
629 size_t size)
630{
631 const u8 *ptr = data;
632 unsigned long offset;
Thierry Redingac24c222012-11-23 15:14:00 +0100633 size_t i, j;
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100634 u32 value;
Thierry Redingedec4af2012-11-15 21:28:23 +0000635
Thierry Redingac24c222012-11-23 15:14:00 +0100636 switch (ptr[0]) {
637 case HDMI_INFOFRAME_TYPE_AVI:
638 offset = HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_HEADER;
639 break;
640
641 case HDMI_INFOFRAME_TYPE_AUDIO:
642 offset = HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_HEADER;
643 break;
644
645 case HDMI_INFOFRAME_TYPE_VENDOR:
646 offset = HDMI_NV_PDISP_HDMI_GENERIC_HEADER;
647 break;
648
649 default:
650 dev_err(hdmi->dev, "unsupported infoframe type: %02x\n",
651 ptr[0]);
652 return;
653 }
654
655 value = INFOFRAME_HEADER_TYPE(ptr[0]) |
656 INFOFRAME_HEADER_VERSION(ptr[1]) |
657 INFOFRAME_HEADER_LEN(ptr[2]);
Thierry Redingedec4af2012-11-15 21:28:23 +0000658 tegra_hdmi_writel(hdmi, value, offset);
Thierry Redingac24c222012-11-23 15:14:00 +0100659 offset++;
Thierry Redingedec4af2012-11-15 21:28:23 +0000660
Thierry Redingac24c222012-11-23 15:14:00 +0100661 /*
662 * Each subpack contains 7 bytes, divided into:
663 * - subpack_low: bytes 0 - 3
664 * - subpack_high: bytes 4 - 6 (with byte 7 padded to 0x00)
Thierry Redingedec4af2012-11-15 21:28:23 +0000665 */
Thierry Redingac24c222012-11-23 15:14:00 +0100666 for (i = 3, j = 0; i < size; i += 7, j += 8) {
667 size_t rem = size - i, num = min_t(size_t, rem, 4);
Thierry Redingedec4af2012-11-15 21:28:23 +0000668
Thierry Redingac24c222012-11-23 15:14:00 +0100669 value = tegra_hdmi_subpack(&ptr[i], num);
670 tegra_hdmi_writel(hdmi, value, offset++);
Thierry Redingedec4af2012-11-15 21:28:23 +0000671
Thierry Redingac24c222012-11-23 15:14:00 +0100672 num = min_t(size_t, rem - num, 3);
Thierry Redingedec4af2012-11-15 21:28:23 +0000673
Thierry Redingac24c222012-11-23 15:14:00 +0100674 value = tegra_hdmi_subpack(&ptr[i + 4], num);
675 tegra_hdmi_writel(hdmi, value, offset++);
Thierry Redingedec4af2012-11-15 21:28:23 +0000676 }
677}
678
679static void tegra_hdmi_setup_avi_infoframe(struct tegra_hdmi *hdmi,
680 struct drm_display_mode *mode)
681{
682 struct hdmi_avi_infoframe frame;
Thierry Redingac24c222012-11-23 15:14:00 +0100683 u8 buffer[17];
684 ssize_t err;
Thierry Redingedec4af2012-11-15 21:28:23 +0000685
Ville Syrjälä13d0add2019-01-08 19:28:25 +0200686 err = drm_hdmi_avi_infoframe_from_display_mode(&frame,
687 &hdmi->output.connector, mode);
Thierry Redingac24c222012-11-23 15:14:00 +0100688 if (err < 0) {
689 dev_err(hdmi->dev, "failed to setup AVI infoframe: %zd\n", err);
690 return;
Thierry Redingedec4af2012-11-15 21:28:23 +0000691 }
692
Thierry Redingac24c222012-11-23 15:14:00 +0100693 err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
694 if (err < 0) {
695 dev_err(hdmi->dev, "failed to pack AVI infoframe: %zd\n", err);
696 return;
697 }
698
699 tegra_hdmi_write_infopack(hdmi, buffer, err);
Thierry Reding2ccb3962015-01-15 13:43:18 +0100700}
Thierry Redingedec4af2012-11-15 21:28:23 +0000701
Thierry Reding2ccb3962015-01-15 13:43:18 +0100702static void tegra_hdmi_disable_avi_infoframe(struct tegra_hdmi *hdmi)
703{
704 u32 value;
705
706 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_CTRL);
707 value &= ~INFOFRAME_CTRL_ENABLE;
708 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_CTRL);
709}
710
711static void tegra_hdmi_enable_avi_infoframe(struct tegra_hdmi *hdmi)
712{
713 u32 value;
714
715 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_CTRL);
716 value |= INFOFRAME_CTRL_ENABLE;
717 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_CTRL);
Thierry Redingedec4af2012-11-15 21:28:23 +0000718}
719
720static void tegra_hdmi_setup_audio_infoframe(struct tegra_hdmi *hdmi)
721{
722 struct hdmi_audio_infoframe frame;
Thierry Redingac24c222012-11-23 15:14:00 +0100723 u8 buffer[14];
724 ssize_t err;
Thierry Redingedec4af2012-11-15 21:28:23 +0000725
Thierry Redingac24c222012-11-23 15:14:00 +0100726 err = hdmi_audio_infoframe_init(&frame);
727 if (err < 0) {
Thierry Redingef284c72013-10-16 19:51:22 +0200728 dev_err(hdmi->dev, "failed to setup audio infoframe: %zd\n",
Thierry Redingac24c222012-11-23 15:14:00 +0100729 err);
730 return;
731 }
Thierry Redingedec4af2012-11-15 21:28:23 +0000732
Thierry Redinge3c702d2019-01-03 15:23:17 +0100733 frame.channels = hdmi->format.channels;
Thierry Redingac24c222012-11-23 15:14:00 +0100734
735 err = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
736 if (err < 0) {
737 dev_err(hdmi->dev, "failed to pack audio infoframe: %zd\n",
738 err);
739 return;
740 }
741
742 /*
743 * The audio infoframe has only one set of subpack registers, so the
744 * infoframe needs to be truncated. One set of subpack registers can
745 * contain 7 bytes. Including the 3 byte header only the first 10
746 * bytes can be programmed.
747 */
Thierry Redingef284c72013-10-16 19:51:22 +0200748 tegra_hdmi_write_infopack(hdmi, buffer, min_t(size_t, 10, err));
Thierry Reding2ccb3962015-01-15 13:43:18 +0100749}
Thierry Redingedec4af2012-11-15 21:28:23 +0000750
Thierry Reding2ccb3962015-01-15 13:43:18 +0100751static void tegra_hdmi_disable_audio_infoframe(struct tegra_hdmi *hdmi)
752{
753 u32 value;
754
755 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_CTRL);
756 value &= ~INFOFRAME_CTRL_ENABLE;
757 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_CTRL);
758}
759
760static void tegra_hdmi_enable_audio_infoframe(struct tegra_hdmi *hdmi)
761{
762 u32 value;
763
764 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_CTRL);
765 value |= INFOFRAME_CTRL_ENABLE;
766 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_CTRL);
Thierry Redingedec4af2012-11-15 21:28:23 +0000767}
768
769static void tegra_hdmi_setup_stereo_infoframe(struct tegra_hdmi *hdmi)
770{
Lespiau, Damienae84b902013-08-19 16:59:02 +0100771 struct hdmi_vendor_infoframe frame;
Thierry Redingac24c222012-11-23 15:14:00 +0100772 u8 buffer[10];
773 ssize_t err;
Thierry Redingedec4af2012-11-15 21:28:23 +0000774
Lespiau, Damienae84b902013-08-19 16:59:02 +0100775 hdmi_vendor_infoframe_init(&frame);
Lespiau, Damiena26a58e82013-08-19 16:58:59 +0100776 frame.s3d_struct = HDMI_3D_STRUCTURE_FRAME_PACKING;
Thierry Redingac24c222012-11-23 15:14:00 +0100777
Lespiau, Damienae84b902013-08-19 16:59:02 +0100778 err = hdmi_vendor_infoframe_pack(&frame, buffer, sizeof(buffer));
Thierry Redingac24c222012-11-23 15:14:00 +0100779 if (err < 0) {
780 dev_err(hdmi->dev, "failed to pack vendor infoframe: %zd\n",
781 err);
782 return;
783 }
784
785 tegra_hdmi_write_infopack(hdmi, buffer, err);
Thierry Reding2ccb3962015-01-15 13:43:18 +0100786}
787
788static void tegra_hdmi_disable_stereo_infoframe(struct tegra_hdmi *hdmi)
789{
790 u32 value;
791
792 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
793 value &= ~GENERIC_CTRL_ENABLE;
794 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
795}
796
797static void tegra_hdmi_enable_stereo_infoframe(struct tegra_hdmi *hdmi)
798{
799 u32 value;
Thierry Redingedec4af2012-11-15 21:28:23 +0000800
801 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
802 value |= GENERIC_CTRL_ENABLE;
803 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_GENERIC_CTRL);
804}
805
806static void tegra_hdmi_setup_tmds(struct tegra_hdmi *hdmi,
807 const struct tmds_config *tmds)
808{
Thierry Reding4ee8cee2014-12-08 16:25:14 +0100809 u32 value;
Thierry Redingedec4af2012-11-15 21:28:23 +0000810
811 tegra_hdmi_writel(hdmi, tmds->pll0, HDMI_NV_PDISP_SOR_PLL0);
812 tegra_hdmi_writel(hdmi, tmds->pll1, HDMI_NV_PDISP_SOR_PLL1);
813 tegra_hdmi_writel(hdmi, tmds->pe_current, HDMI_NV_PDISP_PE_CURRENT);
814
Thierry Reding59af0592013-10-14 09:43:05 +0200815 tegra_hdmi_writel(hdmi, tmds->drive_current,
816 HDMI_NV_PDISP_SOR_LANE_DRIVE_CURRENT);
817
818 value = tegra_hdmi_readl(hdmi, hdmi->config->fuse_override_offset);
819 value |= hdmi->config->fuse_override_value;
820 tegra_hdmi_writel(hdmi, value, hdmi->config->fuse_override_offset);
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +0200821
822 if (hdmi->config->has_sor_io_peak_current)
823 tegra_hdmi_writel(hdmi, tmds->peak_current,
824 HDMI_NV_PDISP_SOR_IO_PEAK_CURRENT);
Thierry Redingedec4af2012-11-15 21:28:23 +0000825}
826
Mikko Perttunen9f159122013-08-28 18:48:38 +0300827static bool tegra_output_is_hdmi(struct tegra_output *output)
828{
829 struct edid *edid;
830
831 if (!output->connector.edid_blob_ptr)
832 return false;
833
834 edid = (struct edid *)output->connector.edid_blob_ptr->data;
835
836 return drm_detect_hdmi_monitor(edid);
837}
838
Thierry Reding2ccb3962015-01-15 13:43:18 +0100839static enum drm_connector_status
840tegra_hdmi_connector_detect(struct drm_connector *connector, bool force)
841{
842 struct tegra_output *output = connector_to_output(connector);
843 struct tegra_hdmi *hdmi = to_hdmi(output);
844 enum drm_connector_status status;
845
846 status = tegra_output_connector_detect(connector, force);
847 if (status == connector_status_connected)
848 return status;
849
850 tegra_hdmi_writel(hdmi, 0, HDMI_NV_PDISP_SOR_AUDIO_HDA_PRESENSE);
851 return status;
852}
853
Thierry Reding1d600472017-11-08 13:18:31 +0100854#define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
855
856static const struct debugfs_reg32 tegra_hdmi_regs[] = {
857 DEBUGFS_REG32(HDMI_CTXSW),
858 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_STATE0),
859 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_STATE1),
860 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_STATE2),
861 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_AN_MSB),
862 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_AN_LSB),
863 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_CN_MSB),
864 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_CN_LSB),
865 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_AKSV_MSB),
866 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_AKSV_LSB),
867 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_BKSV_MSB),
868 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_BKSV_LSB),
869 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_CKSV_MSB),
870 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_CKSV_LSB),
871 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_DKSV_MSB),
872 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_DKSV_LSB),
873 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_CTRL),
874 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_CMODE),
875 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_MPRIME_MSB),
876 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_MPRIME_LSB),
877 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_SPRIME_MSB),
878 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_SPRIME_LSB2),
879 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_SPRIME_LSB1),
880 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_RI),
881 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_CS_MSB),
882 DEBUGFS_REG32(HDMI_NV_PDISP_RG_HDCP_CS_LSB),
883 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AUDIO_EMU0),
884 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AUDIO_EMU_RDATA0),
885 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AUDIO_EMU1),
886 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AUDIO_EMU2),
887 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_CTRL),
888 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_STATUS),
889 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_HEADER),
890 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_SUBPACK0_LOW),
891 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_SUBPACK0_HIGH),
892 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_CTRL),
893 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_STATUS),
894 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_HEADER),
895 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_SUBPACK0_LOW),
896 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_SUBPACK0_HIGH),
897 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_SUBPACK1_LOW),
898 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_SUBPACK1_HIGH),
899 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_GENERIC_CTRL),
900 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_GENERIC_STATUS),
901 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_GENERIC_HEADER),
902 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK0_LOW),
903 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK0_HIGH),
904 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK1_LOW),
905 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK1_HIGH),
906 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK2_LOW),
907 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK2_HIGH),
908 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK3_LOW),
909 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_GENERIC_SUBPACK3_HIGH),
910 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_ACR_CTRL),
911 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_ACR_0320_SUBPACK_LOW),
912 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_ACR_0320_SUBPACK_HIGH),
913 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_ACR_0441_SUBPACK_LOW),
914 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_ACR_0441_SUBPACK_HIGH),
915 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_ACR_0882_SUBPACK_LOW),
916 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_ACR_0882_SUBPACK_HIGH),
917 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_ACR_1764_SUBPACK_LOW),
918 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_ACR_1764_SUBPACK_HIGH),
919 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_ACR_0480_SUBPACK_LOW),
920 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_ACR_0480_SUBPACK_HIGH),
921 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_ACR_0960_SUBPACK_LOW),
922 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_ACR_0960_SUBPACK_HIGH),
923 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_ACR_1920_SUBPACK_LOW),
924 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_ACR_1920_SUBPACK_HIGH),
925 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_CTRL),
926 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_VSYNC_KEEPOUT),
927 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_VSYNC_WINDOW),
928 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_GCP_CTRL),
929 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_GCP_STATUS),
930 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_GCP_SUBPACK),
931 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_CHANNEL_STATUS1),
932 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_CHANNEL_STATUS2),
933 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_EMU0),
934 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_EMU1),
935 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_EMU1_RDATA),
936 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_SPARE),
937 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_SPDIF_CHN_STATUS1),
938 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_SPDIF_CHN_STATUS2),
939 DEBUGFS_REG32(HDMI_NV_PDISP_HDMI_HDCPRIF_ROM_CTRL),
940 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_CAP),
941 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_PWR),
942 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_TEST),
943 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_PLL0),
944 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_PLL1),
945 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_PLL2),
946 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_CSTM),
947 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_LVDS),
948 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_CRCA),
949 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_CRCB),
950 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_BLANK),
951 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_CTL),
952 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(0)),
953 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(1)),
954 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(2)),
955 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(3)),
956 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(4)),
957 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(5)),
958 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(6)),
959 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(7)),
960 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(8)),
961 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(9)),
962 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(10)),
963 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(11)),
964 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(12)),
965 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(13)),
966 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(14)),
967 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_SEQ_INST(15)),
968 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_VCRCA0),
969 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_VCRCA1),
970 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_CCRCA0),
971 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_CCRCA1),
972 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_EDATAA0),
973 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_EDATAA1),
974 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_COUNTA0),
975 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_COUNTA1),
976 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_DEBUGA0),
977 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_DEBUGA1),
978 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_TRIG),
979 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_MSCHECK),
980 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_LANE_DRIVE_CURRENT),
981 DEBUGFS_REG32(HDMI_NV_PDISP_AUDIO_DEBUG0),
982 DEBUGFS_REG32(HDMI_NV_PDISP_AUDIO_DEBUG1),
983 DEBUGFS_REG32(HDMI_NV_PDISP_AUDIO_DEBUG2),
984 DEBUGFS_REG32(HDMI_NV_PDISP_AUDIO_FS(0)),
985 DEBUGFS_REG32(HDMI_NV_PDISP_AUDIO_FS(1)),
986 DEBUGFS_REG32(HDMI_NV_PDISP_AUDIO_FS(2)),
987 DEBUGFS_REG32(HDMI_NV_PDISP_AUDIO_FS(3)),
988 DEBUGFS_REG32(HDMI_NV_PDISP_AUDIO_FS(4)),
989 DEBUGFS_REG32(HDMI_NV_PDISP_AUDIO_FS(5)),
990 DEBUGFS_REG32(HDMI_NV_PDISP_AUDIO_FS(6)),
991 DEBUGFS_REG32(HDMI_NV_PDISP_AUDIO_PULSE_WIDTH),
992 DEBUGFS_REG32(HDMI_NV_PDISP_AUDIO_THRESHOLD),
993 DEBUGFS_REG32(HDMI_NV_PDISP_AUDIO_CNTRL0),
994 DEBUGFS_REG32(HDMI_NV_PDISP_AUDIO_N),
995 DEBUGFS_REG32(HDMI_NV_PDISP_HDCPRIF_ROM_TIMING),
996 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_REFCLK),
997 DEBUGFS_REG32(HDMI_NV_PDISP_CRC_CONTROL),
998 DEBUGFS_REG32(HDMI_NV_PDISP_INPUT_CONTROL),
999 DEBUGFS_REG32(HDMI_NV_PDISP_SCRATCH),
1000 DEBUGFS_REG32(HDMI_NV_PDISP_PE_CURRENT),
1001 DEBUGFS_REG32(HDMI_NV_PDISP_KEY_CTRL),
1002 DEBUGFS_REG32(HDMI_NV_PDISP_KEY_DEBUG0),
1003 DEBUGFS_REG32(HDMI_NV_PDISP_KEY_DEBUG1),
1004 DEBUGFS_REG32(HDMI_NV_PDISP_KEY_DEBUG2),
1005 DEBUGFS_REG32(HDMI_NV_PDISP_KEY_HDCP_KEY_0),
1006 DEBUGFS_REG32(HDMI_NV_PDISP_KEY_HDCP_KEY_1),
1007 DEBUGFS_REG32(HDMI_NV_PDISP_KEY_HDCP_KEY_2),
1008 DEBUGFS_REG32(HDMI_NV_PDISP_KEY_HDCP_KEY_3),
1009 DEBUGFS_REG32(HDMI_NV_PDISP_KEY_HDCP_KEY_TRIG),
1010 DEBUGFS_REG32(HDMI_NV_PDISP_KEY_SKEY_INDEX),
1011 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_AUDIO_CNTRL0),
1012 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_AUDIO_SPARE0),
1013 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_AUDIO_HDA_CODEC_SCRATCH0),
1014 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_AUDIO_HDA_CODEC_SCRATCH1),
1015 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_AUDIO_HDA_ELD_BUFWR),
1016 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_AUDIO_HDA_PRESENSE),
1017 DEBUGFS_REG32(HDMI_NV_PDISP_INT_STATUS),
1018 DEBUGFS_REG32(HDMI_NV_PDISP_INT_MASK),
1019 DEBUGFS_REG32(HDMI_NV_PDISP_INT_ENABLE),
1020 DEBUGFS_REG32(HDMI_NV_PDISP_SOR_IO_PEAK_CURRENT),
1021};
1022
1023static int tegra_hdmi_show_regs(struct seq_file *s, void *data)
1024{
1025 struct drm_info_node *node = s->private;
1026 struct tegra_hdmi *hdmi = node->info_ent->data;
1027 struct drm_crtc *crtc = hdmi->output.encoder.crtc;
1028 struct drm_device *drm = node->minor->dev;
1029 unsigned int i;
1030 int err = 0;
1031
1032 drm_modeset_lock_all(drm);
1033
1034 if (!crtc || !crtc->state->active) {
1035 err = -EBUSY;
1036 goto unlock;
1037 }
1038
1039 for (i = 0; i < ARRAY_SIZE(tegra_hdmi_regs); i++) {
1040 unsigned int offset = tegra_hdmi_regs[i].offset;
1041
1042 seq_printf(s, "%-56s %#05x %08x\n", tegra_hdmi_regs[i].name,
1043 offset, tegra_hdmi_readl(hdmi, offset));
1044 }
1045
1046unlock:
1047 drm_modeset_unlock_all(drm);
1048 return err;
1049}
1050
1051static struct drm_info_list debugfs_files[] = {
1052 { "regs", tegra_hdmi_show_regs, 0, NULL },
1053};
1054
1055static int tegra_hdmi_late_register(struct drm_connector *connector)
1056{
1057 struct tegra_output *output = connector_to_output(connector);
1058 unsigned int i, count = ARRAY_SIZE(debugfs_files);
1059 struct drm_minor *minor = connector->dev->primary;
1060 struct dentry *root = connector->debugfs_entry;
1061 struct tegra_hdmi *hdmi = to_hdmi(output);
1062 int err;
1063
1064 hdmi->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1065 GFP_KERNEL);
1066 if (!hdmi->debugfs_files)
1067 return -ENOMEM;
1068
1069 for (i = 0; i < count; i++)
1070 hdmi->debugfs_files[i].data = hdmi;
1071
1072 err = drm_debugfs_create_files(hdmi->debugfs_files, count, root, minor);
1073 if (err < 0)
1074 goto free;
1075
1076 return 0;
1077
1078free:
1079 kfree(hdmi->debugfs_files);
1080 hdmi->debugfs_files = NULL;
1081
1082 return err;
1083}
1084
1085static void tegra_hdmi_early_unregister(struct drm_connector *connector)
1086{
1087 struct tegra_output *output = connector_to_output(connector);
1088 struct drm_minor *minor = connector->dev->primary;
1089 unsigned int count = ARRAY_SIZE(debugfs_files);
1090 struct tegra_hdmi *hdmi = to_hdmi(output);
1091
1092 drm_debugfs_remove_files(hdmi->debugfs_files, count, minor);
1093 kfree(hdmi->debugfs_files);
1094 hdmi->debugfs_files = NULL;
1095}
1096
Thierry Reding59682712014-11-28 16:50:59 +01001097static const struct drm_connector_funcs tegra_hdmi_connector_funcs = {
Thierry Reding9d441892014-11-24 17:02:53 +01001098 .reset = drm_atomic_helper_connector_reset,
Thierry Reding2ccb3962015-01-15 13:43:18 +01001099 .detect = tegra_hdmi_connector_detect,
Thierry Reding59682712014-11-28 16:50:59 +01001100 .fill_modes = drm_helper_probe_single_connector_modes,
1101 .destroy = tegra_output_connector_destroy,
Thierry Reding9d441892014-11-24 17:02:53 +01001102 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
Thierry Reding4aa3df72014-11-24 16:27:13 +01001103 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Thierry Reding1d600472017-11-08 13:18:31 +01001104 .late_register = tegra_hdmi_late_register,
1105 .early_unregister = tegra_hdmi_early_unregister,
Thierry Reding59682712014-11-28 16:50:59 +01001106};
1107
1108static enum drm_mode_status
1109tegra_hdmi_connector_mode_valid(struct drm_connector *connector,
1110 struct drm_display_mode *mode)
1111{
1112 struct tegra_output *output = connector_to_output(connector);
1113 struct tegra_hdmi *hdmi = to_hdmi(output);
1114 unsigned long pclk = mode->clock * 1000;
1115 enum drm_mode_status status = MODE_OK;
1116 struct clk *parent;
1117 long err;
1118
1119 parent = clk_get_parent(hdmi->clk_parent);
1120
1121 err = clk_round_rate(parent, pclk * 4);
1122 if (err <= 0)
1123 status = MODE_NOCLOCK;
1124
1125 return status;
1126}
1127
1128static const struct drm_connector_helper_funcs
1129tegra_hdmi_connector_helper_funcs = {
1130 .get_modes = tegra_output_connector_get_modes,
1131 .mode_valid = tegra_hdmi_connector_mode_valid,
Thierry Reding59682712014-11-28 16:50:59 +01001132};
1133
1134static const struct drm_encoder_funcs tegra_hdmi_encoder_funcs = {
1135 .destroy = tegra_output_encoder_destroy,
1136};
1137
Thierry Reding29871b22015-07-29 09:46:40 +02001138static void tegra_hdmi_encoder_disable(struct drm_encoder *encoder)
Thierry Reding59682712014-11-28 16:50:59 +01001139{
Thierry Reding2ccb3962015-01-15 13:43:18 +01001140 struct tegra_output *output = encoder_to_output(encoder);
Thierry Reding29871b22015-07-29 09:46:40 +02001141 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
Thierry Reding2ccb3962015-01-15 13:43:18 +01001142 struct tegra_hdmi *hdmi = to_hdmi(output);
Thierry Reding29871b22015-07-29 09:46:40 +02001143 u32 value;
1144
1145 /*
1146 * The following accesses registers of the display controller, so make
1147 * sure it's only executed when the output is attached to one.
1148 */
1149 if (dc) {
1150 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1151 value &= ~HDMI_ENABLE;
1152 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1153
1154 tegra_dc_commit(dc);
1155 }
Thierry Reding2ccb3962015-01-15 13:43:18 +01001156
1157 if (!hdmi->dvi) {
1158 if (hdmi->stereo)
1159 tegra_hdmi_disable_stereo_infoframe(hdmi);
1160
1161 tegra_hdmi_disable_audio_infoframe(hdmi);
1162 tegra_hdmi_disable_avi_infoframe(hdmi);
1163 tegra_hdmi_disable_audio(hdmi);
1164 }
Thierry Reding2ccb3962015-01-15 13:43:18 +01001165
Thierry Reding52345492015-08-07 16:00:43 +02001166 tegra_hdmi_writel(hdmi, 0, HDMI_NV_PDISP_INT_ENABLE);
1167 tegra_hdmi_writel(hdmi, 0, HDMI_NV_PDISP_INT_MASK);
Thierry Reding2ccb3962015-01-15 13:43:18 +01001168
Thierry Reding52345492015-08-07 16:00:43 +02001169 pm_runtime_put(hdmi->dev);
Thierry Reding59682712014-11-28 16:50:59 +01001170}
1171
Thierry Reding29871b22015-07-29 09:46:40 +02001172static void tegra_hdmi_encoder_enable(struct drm_encoder *encoder)
Thierry Reding59682712014-11-28 16:50:59 +01001173{
Thierry Reding29871b22015-07-29 09:46:40 +02001174 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
Thierry Redingedec4af2012-11-15 21:28:23 +00001175 unsigned int h_sync_width, h_front_porch, h_back_porch, i, rekey;
Thierry Reding59682712014-11-28 16:50:59 +01001176 struct tegra_output *output = encoder_to_output(encoder);
1177 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
Thierry Redingedec4af2012-11-15 21:28:23 +00001178 struct tegra_hdmi *hdmi = to_hdmi(output);
Thierry Reding2ccb3962015-01-15 13:43:18 +01001179 unsigned int pulse_start, div82;
Thierry Redingedec4af2012-11-15 21:28:23 +00001180 int retries = 1000;
Thierry Reding4ee8cee2014-12-08 16:25:14 +01001181 u32 value;
Thierry Redingedec4af2012-11-15 21:28:23 +00001182 int err;
1183
Thierry Reding52345492015-08-07 16:00:43 +02001184 pm_runtime_get_sync(hdmi->dev);
Mikko Perttunen9f159122013-08-28 18:48:38 +03001185
Thierry Reding52345492015-08-07 16:00:43 +02001186 /*
1187 * Enable and unmask the HDA codec SCRATCH0 register interrupt. This
1188 * is used for interoperability between the HDA codec driver and the
1189 * HDMI driver.
1190 */
1191 tegra_hdmi_writel(hdmi, INT_CODEC_SCRATCH0, HDMI_NV_PDISP_INT_ENABLE);
1192 tegra_hdmi_writel(hdmi, INT_CODEC_SCRATCH0, HDMI_NV_PDISP_INT_MASK);
1193
Thierry Reding2ccb3962015-01-15 13:43:18 +01001194 hdmi->pixel_clock = mode->clock * 1000;
Thierry Redingedec4af2012-11-15 21:28:23 +00001195 h_sync_width = mode->hsync_end - mode->hsync_start;
Lucas Stach40495082012-12-19 21:38:52 +00001196 h_back_porch = mode->htotal - mode->hsync_end;
1197 h_front_porch = mode->hsync_start - mode->hdisplay;
Thierry Redingedec4af2012-11-15 21:28:23 +00001198
Thierry Reding2ccb3962015-01-15 13:43:18 +01001199 err = clk_set_rate(hdmi->clk, hdmi->pixel_clock);
Thierry Redingc03bf1bf2015-02-18 10:34:08 +01001200 if (err < 0) {
1201 dev_err(hdmi->dev, "failed to set HDMI clock frequency: %d\n",
1202 err);
1203 }
1204
1205 DRM_DEBUG_KMS("HDMI clock rate: %lu Hz\n", clk_get_rate(hdmi->clk));
1206
Thierry Reding8c8282c2014-04-16 10:46:24 +02001207 /* power up sequence */
1208 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_SOR_PLL0);
1209 value &= ~SOR_PLL_PDBG;
1210 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_PLL0);
1211
1212 usleep_range(10, 20);
1213
1214 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_SOR_PLL0);
1215 value &= ~SOR_PLL_PWR;
1216 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_PLL0);
1217
Thierry Redingedec4af2012-11-15 21:28:23 +00001218 tegra_dc_writel(dc, VSYNC_H_POSITION(1),
1219 DC_DISP_DISP_TIMING_OPTIONS);
Thierry Reding472a6d12015-08-05 16:39:55 +02001220 tegra_dc_writel(dc, DITHER_CONTROL_DISABLE | BASE_COLOR_SIZE_888,
Thierry Redingedec4af2012-11-15 21:28:23 +00001221 DC_DISP_DISP_COLOR_CONTROL);
1222
1223 /* video_preamble uses h_pulse2 */
1224 pulse_start = 1 + h_sync_width + h_back_porch - 10;
1225
Thierry Reding8fd3ffa2015-04-27 14:48:35 +02001226 tegra_dc_writel(dc, H_PULSE2_ENABLE, DC_DISP_DISP_SIGNAL_OPTIONS0);
Thierry Redingedec4af2012-11-15 21:28:23 +00001227
1228 value = PULSE_MODE_NORMAL | PULSE_POLARITY_HIGH | PULSE_QUAL_VACTIVE |
1229 PULSE_LAST_END_A;
1230 tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_CONTROL);
1231
1232 value = PULSE_START(pulse_start) | PULSE_END(pulse_start + 8);
1233 tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_POSITION_A);
1234
1235 value = VSYNC_WINDOW_END(0x210) | VSYNC_WINDOW_START(0x200) |
1236 VSYNC_WINDOW_ENABLE;
1237 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_VSYNC_WINDOW);
1238
1239 if (dc->pipe)
1240 value = HDMI_SRC_DISPLAYB;
1241 else
1242 value = HDMI_SRC_DISPLAYA;
1243
1244 if ((mode->hdisplay == 720) && ((mode->vdisplay == 480) ||
1245 (mode->vdisplay == 576)))
1246 tegra_hdmi_writel(hdmi,
1247 value | ARM_VIDEO_RANGE_FULL,
1248 HDMI_NV_PDISP_INPUT_CONTROL);
1249 else
1250 tegra_hdmi_writel(hdmi,
1251 value | ARM_VIDEO_RANGE_LIMITED,
1252 HDMI_NV_PDISP_INPUT_CONTROL);
1253
1254 div82 = clk_get_rate(hdmi->clk) / 1000000 * 4;
1255 value = SOR_REFCLK_DIV_INT(div82 >> 2) | SOR_REFCLK_DIV_FRAC(div82);
1256 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_REFCLK);
1257
Thierry Reding2ccb3962015-01-15 13:43:18 +01001258 hdmi->dvi = !tegra_output_is_hdmi(output);
Thierry Redingedec4af2012-11-15 21:28:23 +00001259 if (!hdmi->dvi) {
Thierry Reding83f8bf42019-04-16 14:43:26 +02001260 /*
1261 * Make sure that the audio format has been configured before
1262 * enabling audio, otherwise we may try to divide by zero.
1263 */
1264 if (hdmi->format.sample_rate > 0) {
1265 err = tegra_hdmi_setup_audio(hdmi);
1266 if (err < 0)
1267 hdmi->dvi = true;
1268 }
Thierry Redingedec4af2012-11-15 21:28:23 +00001269 }
1270
Thierry Reding2ccb3962015-01-15 13:43:18 +01001271 if (hdmi->config->has_hda)
1272 tegra_hdmi_write_eld(hdmi);
Thierry Redingedec4af2012-11-15 21:28:23 +00001273
1274 rekey = HDMI_REKEY_DEFAULT;
1275 value = HDMI_CTRL_REKEY(rekey);
1276 value |= HDMI_CTRL_MAX_AC_PACKET((h_sync_width + h_back_porch +
1277 h_front_porch - rekey - 18) / 32);
1278
1279 if (!hdmi->dvi)
1280 value |= HDMI_CTRL_ENABLE;
1281
1282 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_HDMI_CTRL);
1283
Thierry Reding2ccb3962015-01-15 13:43:18 +01001284 if (!hdmi->dvi) {
1285 tegra_hdmi_setup_avi_infoframe(hdmi, mode);
1286 tegra_hdmi_setup_audio_infoframe(hdmi);
Thierry Redingedec4af2012-11-15 21:28:23 +00001287
Thierry Reding2ccb3962015-01-15 13:43:18 +01001288 if (hdmi->stereo)
1289 tegra_hdmi_setup_stereo_infoframe(hdmi);
1290 }
Thierry Redingedec4af2012-11-15 21:28:23 +00001291
1292 /* TMDS CONFIG */
Thierry Reding59af0592013-10-14 09:43:05 +02001293 for (i = 0; i < hdmi->config->num_tmds; i++) {
Thierry Reding2ccb3962015-01-15 13:43:18 +01001294 if (hdmi->pixel_clock <= hdmi->config->tmds[i].pclk) {
Thierry Reding59af0592013-10-14 09:43:05 +02001295 tegra_hdmi_setup_tmds(hdmi, &hdmi->config->tmds[i]);
Thierry Redingedec4af2012-11-15 21:28:23 +00001296 break;
1297 }
1298 }
1299
1300 tegra_hdmi_writel(hdmi,
Thierry Reding5c1c0712015-01-28 16:32:52 +01001301 SOR_SEQ_PU_PC(0) |
Thierry Redingedec4af2012-11-15 21:28:23 +00001302 SOR_SEQ_PU_PC_ALT(0) |
1303 SOR_SEQ_PD_PC(8) |
1304 SOR_SEQ_PD_PC_ALT(8),
1305 HDMI_NV_PDISP_SOR_SEQ_CTL);
1306
1307 value = SOR_SEQ_INST_WAIT_TIME(1) |
1308 SOR_SEQ_INST_WAIT_UNITS_VSYNC |
1309 SOR_SEQ_INST_HALT |
1310 SOR_SEQ_INST_PIN_A_LOW |
1311 SOR_SEQ_INST_PIN_B_LOW |
1312 SOR_SEQ_INST_DRIVE_PWM_OUT_LO;
1313
1314 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_SEQ_INST(0));
1315 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_SEQ_INST(8));
1316
Thierry Reding9cbfc732014-04-16 10:47:36 +02001317 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_SOR_CSTM);
Thierry Redingedec4af2012-11-15 21:28:23 +00001318 value &= ~SOR_CSTM_ROTCLK(~0);
1319 value |= SOR_CSTM_ROTCLK(2);
Thierry Reding9cbfc732014-04-16 10:47:36 +02001320 value |= SOR_CSTM_PLLDIV;
1321 value &= ~SOR_CSTM_LVDS_ENABLE;
1322 value &= ~SOR_CSTM_MODE_MASK;
1323 value |= SOR_CSTM_MODE_TMDS;
Thierry Redingedec4af2012-11-15 21:28:23 +00001324 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_CSTM);
1325
Thierry Redingedec4af2012-11-15 21:28:23 +00001326 /* start SOR */
1327 tegra_hdmi_writel(hdmi,
1328 SOR_PWR_NORMAL_STATE_PU |
1329 SOR_PWR_NORMAL_START_NORMAL |
1330 SOR_PWR_SAFE_STATE_PD |
1331 SOR_PWR_SETTING_NEW_TRIGGER,
1332 HDMI_NV_PDISP_SOR_PWR);
1333 tegra_hdmi_writel(hdmi,
1334 SOR_PWR_NORMAL_STATE_PU |
1335 SOR_PWR_NORMAL_START_NORMAL |
1336 SOR_PWR_SAFE_STATE_PD |
1337 SOR_PWR_SETTING_NEW_DONE,
1338 HDMI_NV_PDISP_SOR_PWR);
1339
1340 do {
1341 BUG_ON(--retries < 0);
1342 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_SOR_PWR);
1343 } while (value & SOR_PWR_SETTING_NEW_PENDING);
1344
1345 value = SOR_STATE_ASY_CRCMODE_COMPLETE |
1346 SOR_STATE_ASY_OWNER_HEAD0 |
1347 SOR_STATE_ASY_SUBOWNER_BOTH |
1348 SOR_STATE_ASY_PROTOCOL_SINGLE_TMDS_A |
1349 SOR_STATE_ASY_DEPOL_POS;
1350
1351 /* setup sync polarities */
1352 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
1353 value |= SOR_STATE_ASY_HSYNCPOL_POS;
1354
1355 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1356 value |= SOR_STATE_ASY_HSYNCPOL_NEG;
1357
1358 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
1359 value |= SOR_STATE_ASY_VSYNCPOL_POS;
1360
1361 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1362 value |= SOR_STATE_ASY_VSYNCPOL_NEG;
1363
1364 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_STATE2);
1365
1366 value = SOR_STATE_ASY_HEAD_OPMODE_AWAKE | SOR_STATE_ASY_ORMODE_NORMAL;
1367 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_STATE1);
1368
1369 tegra_hdmi_writel(hdmi, 0, HDMI_NV_PDISP_SOR_STATE0);
1370 tegra_hdmi_writel(hdmi, SOR_STATE_UPDATE, HDMI_NV_PDISP_SOR_STATE0);
1371 tegra_hdmi_writel(hdmi, value | SOR_STATE_ATTACHED,
1372 HDMI_NV_PDISP_SOR_STATE1);
1373 tegra_hdmi_writel(hdmi, 0, HDMI_NV_PDISP_SOR_STATE0);
1374
Thierry Reding72d30282013-12-12 11:06:55 +01001375 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1376 value |= HDMI_ENABLE;
1377 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
Thierry Redingedec4af2012-11-15 21:28:23 +00001378
Thierry Reding62b9e062014-11-21 17:33:33 +01001379 tegra_dc_commit(dc);
Thierry Redingedec4af2012-11-15 21:28:23 +00001380
Thierry Reding2ccb3962015-01-15 13:43:18 +01001381 if (!hdmi->dvi) {
1382 tegra_hdmi_enable_avi_infoframe(hdmi);
1383 tegra_hdmi_enable_audio_infoframe(hdmi);
1384 tegra_hdmi_enable_audio(hdmi);
1385
1386 if (hdmi->stereo)
1387 tegra_hdmi_enable_stereo_infoframe(hdmi);
1388 }
1389
Thierry Redingedec4af2012-11-15 21:28:23 +00001390 /* TODO: add HDCP support */
Thierry Redingedec4af2012-11-15 21:28:23 +00001391}
1392
Thierry Redinga9825a62014-12-08 16:33:03 +01001393static int
1394tegra_hdmi_encoder_atomic_check(struct drm_encoder *encoder,
1395 struct drm_crtc_state *crtc_state,
1396 struct drm_connector_state *conn_state)
1397{
1398 struct tegra_output *output = encoder_to_output(encoder);
1399 struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
1400 unsigned long pclk = crtc_state->mode.clock * 1000;
1401 struct tegra_hdmi *hdmi = to_hdmi(output);
1402 int err;
1403
1404 err = tegra_dc_state_setup_clock(dc, crtc_state, hdmi->clk_parent,
1405 pclk, 0);
1406 if (err < 0) {
1407 dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
1408 return err;
1409 }
1410
1411 return err;
1412}
1413
Thierry Reding59682712014-11-28 16:50:59 +01001414static const struct drm_encoder_helper_funcs tegra_hdmi_encoder_helper_funcs = {
Thierry Reding59682712014-11-28 16:50:59 +01001415 .disable = tegra_hdmi_encoder_disable,
Thierry Reding29871b22015-07-29 09:46:40 +02001416 .enable = tegra_hdmi_encoder_enable,
Thierry Redinga9825a62014-12-08 16:33:03 +01001417 .atomic_check = tegra_hdmi_encoder_atomic_check,
Thierry Redingedec4af2012-11-15 21:28:23 +00001418};
1419
Thierry Reding53fa7f72013-09-24 15:35:40 +02001420static int tegra_hdmi_init(struct host1x_client *client)
Thierry Redingedec4af2012-11-15 21:28:23 +00001421{
Thierry Reding9910f5c2014-05-22 09:57:15 +02001422 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Reding776dc382013-10-14 14:43:22 +02001423 struct tegra_hdmi *hdmi = host1x_client_to_hdmi(client);
Thierry Redingedec4af2012-11-15 21:28:23 +00001424 int err;
1425
Thierry Redingedec4af2012-11-15 21:28:23 +00001426 hdmi->output.dev = client->dev;
Thierry Redingedec4af2012-11-15 21:28:23 +00001427
Thierry Reding59682712014-11-28 16:50:59 +01001428 drm_connector_init(drm, &hdmi->output.connector,
1429 &tegra_hdmi_connector_funcs,
1430 DRM_MODE_CONNECTOR_HDMIA);
1431 drm_connector_helper_add(&hdmi->output.connector,
1432 &tegra_hdmi_connector_helper_funcs);
1433 hdmi->output.connector.dpms = DRM_MODE_DPMS_OFF;
1434
1435 drm_encoder_init(drm, &hdmi->output.encoder, &tegra_hdmi_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001436 DRM_MODE_ENCODER_TMDS, NULL);
Thierry Reding59682712014-11-28 16:50:59 +01001437 drm_encoder_helper_add(&hdmi->output.encoder,
1438 &tegra_hdmi_encoder_helper_funcs);
1439
Daniel Vettercde4c442018-07-09 10:40:07 +02001440 drm_connector_attach_encoder(&hdmi->output.connector,
Thierry Reding59682712014-11-28 16:50:59 +01001441 &hdmi->output.encoder);
1442 drm_connector_register(&hdmi->output.connector);
1443
Thierry Redingea130b22014-12-19 15:51:35 +01001444 err = tegra_output_init(drm, &hdmi->output);
1445 if (err < 0) {
1446 dev_err(client->dev, "failed to initialize output: %d\n", err);
1447 return err;
1448 }
Thierry Reding59682712014-11-28 16:50:59 +01001449
Thierry Redingea130b22014-12-19 15:51:35 +01001450 hdmi->output.encoder.possible_crtcs = 0x3;
Thierry Redingedec4af2012-11-15 21:28:23 +00001451
Thierry Redingfb50a112014-02-28 16:57:34 +01001452 err = regulator_enable(hdmi->hdmi);
1453 if (err < 0) {
1454 dev_err(client->dev, "failed to enable HDMI regulator: %d\n",
1455 err);
1456 return err;
1457 }
1458
Thierry Reding59682712014-11-28 16:50:59 +01001459 err = regulator_enable(hdmi->pll);
1460 if (err < 0) {
1461 dev_err(hdmi->dev, "failed to enable PLL regulator: %d\n", err);
1462 return err;
1463 }
1464
1465 err = regulator_enable(hdmi->vdd);
1466 if (err < 0) {
1467 dev_err(hdmi->dev, "failed to enable VDD regulator: %d\n", err);
1468 return err;
1469 }
1470
Thierry Redingedec4af2012-11-15 21:28:23 +00001471 return 0;
1472}
1473
Thierry Reding53fa7f72013-09-24 15:35:40 +02001474static int tegra_hdmi_exit(struct host1x_client *client)
Thierry Redingedec4af2012-11-15 21:28:23 +00001475{
Thierry Reding776dc382013-10-14 14:43:22 +02001476 struct tegra_hdmi *hdmi = host1x_client_to_hdmi(client);
Thierry Redingedec4af2012-11-15 21:28:23 +00001477
Thierry Reding59682712014-11-28 16:50:59 +01001478 tegra_output_exit(&hdmi->output);
1479
Thierry Reding59682712014-11-28 16:50:59 +01001480 regulator_disable(hdmi->vdd);
1481 regulator_disable(hdmi->pll);
Thierry Redingfb50a112014-02-28 16:57:34 +01001482 regulator_disable(hdmi->hdmi);
1483
Thierry Redingedec4af2012-11-15 21:28:23 +00001484 return 0;
1485}
1486
1487static const struct host1x_client_ops hdmi_client_ops = {
Thierry Reding53fa7f72013-09-24 15:35:40 +02001488 .init = tegra_hdmi_init,
1489 .exit = tegra_hdmi_exit,
Thierry Redingedec4af2012-11-15 21:28:23 +00001490};
1491
Thierry Reding59af0592013-10-14 09:43:05 +02001492static const struct tegra_hdmi_config tegra20_hdmi_config = {
1493 .tmds = tegra20_tmds_config,
1494 .num_tmds = ARRAY_SIZE(tegra20_tmds_config),
1495 .fuse_override_offset = HDMI_NV_PDISP_SOR_LANE_DRIVE_CURRENT,
1496 .fuse_override_value = 1 << 31,
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +02001497 .has_sor_io_peak_current = false,
Thierry Reding2ccb3962015-01-15 13:43:18 +01001498 .has_hda = false,
1499 .has_hbr = false,
Thierry Reding59af0592013-10-14 09:43:05 +02001500};
1501
1502static const struct tegra_hdmi_config tegra30_hdmi_config = {
1503 .tmds = tegra30_tmds_config,
1504 .num_tmds = ARRAY_SIZE(tegra30_tmds_config),
1505 .fuse_override_offset = HDMI_NV_PDISP_SOR_LANE_DRIVE_CURRENT,
1506 .fuse_override_value = 1 << 31,
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +02001507 .has_sor_io_peak_current = false,
Thierry Reding2ccb3962015-01-15 13:43:18 +01001508 .has_hda = true,
1509 .has_hbr = false,
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +02001510};
1511
1512static const struct tegra_hdmi_config tegra114_hdmi_config = {
1513 .tmds = tegra114_tmds_config,
1514 .num_tmds = ARRAY_SIZE(tegra114_tmds_config),
1515 .fuse_override_offset = HDMI_NV_PDISP_SOR_PAD_CTLS0,
1516 .fuse_override_value = 1 << 31,
1517 .has_sor_io_peak_current = true,
Thierry Reding2ccb3962015-01-15 13:43:18 +01001518 .has_hda = true,
1519 .has_hbr = true,
Thierry Reding59af0592013-10-14 09:43:05 +02001520};
1521
Thierry Redingfb7be702013-11-15 16:07:32 +01001522static const struct tegra_hdmi_config tegra124_hdmi_config = {
1523 .tmds = tegra124_tmds_config,
1524 .num_tmds = ARRAY_SIZE(tegra124_tmds_config),
1525 .fuse_override_offset = HDMI_NV_PDISP_SOR_PAD_CTLS0,
1526 .fuse_override_value = 1 << 31,
1527 .has_sor_io_peak_current = true,
Thierry Reding2ccb3962015-01-15 13:43:18 +01001528 .has_hda = true,
1529 .has_hbr = true,
Thierry Redingfb7be702013-11-15 16:07:32 +01001530};
1531
Thierry Reding59af0592013-10-14 09:43:05 +02001532static const struct of_device_id tegra_hdmi_of_match[] = {
Thierry Redingfb7be702013-11-15 16:07:32 +01001533 { .compatible = "nvidia,tegra124-hdmi", .data = &tegra124_hdmi_config },
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +02001534 { .compatible = "nvidia,tegra114-hdmi", .data = &tegra114_hdmi_config },
Thierry Reding59af0592013-10-14 09:43:05 +02001535 { .compatible = "nvidia,tegra30-hdmi", .data = &tegra30_hdmi_config },
1536 { .compatible = "nvidia,tegra20-hdmi", .data = &tegra20_hdmi_config },
1537 { },
1538};
Stephen Warrenef707282014-06-18 16:21:55 -06001539MODULE_DEVICE_TABLE(of, tegra_hdmi_of_match);
Thierry Reding59af0592013-10-14 09:43:05 +02001540
Thierry Reding2ccb3962015-01-15 13:43:18 +01001541static irqreturn_t tegra_hdmi_irq(int irq, void *data)
1542{
1543 struct tegra_hdmi *hdmi = data;
1544 u32 value;
1545 int err;
1546
1547 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_INT_STATUS);
1548 tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_INT_STATUS);
1549
1550 if (value & INT_CODEC_SCRATCH0) {
1551 unsigned int format;
1552 u32 value;
1553
1554 value = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_SOR_AUDIO_HDA_CODEC_SCRATCH0);
1555
1556 if (value & SOR_AUDIO_HDA_CODEC_SCRATCH0_VALID) {
Thierry Reding2ccb3962015-01-15 13:43:18 +01001557 format = value & SOR_AUDIO_HDA_CODEC_SCRATCH0_FMT_MASK;
1558
Thierry Redinge3c702d2019-01-03 15:23:17 +01001559 tegra_hda_parse_format(format, &hdmi->format);
Thierry Reding2ccb3962015-01-15 13:43:18 +01001560
1561 err = tegra_hdmi_setup_audio(hdmi);
1562 if (err < 0) {
1563 tegra_hdmi_disable_audio_infoframe(hdmi);
1564 tegra_hdmi_disable_audio(hdmi);
1565 } else {
1566 tegra_hdmi_setup_audio_infoframe(hdmi);
1567 tegra_hdmi_enable_audio_infoframe(hdmi);
1568 tegra_hdmi_enable_audio(hdmi);
1569 }
1570 } else {
1571 tegra_hdmi_disable_audio_infoframe(hdmi);
1572 tegra_hdmi_disable_audio(hdmi);
1573 }
1574 }
1575
1576 return IRQ_HANDLED;
1577}
1578
Thierry Redingedec4af2012-11-15 21:28:23 +00001579static int tegra_hdmi_probe(struct platform_device *pdev)
1580{
Thierry Redingedec4af2012-11-15 21:28:23 +00001581 struct tegra_hdmi *hdmi;
1582 struct resource *regs;
1583 int err;
1584
1585 hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
1586 if (!hdmi)
1587 return -ENOMEM;
1588
Thierry Reding5e4acd32017-08-21 18:05:10 +02001589 hdmi->config = of_device_get_match_data(&pdev->dev);
Thierry Redingedec4af2012-11-15 21:28:23 +00001590 hdmi->dev = &pdev->dev;
Thierry Reding2ccb3962015-01-15 13:43:18 +01001591
Thierry Redingedec4af2012-11-15 21:28:23 +00001592 hdmi->audio_source = AUTO;
Thierry Redingedec4af2012-11-15 21:28:23 +00001593 hdmi->stereo = false;
1594 hdmi->dvi = false;
1595
1596 hdmi->clk = devm_clk_get(&pdev->dev, NULL);
1597 if (IS_ERR(hdmi->clk)) {
1598 dev_err(&pdev->dev, "failed to get clock\n");
1599 return PTR_ERR(hdmi->clk);
1600 }
1601
Stephen Warrenca480802013-11-06 16:20:54 -07001602 hdmi->rst = devm_reset_control_get(&pdev->dev, "hdmi");
1603 if (IS_ERR(hdmi->rst)) {
1604 dev_err(&pdev->dev, "failed to get reset\n");
1605 return PTR_ERR(hdmi->rst);
1606 }
1607
Thierry Redingedec4af2012-11-15 21:28:23 +00001608 hdmi->clk_parent = devm_clk_get(&pdev->dev, "parent");
1609 if (IS_ERR(hdmi->clk_parent))
1610 return PTR_ERR(hdmi->clk_parent);
1611
Thierry Redingedec4af2012-11-15 21:28:23 +00001612 err = clk_set_parent(hdmi->clk, hdmi->clk_parent);
1613 if (err < 0) {
1614 dev_err(&pdev->dev, "failed to setup clocks: %d\n", err);
1615 return err;
1616 }
1617
Thierry Redingfb50a112014-02-28 16:57:34 +01001618 hdmi->hdmi = devm_regulator_get(&pdev->dev, "hdmi");
1619 if (IS_ERR(hdmi->hdmi)) {
1620 dev_err(&pdev->dev, "failed to get HDMI regulator\n");
1621 return PTR_ERR(hdmi->hdmi);
1622 }
1623
Thierry Redingedec4af2012-11-15 21:28:23 +00001624 hdmi->pll = devm_regulator_get(&pdev->dev, "pll");
1625 if (IS_ERR(hdmi->pll)) {
1626 dev_err(&pdev->dev, "failed to get PLL regulator\n");
1627 return PTR_ERR(hdmi->pll);
1628 }
1629
Thierry Reding88685682014-04-16 10:24:12 +02001630 hdmi->vdd = devm_regulator_get(&pdev->dev, "vdd");
1631 if (IS_ERR(hdmi->vdd)) {
1632 dev_err(&pdev->dev, "failed to get VDD regulator\n");
1633 return PTR_ERR(hdmi->vdd);
1634 }
1635
Thierry Redingedec4af2012-11-15 21:28:23 +00001636 hdmi->output.dev = &pdev->dev;
1637
Thierry Reding59d29c02013-10-14 14:26:42 +02001638 err = tegra_output_probe(&hdmi->output);
Thierry Redingedec4af2012-11-15 21:28:23 +00001639 if (err < 0)
1640 return err;
1641
1642 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Redingd4ed6022013-01-21 11:09:02 +01001643 hdmi->regs = devm_ioremap_resource(&pdev->dev, regs);
1644 if (IS_ERR(hdmi->regs))
1645 return PTR_ERR(hdmi->regs);
Thierry Redingedec4af2012-11-15 21:28:23 +00001646
1647 err = platform_get_irq(pdev, 0);
1648 if (err < 0)
1649 return err;
1650
1651 hdmi->irq = err;
1652
Thierry Reding2ccb3962015-01-15 13:43:18 +01001653 err = devm_request_irq(hdmi->dev, hdmi->irq, tegra_hdmi_irq, 0,
1654 dev_name(hdmi->dev), hdmi);
1655 if (err < 0) {
1656 dev_err(&pdev->dev, "failed to request IRQ#%u: %d\n",
1657 hdmi->irq, err);
1658 return err;
1659 }
1660
Thierry Reding52345492015-08-07 16:00:43 +02001661 platform_set_drvdata(pdev, hdmi);
1662 pm_runtime_enable(&pdev->dev);
1663
Thierry Reding776dc382013-10-14 14:43:22 +02001664 INIT_LIST_HEAD(&hdmi->client.list);
1665 hdmi->client.ops = &hdmi_client_ops;
1666 hdmi->client.dev = &pdev->dev;
Thierry Redingedec4af2012-11-15 21:28:23 +00001667
Thierry Reding776dc382013-10-14 14:43:22 +02001668 err = host1x_client_register(&hdmi->client);
Thierry Redingedec4af2012-11-15 21:28:23 +00001669 if (err < 0) {
1670 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
1671 err);
1672 return err;
1673 }
1674
Thierry Redingedec4af2012-11-15 21:28:23 +00001675 return 0;
1676}
1677
1678static int tegra_hdmi_remove(struct platform_device *pdev)
1679{
Thierry Redingedec4af2012-11-15 21:28:23 +00001680 struct tegra_hdmi *hdmi = platform_get_drvdata(pdev);
1681 int err;
1682
Thierry Reding52345492015-08-07 16:00:43 +02001683 pm_runtime_disable(&pdev->dev);
1684
Thierry Reding776dc382013-10-14 14:43:22 +02001685 err = host1x_client_unregister(&hdmi->client);
Thierry Redingedec4af2012-11-15 21:28:23 +00001686 if (err < 0) {
1687 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1688 err);
1689 return err;
1690 }
1691
Thierry Reding328ec692014-12-19 15:55:08 +01001692 tegra_output_remove(&hdmi->output);
Thierry Reding59d29c02013-10-14 14:26:42 +02001693
Thierry Reding52345492015-08-07 16:00:43 +02001694 return 0;
1695}
1696
1697#ifdef CONFIG_PM
1698static int tegra_hdmi_suspend(struct device *dev)
1699{
1700 struct tegra_hdmi *hdmi = dev_get_drvdata(dev);
1701 int err;
1702
1703 err = reset_control_assert(hdmi->rst);
1704 if (err < 0) {
1705 dev_err(dev, "failed to assert reset: %d\n", err);
1706 return err;
1707 }
1708
1709 usleep_range(1000, 2000);
1710
Thierry Redingd06e7f82014-04-16 10:43:41 +02001711 clk_disable_unprepare(hdmi->clk);
Thierry Redingedec4af2012-11-15 21:28:23 +00001712
1713 return 0;
1714}
1715
Thierry Reding52345492015-08-07 16:00:43 +02001716static int tegra_hdmi_resume(struct device *dev)
1717{
1718 struct tegra_hdmi *hdmi = dev_get_drvdata(dev);
1719 int err;
1720
1721 err = clk_prepare_enable(hdmi->clk);
1722 if (err < 0) {
1723 dev_err(dev, "failed to enable clock: %d\n", err);
1724 return err;
1725 }
1726
1727 usleep_range(1000, 2000);
1728
1729 err = reset_control_deassert(hdmi->rst);
1730 if (err < 0) {
1731 dev_err(dev, "failed to deassert reset: %d\n", err);
1732 clk_disable_unprepare(hdmi->clk);
1733 return err;
1734 }
1735
1736 return 0;
1737}
1738#endif
1739
1740static const struct dev_pm_ops tegra_hdmi_pm_ops = {
1741 SET_RUNTIME_PM_OPS(tegra_hdmi_suspend, tegra_hdmi_resume, NULL)
1742};
1743
Thierry Redingedec4af2012-11-15 21:28:23 +00001744struct platform_driver tegra_hdmi_driver = {
1745 .driver = {
1746 .name = "tegra-hdmi",
Thierry Redingedec4af2012-11-15 21:28:23 +00001747 .of_match_table = tegra_hdmi_of_match,
Thierry Reding52345492015-08-07 16:00:43 +02001748 .pm = &tegra_hdmi_pm_ops,
Thierry Redingedec4af2012-11-15 21:28:23 +00001749 },
1750 .probe = tegra_hdmi_probe,
1751 .remove = tegra_hdmi_remove,
1752};