blob: 70e7a938a37b7388a92a6bc51f79576371952a68 [file] [log] [blame]
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001/*
2 * Copyright (C) 2011-2013 Freescale Semiconductor, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
Andy Yanb21f4b62014-12-05 14:26:31 +08009 * Designware High-Definition Multimedia Interface (HDMI) driver
Fabio Estevam9aaf8802013-11-29 08:46:32 -020010 *
11 * Copyright (C) 2010, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
12 */
Andy Yanb21f4b62014-12-05 14:26:31 +080013#include <linux/module.h>
Fabio Estevam9aaf8802013-11-29 08:46:32 -020014#include <linux/irq.h>
15#include <linux/delay.h>
16#include <linux/err.h>
17#include <linux/clk.h>
Sachin Kamat5a819ed2014-01-28 10:33:16 +053018#include <linux/hdmi.h>
Russell King6bcf4952015-02-02 11:01:08 +000019#include <linux/mutex.h>
Fabio Estevam9aaf8802013-11-29 08:46:32 -020020#include <linux/of_device.h>
Russell Kingb90120a2015-03-27 12:59:58 +000021#include <linux/spinlock.h>
Fabio Estevam9aaf8802013-11-29 08:46:32 -020022
Andy Yan3d1b35a2014-12-05 14:25:05 +080023#include <drm/drm_of.h>
Fabio Estevam9aaf8802013-11-29 08:46:32 -020024#include <drm/drmP.h>
25#include <drm/drm_crtc_helper.h>
26#include <drm/drm_edid.h>
27#include <drm/drm_encoder_slave.h>
Andy Yanb21f4b62014-12-05 14:26:31 +080028#include <drm/bridge/dw_hdmi.h>
Fabio Estevam9aaf8802013-11-29 08:46:32 -020029
Andy Yanb21f4b62014-12-05 14:26:31 +080030#include "dw_hdmi.h"
Russell King7ed6c662013-11-07 16:01:45 +000031#include "dw_hdmi-audio.h"
Fabio Estevam9aaf8802013-11-29 08:46:32 -020032
33#define HDMI_EDID_LEN 512
34
35#define RGB 0
36#define YCBCR444 1
37#define YCBCR422_16BITS 2
38#define YCBCR422_8BITS 3
39#define XVYCC444 4
40
41enum hdmi_datamap {
42 RGB444_8B = 0x01,
43 RGB444_10B = 0x03,
44 RGB444_12B = 0x05,
45 RGB444_16B = 0x07,
46 YCbCr444_8B = 0x09,
47 YCbCr444_10B = 0x0B,
48 YCbCr444_12B = 0x0D,
49 YCbCr444_16B = 0x0F,
50 YCbCr422_8B = 0x16,
51 YCbCr422_10B = 0x14,
52 YCbCr422_12B = 0x12,
53};
54
Fabio Estevam9aaf8802013-11-29 08:46:32 -020055static const u16 csc_coeff_default[3][4] = {
56 { 0x2000, 0x0000, 0x0000, 0x0000 },
57 { 0x0000, 0x2000, 0x0000, 0x0000 },
58 { 0x0000, 0x0000, 0x2000, 0x0000 }
59};
60
61static const u16 csc_coeff_rgb_out_eitu601[3][4] = {
62 { 0x2000, 0x6926, 0x74fd, 0x010e },
63 { 0x2000, 0x2cdd, 0x0000, 0x7e9a },
64 { 0x2000, 0x0000, 0x38b4, 0x7e3b }
65};
66
67static const u16 csc_coeff_rgb_out_eitu709[3][4] = {
68 { 0x2000, 0x7106, 0x7a02, 0x00a7 },
69 { 0x2000, 0x3264, 0x0000, 0x7e6d },
70 { 0x2000, 0x0000, 0x3b61, 0x7e25 }
71};
72
73static const u16 csc_coeff_rgb_in_eitu601[3][4] = {
74 { 0x2591, 0x1322, 0x074b, 0x0000 },
75 { 0x6535, 0x2000, 0x7acc, 0x0200 },
76 { 0x6acd, 0x7534, 0x2000, 0x0200 }
77};
78
79static const u16 csc_coeff_rgb_in_eitu709[3][4] = {
80 { 0x2dc5, 0x0d9b, 0x049e, 0x0000 },
81 { 0x62f0, 0x2000, 0x7d11, 0x0200 },
82 { 0x6756, 0x78ab, 0x2000, 0x0200 }
83};
84
85struct hdmi_vmode {
Fabio Estevam9aaf8802013-11-29 08:46:32 -020086 bool mdataenablepolarity;
87
88 unsigned int mpixelclock;
89 unsigned int mpixelrepetitioninput;
90 unsigned int mpixelrepetitionoutput;
91};
92
93struct hdmi_data_info {
94 unsigned int enc_in_format;
95 unsigned int enc_out_format;
96 unsigned int enc_color_depth;
97 unsigned int colorimetry;
98 unsigned int pix_repet_factor;
99 unsigned int hdcp_enable;
100 struct hdmi_vmode video_mode;
101};
102
Andy Yanb21f4b62014-12-05 14:26:31 +0800103struct dw_hdmi {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200104 struct drm_connector connector;
Andy Yan3d1b35a2014-12-05 14:25:05 +0800105 struct drm_encoder *encoder;
106 struct drm_bridge *bridge;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200107
Russell King7ed6c662013-11-07 16:01:45 +0000108 struct platform_device *audio;
Andy Yanb21f4b62014-12-05 14:26:31 +0800109 enum dw_hdmi_devtype dev_type;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200110 struct device *dev;
111 struct clk *isfr_clk;
112 struct clk *iahb_clk;
113
114 struct hdmi_data_info hdmi_data;
Andy Yanb21f4b62014-12-05 14:26:31 +0800115 const struct dw_hdmi_plat_data *plat_data;
116
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200117 int vic;
118
119 u8 edid[HDMI_EDID_LEN];
120 bool cable_plugin;
121
122 bool phy_enabled;
123 struct drm_display_mode previous_mode;
124
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200125 struct i2c_adapter *ddc;
126 void __iomem *regs;
Russell King05b13422015-07-21 15:35:52 +0100127 bool sink_is_hdmi;
Russell Kingf709ec02015-07-21 16:09:39 +0100128 bool sink_has_audio;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200129
Russell Kingb872a8e2015-06-05 12:22:46 +0100130 struct mutex mutex; /* for state below and previous_mode */
Russell King381f05a2015-06-05 15:25:08 +0100131 enum drm_connector_force force; /* mutex-protected force state */
Russell Kingb872a8e2015-06-05 12:22:46 +0100132 bool disabled; /* DRM has disabled our bridge */
Russell King381f05a2015-06-05 15:25:08 +0100133 bool bridge_is_on; /* indicates the bridge is on */
Russell Kingaeac23b2015-06-05 13:46:22 +0100134 bool rxsense; /* rxsense state */
135 u8 phy_mask; /* desired phy int mask settings */
Russell Kingb872a8e2015-06-05 12:22:46 +0100136
Russell Kingb90120a2015-03-27 12:59:58 +0000137 spinlock_t audio_lock;
Russell King6bcf4952015-02-02 11:01:08 +0000138 struct mutex audio_mutex;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200139 unsigned int sample_rate;
Russell Kingb90120a2015-03-27 12:59:58 +0000140 unsigned int audio_cts;
141 unsigned int audio_n;
142 bool audio_enable;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200143 int ratio;
Andy Yan0cd9d142014-12-05 14:28:24 +0800144
145 void (*write)(struct dw_hdmi *hdmi, u8 val, int offset);
146 u8 (*read)(struct dw_hdmi *hdmi, int offset);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200147};
148
Russell Kingaeac23b2015-06-05 13:46:22 +0100149#define HDMI_IH_PHY_STAT0_RX_SENSE \
150 (HDMI_IH_PHY_STAT0_RX_SENSE0 | HDMI_IH_PHY_STAT0_RX_SENSE1 | \
151 HDMI_IH_PHY_STAT0_RX_SENSE2 | HDMI_IH_PHY_STAT0_RX_SENSE3)
152
153#define HDMI_PHY_RX_SENSE \
154 (HDMI_PHY_RX_SENSE0 | HDMI_PHY_RX_SENSE1 | \
155 HDMI_PHY_RX_SENSE2 | HDMI_PHY_RX_SENSE3)
156
Andy Yan0cd9d142014-12-05 14:28:24 +0800157static void dw_hdmi_writel(struct dw_hdmi *hdmi, u8 val, int offset)
158{
159 writel(val, hdmi->regs + (offset << 2));
160}
161
162static u8 dw_hdmi_readl(struct dw_hdmi *hdmi, int offset)
163{
164 return readl(hdmi->regs + (offset << 2));
165}
166
167static void dw_hdmi_writeb(struct dw_hdmi *hdmi, u8 val, int offset)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200168{
169 writeb(val, hdmi->regs + offset);
170}
171
Andy Yan0cd9d142014-12-05 14:28:24 +0800172static u8 dw_hdmi_readb(struct dw_hdmi *hdmi, int offset)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200173{
174 return readb(hdmi->regs + offset);
175}
176
Andy Yan0cd9d142014-12-05 14:28:24 +0800177static inline void hdmi_writeb(struct dw_hdmi *hdmi, u8 val, int offset)
178{
179 hdmi->write(hdmi, val, offset);
180}
181
182static inline u8 hdmi_readb(struct dw_hdmi *hdmi, int offset)
183{
184 return hdmi->read(hdmi, offset);
185}
186
Andy Yanb21f4b62014-12-05 14:26:31 +0800187static void hdmi_modb(struct dw_hdmi *hdmi, u8 data, u8 mask, unsigned reg)
Russell King812bc612013-11-04 12:42:02 +0000188{
189 u8 val = hdmi_readb(hdmi, reg) & ~mask;
Fabio Estevamb44ab1b2014-04-28 08:01:07 -0300190
Russell King812bc612013-11-04 12:42:02 +0000191 val |= data & mask;
192 hdmi_writeb(hdmi, val, reg);
193}
194
Andy Yanb21f4b62014-12-05 14:26:31 +0800195static void hdmi_mask_writeb(struct dw_hdmi *hdmi, u8 data, unsigned int reg,
Andy Yanb5878332014-12-05 14:23:52 +0800196 u8 shift, u8 mask)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200197{
Russell King812bc612013-11-04 12:42:02 +0000198 hdmi_modb(hdmi, data << shift, mask, reg);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200199}
200
Russell King351e1352015-01-31 14:50:23 +0000201static void hdmi_set_cts_n(struct dw_hdmi *hdmi, unsigned int cts,
202 unsigned int n)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200203{
Russell King622494a2015-02-02 10:55:38 +0000204 /* Must be set/cleared first */
205 hdmi_modb(hdmi, 0, HDMI_AUD_CTS3_CTS_MANUAL, HDMI_AUD_CTS3);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200206
207 /* nshift factor = 0 */
Russell King812bc612013-11-04 12:42:02 +0000208 hdmi_modb(hdmi, 0, HDMI_AUD_CTS3_N_SHIFT_MASK, HDMI_AUD_CTS3);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200209
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200210 hdmi_writeb(hdmi, ((cts >> 16) & HDMI_AUD_CTS3_AUDCTS19_16_MASK) |
211 HDMI_AUD_CTS3_CTS_MANUAL, HDMI_AUD_CTS3);
Russell King622494a2015-02-02 10:55:38 +0000212 hdmi_writeb(hdmi, (cts >> 8) & 0xff, HDMI_AUD_CTS2);
213 hdmi_writeb(hdmi, cts & 0xff, HDMI_AUD_CTS1);
214
215 hdmi_writeb(hdmi, (n >> 16) & 0x0f, HDMI_AUD_N3);
216 hdmi_writeb(hdmi, (n >> 8) & 0xff, HDMI_AUD_N2);
217 hdmi_writeb(hdmi, n & 0xff, HDMI_AUD_N1);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200218}
219
220static unsigned int hdmi_compute_n(unsigned int freq, unsigned long pixel_clk,
221 unsigned int ratio)
222{
223 unsigned int n = (128 * freq) / 1000;
224
225 switch (freq) {
226 case 32000:
227 if (pixel_clk == 25170000)
228 n = (ratio == 150) ? 9152 : 4576;
229 else if (pixel_clk == 27020000)
230 n = (ratio == 150) ? 8192 : 4096;
231 else if (pixel_clk == 74170000 || pixel_clk == 148350000)
232 n = 11648;
233 else
234 n = 4096;
235 break;
236
237 case 44100:
238 if (pixel_clk == 25170000)
239 n = 7007;
240 else if (pixel_clk == 74170000)
241 n = 17836;
242 else if (pixel_clk == 148350000)
243 n = (ratio == 150) ? 17836 : 8918;
244 else
245 n = 6272;
246 break;
247
248 case 48000:
249 if (pixel_clk == 25170000)
250 n = (ratio == 150) ? 9152 : 6864;
251 else if (pixel_clk == 27020000)
252 n = (ratio == 150) ? 8192 : 6144;
253 else if (pixel_clk == 74170000)
254 n = 11648;
255 else if (pixel_clk == 148350000)
256 n = (ratio == 150) ? 11648 : 5824;
257 else
258 n = 6144;
259 break;
260
261 case 88200:
262 n = hdmi_compute_n(44100, pixel_clk, ratio) * 2;
263 break;
264
265 case 96000:
266 n = hdmi_compute_n(48000, pixel_clk, ratio) * 2;
267 break;
268
269 case 176400:
270 n = hdmi_compute_n(44100, pixel_clk, ratio) * 4;
271 break;
272
273 case 192000:
274 n = hdmi_compute_n(48000, pixel_clk, ratio) * 4;
275 break;
276
277 default:
278 break;
279 }
280
281 return n;
282}
283
284static unsigned int hdmi_compute_cts(unsigned int freq, unsigned long pixel_clk,
285 unsigned int ratio)
286{
287 unsigned int cts = 0;
288
289 pr_debug("%s: freq: %d pixel_clk: %ld ratio: %d\n", __func__, freq,
290 pixel_clk, ratio);
291
292 switch (freq) {
293 case 32000:
294 if (pixel_clk == 297000000) {
295 cts = 222750;
296 break;
297 }
298 case 48000:
299 case 96000:
300 case 192000:
301 switch (pixel_clk) {
302 case 25200000:
303 case 27000000:
304 case 54000000:
305 case 74250000:
306 case 148500000:
307 cts = pixel_clk / 1000;
308 break;
309 case 297000000:
310 cts = 247500;
311 break;
312 /*
313 * All other TMDS clocks are not supported by
314 * DWC_hdmi_tx. The TMDS clocks divided or
315 * multiplied by 1,001 coefficients are not
316 * supported.
317 */
318 default:
319 break;
320 }
321 break;
322 case 44100:
323 case 88200:
324 case 176400:
325 switch (pixel_clk) {
326 case 25200000:
327 cts = 28000;
328 break;
329 case 27000000:
330 cts = 30000;
331 break;
332 case 54000000:
333 cts = 60000;
334 break;
335 case 74250000:
336 cts = 82500;
337 break;
338 case 148500000:
339 cts = 165000;
340 break;
341 case 297000000:
342 cts = 247500;
343 break;
344 default:
345 break;
346 }
347 break;
348 default:
349 break;
350 }
351 if (ratio == 100)
352 return cts;
Catalina Mocanu7557b6e2014-09-24 14:27:36 -0700353 return (cts * ratio) / 100;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200354}
355
Andy Yanb21f4b62014-12-05 14:26:31 +0800356static void hdmi_set_clk_regenerator(struct dw_hdmi *hdmi,
Russell Kingf879b382015-03-27 12:53:29 +0000357 unsigned long pixel_clk, unsigned int sample_rate, unsigned int ratio)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200358{
Russell Kingf879b382015-03-27 12:53:29 +0000359 unsigned int n, cts;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200360
Russell Kingf879b382015-03-27 12:53:29 +0000361 n = hdmi_compute_n(sample_rate, pixel_clk, ratio);
362 cts = hdmi_compute_cts(sample_rate, pixel_clk, ratio);
363 if (!cts) {
364 dev_err(hdmi->dev,
365 "%s: pixel clock/sample rate not supported: %luMHz / %ukHz\n",
366 __func__, pixel_clk, sample_rate);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200367 }
368
Russell Kingf879b382015-03-27 12:53:29 +0000369 dev_dbg(hdmi->dev, "%s: samplerate=%ukHz ratio=%d pixelclk=%luMHz N=%d cts=%d\n",
370 __func__, sample_rate, ratio, pixel_clk, n, cts);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200371
Russell Kingb90120a2015-03-27 12:59:58 +0000372 spin_lock_irq(&hdmi->audio_lock);
373 hdmi->audio_n = n;
374 hdmi->audio_cts = cts;
375 hdmi_set_cts_n(hdmi, cts, hdmi->audio_enable ? n : 0);
376 spin_unlock_irq(&hdmi->audio_lock);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200377}
378
Andy Yanb21f4b62014-12-05 14:26:31 +0800379static void hdmi_init_clk_regenerator(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200380{
Russell King6bcf4952015-02-02 11:01:08 +0000381 mutex_lock(&hdmi->audio_mutex);
Russell Kingf879b382015-03-27 12:53:29 +0000382 hdmi_set_clk_regenerator(hdmi, 74250000, hdmi->sample_rate,
383 hdmi->ratio);
Russell King6bcf4952015-02-02 11:01:08 +0000384 mutex_unlock(&hdmi->audio_mutex);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200385}
386
Andy Yanb21f4b62014-12-05 14:26:31 +0800387static void hdmi_clk_regenerator_update_pixel_clock(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200388{
Russell King6bcf4952015-02-02 11:01:08 +0000389 mutex_lock(&hdmi->audio_mutex);
Russell Kingf879b382015-03-27 12:53:29 +0000390 hdmi_set_clk_regenerator(hdmi, hdmi->hdmi_data.video_mode.mpixelclock,
391 hdmi->sample_rate, hdmi->ratio);
Russell King6bcf4952015-02-02 11:01:08 +0000392 mutex_unlock(&hdmi->audio_mutex);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200393}
394
Russell Kingb5814ff2015-03-27 12:50:58 +0000395void dw_hdmi_set_sample_rate(struct dw_hdmi *hdmi, unsigned int rate)
396{
397 mutex_lock(&hdmi->audio_mutex);
398 hdmi->sample_rate = rate;
399 hdmi_set_clk_regenerator(hdmi, hdmi->hdmi_data.video_mode.mpixelclock,
400 hdmi->sample_rate, hdmi->ratio);
401 mutex_unlock(&hdmi->audio_mutex);
402}
403EXPORT_SYMBOL_GPL(dw_hdmi_set_sample_rate);
404
Russell Kingb90120a2015-03-27 12:59:58 +0000405void dw_hdmi_audio_enable(struct dw_hdmi *hdmi)
406{
407 unsigned long flags;
408
409 spin_lock_irqsave(&hdmi->audio_lock, flags);
410 hdmi->audio_enable = true;
411 hdmi_set_cts_n(hdmi, hdmi->audio_cts, hdmi->audio_n);
412 spin_unlock_irqrestore(&hdmi->audio_lock, flags);
413}
414EXPORT_SYMBOL_GPL(dw_hdmi_audio_enable);
415
416void dw_hdmi_audio_disable(struct dw_hdmi *hdmi)
417{
418 unsigned long flags;
419
420 spin_lock_irqsave(&hdmi->audio_lock, flags);
421 hdmi->audio_enable = false;
422 hdmi_set_cts_n(hdmi, hdmi->audio_cts, 0);
423 spin_unlock_irqrestore(&hdmi->audio_lock, flags);
424}
425EXPORT_SYMBOL_GPL(dw_hdmi_audio_disable);
426
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200427/*
428 * this submodule is responsible for the video data synchronization.
429 * for example, for RGB 4:4:4 input, the data map is defined as
430 * pin{47~40} <==> R[7:0]
431 * pin{31~24} <==> G[7:0]
432 * pin{15~8} <==> B[7:0]
433 */
Andy Yanb21f4b62014-12-05 14:26:31 +0800434static void hdmi_video_sample(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200435{
436 int color_format = 0;
437 u8 val;
438
439 if (hdmi->hdmi_data.enc_in_format == RGB) {
440 if (hdmi->hdmi_data.enc_color_depth == 8)
441 color_format = 0x01;
442 else if (hdmi->hdmi_data.enc_color_depth == 10)
443 color_format = 0x03;
444 else if (hdmi->hdmi_data.enc_color_depth == 12)
445 color_format = 0x05;
446 else if (hdmi->hdmi_data.enc_color_depth == 16)
447 color_format = 0x07;
448 else
449 return;
450 } else if (hdmi->hdmi_data.enc_in_format == YCBCR444) {
451 if (hdmi->hdmi_data.enc_color_depth == 8)
452 color_format = 0x09;
453 else if (hdmi->hdmi_data.enc_color_depth == 10)
454 color_format = 0x0B;
455 else if (hdmi->hdmi_data.enc_color_depth == 12)
456 color_format = 0x0D;
457 else if (hdmi->hdmi_data.enc_color_depth == 16)
458 color_format = 0x0F;
459 else
460 return;
461 } else if (hdmi->hdmi_data.enc_in_format == YCBCR422_8BITS) {
462 if (hdmi->hdmi_data.enc_color_depth == 8)
463 color_format = 0x16;
464 else if (hdmi->hdmi_data.enc_color_depth == 10)
465 color_format = 0x14;
466 else if (hdmi->hdmi_data.enc_color_depth == 12)
467 color_format = 0x12;
468 else
469 return;
470 }
471
472 val = HDMI_TX_INVID0_INTERNAL_DE_GENERATOR_DISABLE |
473 ((color_format << HDMI_TX_INVID0_VIDEO_MAPPING_OFFSET) &
474 HDMI_TX_INVID0_VIDEO_MAPPING_MASK);
475 hdmi_writeb(hdmi, val, HDMI_TX_INVID0);
476
477 /* Enable TX stuffing: When DE is inactive, fix the output data to 0 */
478 val = HDMI_TX_INSTUFFING_BDBDATA_STUFFING_ENABLE |
479 HDMI_TX_INSTUFFING_RCRDATA_STUFFING_ENABLE |
480 HDMI_TX_INSTUFFING_GYDATA_STUFFING_ENABLE;
481 hdmi_writeb(hdmi, val, HDMI_TX_INSTUFFING);
482 hdmi_writeb(hdmi, 0x0, HDMI_TX_GYDATA0);
483 hdmi_writeb(hdmi, 0x0, HDMI_TX_GYDATA1);
484 hdmi_writeb(hdmi, 0x0, HDMI_TX_RCRDATA0);
485 hdmi_writeb(hdmi, 0x0, HDMI_TX_RCRDATA1);
486 hdmi_writeb(hdmi, 0x0, HDMI_TX_BCBDATA0);
487 hdmi_writeb(hdmi, 0x0, HDMI_TX_BCBDATA1);
488}
489
Andy Yanb21f4b62014-12-05 14:26:31 +0800490static int is_color_space_conversion(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200491{
Fabio Estevamba92b222014-02-06 10:12:03 -0200492 return hdmi->hdmi_data.enc_in_format != hdmi->hdmi_data.enc_out_format;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200493}
494
Andy Yanb21f4b62014-12-05 14:26:31 +0800495static int is_color_space_decimation(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200496{
Fabio Estevamba92b222014-02-06 10:12:03 -0200497 if (hdmi->hdmi_data.enc_out_format != YCBCR422_8BITS)
498 return 0;
499 if (hdmi->hdmi_data.enc_in_format == RGB ||
500 hdmi->hdmi_data.enc_in_format == YCBCR444)
501 return 1;
502 return 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200503}
504
Andy Yanb21f4b62014-12-05 14:26:31 +0800505static int is_color_space_interpolation(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200506{
Fabio Estevamba92b222014-02-06 10:12:03 -0200507 if (hdmi->hdmi_data.enc_in_format != YCBCR422_8BITS)
508 return 0;
509 if (hdmi->hdmi_data.enc_out_format == RGB ||
510 hdmi->hdmi_data.enc_out_format == YCBCR444)
511 return 1;
512 return 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200513}
514
Andy Yanb21f4b62014-12-05 14:26:31 +0800515static void dw_hdmi_update_csc_coeffs(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200516{
517 const u16 (*csc_coeff)[3][4] = &csc_coeff_default;
Russell Kingc082f9d2013-11-04 12:10:40 +0000518 unsigned i;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200519 u32 csc_scale = 1;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200520
521 if (is_color_space_conversion(hdmi)) {
522 if (hdmi->hdmi_data.enc_out_format == RGB) {
Gulsah Kose256a38b2014-03-09 20:11:07 +0200523 if (hdmi->hdmi_data.colorimetry ==
524 HDMI_COLORIMETRY_ITU_601)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200525 csc_coeff = &csc_coeff_rgb_out_eitu601;
526 else
527 csc_coeff = &csc_coeff_rgb_out_eitu709;
528 } else if (hdmi->hdmi_data.enc_in_format == RGB) {
Gulsah Kose256a38b2014-03-09 20:11:07 +0200529 if (hdmi->hdmi_data.colorimetry ==
530 HDMI_COLORIMETRY_ITU_601)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200531 csc_coeff = &csc_coeff_rgb_in_eitu601;
532 else
533 csc_coeff = &csc_coeff_rgb_in_eitu709;
534 csc_scale = 0;
535 }
536 }
537
Russell Kingc082f9d2013-11-04 12:10:40 +0000538 /* The CSC registers are sequential, alternating MSB then LSB */
539 for (i = 0; i < ARRAY_SIZE(csc_coeff_default[0]); i++) {
540 u16 coeff_a = (*csc_coeff)[0][i];
541 u16 coeff_b = (*csc_coeff)[1][i];
542 u16 coeff_c = (*csc_coeff)[2][i];
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200543
Andy Yanb5878332014-12-05 14:23:52 +0800544 hdmi_writeb(hdmi, coeff_a & 0xff, HDMI_CSC_COEF_A1_LSB + i * 2);
Russell Kingc082f9d2013-11-04 12:10:40 +0000545 hdmi_writeb(hdmi, coeff_a >> 8, HDMI_CSC_COEF_A1_MSB + i * 2);
546 hdmi_writeb(hdmi, coeff_b & 0xff, HDMI_CSC_COEF_B1_LSB + i * 2);
547 hdmi_writeb(hdmi, coeff_b >> 8, HDMI_CSC_COEF_B1_MSB + i * 2);
Andy Yanb5878332014-12-05 14:23:52 +0800548 hdmi_writeb(hdmi, coeff_c & 0xff, HDMI_CSC_COEF_C1_LSB + i * 2);
Russell Kingc082f9d2013-11-04 12:10:40 +0000549 hdmi_writeb(hdmi, coeff_c >> 8, HDMI_CSC_COEF_C1_MSB + i * 2);
550 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200551
Russell King812bc612013-11-04 12:42:02 +0000552 hdmi_modb(hdmi, csc_scale, HDMI_CSC_SCALE_CSCSCALE_MASK,
553 HDMI_CSC_SCALE);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200554}
555
Andy Yanb21f4b62014-12-05 14:26:31 +0800556static void hdmi_video_csc(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200557{
558 int color_depth = 0;
559 int interpolation = HDMI_CSC_CFG_INTMODE_DISABLE;
560 int decimation = 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200561
562 /* YCC422 interpolation to 444 mode */
563 if (is_color_space_interpolation(hdmi))
564 interpolation = HDMI_CSC_CFG_INTMODE_CHROMA_INT_FORMULA1;
565 else if (is_color_space_decimation(hdmi))
566 decimation = HDMI_CSC_CFG_DECMODE_CHROMA_INT_FORMULA3;
567
568 if (hdmi->hdmi_data.enc_color_depth == 8)
569 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_24BPP;
570 else if (hdmi->hdmi_data.enc_color_depth == 10)
571 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_30BPP;
572 else if (hdmi->hdmi_data.enc_color_depth == 12)
573 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_36BPP;
574 else if (hdmi->hdmi_data.enc_color_depth == 16)
575 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_48BPP;
576 else
577 return;
578
579 /* Configure the CSC registers */
580 hdmi_writeb(hdmi, interpolation | decimation, HDMI_CSC_CFG);
Russell King812bc612013-11-04 12:42:02 +0000581 hdmi_modb(hdmi, color_depth, HDMI_CSC_SCALE_CSC_COLORDE_PTH_MASK,
582 HDMI_CSC_SCALE);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200583
Andy Yanb21f4b62014-12-05 14:26:31 +0800584 dw_hdmi_update_csc_coeffs(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200585}
586
587/*
588 * HDMI video packetizer is used to packetize the data.
589 * for example, if input is YCC422 mode or repeater is used,
590 * data should be repacked this module can be bypassed.
591 */
Andy Yanb21f4b62014-12-05 14:26:31 +0800592static void hdmi_video_packetize(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200593{
594 unsigned int color_depth = 0;
595 unsigned int remap_size = HDMI_VP_REMAP_YCC422_16bit;
596 unsigned int output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_PP;
597 struct hdmi_data_info *hdmi_data = &hdmi->hdmi_data;
Russell Kingbebdf662013-11-04 12:55:30 +0000598 u8 val, vp_conf;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200599
Andy Yanb5878332014-12-05 14:23:52 +0800600 if (hdmi_data->enc_out_format == RGB ||
601 hdmi_data->enc_out_format == YCBCR444) {
602 if (!hdmi_data->enc_color_depth) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200603 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;
Andy Yanb5878332014-12-05 14:23:52 +0800604 } else if (hdmi_data->enc_color_depth == 8) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200605 color_depth = 4;
606 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;
Andy Yanb5878332014-12-05 14:23:52 +0800607 } else if (hdmi_data->enc_color_depth == 10) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200608 color_depth = 5;
Andy Yanb5878332014-12-05 14:23:52 +0800609 } else if (hdmi_data->enc_color_depth == 12) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200610 color_depth = 6;
Andy Yanb5878332014-12-05 14:23:52 +0800611 } else if (hdmi_data->enc_color_depth == 16) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200612 color_depth = 7;
Andy Yanb5878332014-12-05 14:23:52 +0800613 } else {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200614 return;
Andy Yanb5878332014-12-05 14:23:52 +0800615 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200616 } else if (hdmi_data->enc_out_format == YCBCR422_8BITS) {
617 if (!hdmi_data->enc_color_depth ||
618 hdmi_data->enc_color_depth == 8)
619 remap_size = HDMI_VP_REMAP_YCC422_16bit;
620 else if (hdmi_data->enc_color_depth == 10)
621 remap_size = HDMI_VP_REMAP_YCC422_20bit;
622 else if (hdmi_data->enc_color_depth == 12)
623 remap_size = HDMI_VP_REMAP_YCC422_24bit;
624 else
625 return;
626 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_YCC422;
Andy Yanb5878332014-12-05 14:23:52 +0800627 } else {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200628 return;
Andy Yanb5878332014-12-05 14:23:52 +0800629 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200630
631 /* set the packetizer registers */
632 val = ((color_depth << HDMI_VP_PR_CD_COLOR_DEPTH_OFFSET) &
633 HDMI_VP_PR_CD_COLOR_DEPTH_MASK) |
634 ((hdmi_data->pix_repet_factor <<
635 HDMI_VP_PR_CD_DESIRED_PR_FACTOR_OFFSET) &
636 HDMI_VP_PR_CD_DESIRED_PR_FACTOR_MASK);
637 hdmi_writeb(hdmi, val, HDMI_VP_PR_CD);
638
Russell King812bc612013-11-04 12:42:02 +0000639 hdmi_modb(hdmi, HDMI_VP_STUFF_PR_STUFFING_STUFFING_MODE,
640 HDMI_VP_STUFF_PR_STUFFING_MASK, HDMI_VP_STUFF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200641
642 /* Data from pixel repeater block */
643 if (hdmi_data->pix_repet_factor > 1) {
Russell Kingbebdf662013-11-04 12:55:30 +0000644 vp_conf = HDMI_VP_CONF_PR_EN_ENABLE |
645 HDMI_VP_CONF_BYPASS_SELECT_PIX_REPEATER;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200646 } else { /* data from packetizer block */
Russell Kingbebdf662013-11-04 12:55:30 +0000647 vp_conf = HDMI_VP_CONF_PR_EN_DISABLE |
648 HDMI_VP_CONF_BYPASS_SELECT_VID_PACKETIZER;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200649 }
650
Russell Kingbebdf662013-11-04 12:55:30 +0000651 hdmi_modb(hdmi, vp_conf,
652 HDMI_VP_CONF_PR_EN_MASK |
653 HDMI_VP_CONF_BYPASS_SELECT_MASK, HDMI_VP_CONF);
654
Russell King812bc612013-11-04 12:42:02 +0000655 hdmi_modb(hdmi, 1 << HDMI_VP_STUFF_IDEFAULT_PHASE_OFFSET,
656 HDMI_VP_STUFF_IDEFAULT_PHASE_MASK, HDMI_VP_STUFF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200657
658 hdmi_writeb(hdmi, remap_size, HDMI_VP_REMAP);
659
660 if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_PP) {
Russell Kingbebdf662013-11-04 12:55:30 +0000661 vp_conf = HDMI_VP_CONF_BYPASS_EN_DISABLE |
662 HDMI_VP_CONF_PP_EN_ENABLE |
663 HDMI_VP_CONF_YCC422_EN_DISABLE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200664 } else if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_YCC422) {
Russell Kingbebdf662013-11-04 12:55:30 +0000665 vp_conf = HDMI_VP_CONF_BYPASS_EN_DISABLE |
666 HDMI_VP_CONF_PP_EN_DISABLE |
667 HDMI_VP_CONF_YCC422_EN_ENABLE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200668 } else if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS) {
Russell Kingbebdf662013-11-04 12:55:30 +0000669 vp_conf = HDMI_VP_CONF_BYPASS_EN_ENABLE |
670 HDMI_VP_CONF_PP_EN_DISABLE |
671 HDMI_VP_CONF_YCC422_EN_DISABLE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200672 } else {
673 return;
674 }
675
Russell Kingbebdf662013-11-04 12:55:30 +0000676 hdmi_modb(hdmi, vp_conf,
677 HDMI_VP_CONF_BYPASS_EN_MASK | HDMI_VP_CONF_PP_EN_ENMASK |
678 HDMI_VP_CONF_YCC422_EN_MASK, HDMI_VP_CONF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200679
Russell King812bc612013-11-04 12:42:02 +0000680 hdmi_modb(hdmi, HDMI_VP_STUFF_PP_STUFFING_STUFFING_MODE |
681 HDMI_VP_STUFF_YCC422_STUFFING_STUFFING_MODE,
682 HDMI_VP_STUFF_PP_STUFFING_MASK |
683 HDMI_VP_STUFF_YCC422_STUFFING_MASK, HDMI_VP_STUFF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200684
Russell King812bc612013-11-04 12:42:02 +0000685 hdmi_modb(hdmi, output_select, HDMI_VP_CONF_OUTPUT_SELECTOR_MASK,
686 HDMI_VP_CONF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200687}
688
Andy Yanb21f4b62014-12-05 14:26:31 +0800689static inline void hdmi_phy_test_clear(struct dw_hdmi *hdmi,
Andy Yanb5878332014-12-05 14:23:52 +0800690 unsigned char bit)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200691{
Russell King812bc612013-11-04 12:42:02 +0000692 hdmi_modb(hdmi, bit << HDMI_PHY_TST0_TSTCLR_OFFSET,
693 HDMI_PHY_TST0_TSTCLR_MASK, HDMI_PHY_TST0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200694}
695
Andy Yanb21f4b62014-12-05 14:26:31 +0800696static inline void hdmi_phy_test_enable(struct dw_hdmi *hdmi,
Andy Yanb5878332014-12-05 14:23:52 +0800697 unsigned char bit)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200698{
Russell King812bc612013-11-04 12:42:02 +0000699 hdmi_modb(hdmi, bit << HDMI_PHY_TST0_TSTEN_OFFSET,
700 HDMI_PHY_TST0_TSTEN_MASK, HDMI_PHY_TST0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200701}
702
Andy Yanb21f4b62014-12-05 14:26:31 +0800703static inline void hdmi_phy_test_clock(struct dw_hdmi *hdmi,
Andy Yanb5878332014-12-05 14:23:52 +0800704 unsigned char bit)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200705{
Russell King812bc612013-11-04 12:42:02 +0000706 hdmi_modb(hdmi, bit << HDMI_PHY_TST0_TSTCLK_OFFSET,
707 HDMI_PHY_TST0_TSTCLK_MASK, HDMI_PHY_TST0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200708}
709
Andy Yanb21f4b62014-12-05 14:26:31 +0800710static inline void hdmi_phy_test_din(struct dw_hdmi *hdmi,
Andy Yanb5878332014-12-05 14:23:52 +0800711 unsigned char bit)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200712{
713 hdmi_writeb(hdmi, bit, HDMI_PHY_TST1);
714}
715
Andy Yanb21f4b62014-12-05 14:26:31 +0800716static inline void hdmi_phy_test_dout(struct dw_hdmi *hdmi,
Andy Yanb5878332014-12-05 14:23:52 +0800717 unsigned char bit)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200718{
719 hdmi_writeb(hdmi, bit, HDMI_PHY_TST2);
720}
721
Andy Yanb21f4b62014-12-05 14:26:31 +0800722static bool hdmi_phy_wait_i2c_done(struct dw_hdmi *hdmi, int msec)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200723{
Andy Yana4d3b8b2014-12-05 14:31:09 +0800724 u32 val;
725
726 while ((val = hdmi_readb(hdmi, HDMI_IH_I2CMPHY_STAT0) & 0x3) == 0) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200727 if (msec-- == 0)
728 return false;
Emil Renner Berthing0e6bcf32014-03-30 00:21:21 +0100729 udelay(1000);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200730 }
Andy Yana4d3b8b2014-12-05 14:31:09 +0800731 hdmi_writeb(hdmi, val, HDMI_IH_I2CMPHY_STAT0);
732
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200733 return true;
734}
735
Andy Yanb21f4b62014-12-05 14:26:31 +0800736static void __hdmi_phy_i2c_write(struct dw_hdmi *hdmi, unsigned short data,
Andy Yanb5878332014-12-05 14:23:52 +0800737 unsigned char addr)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200738{
739 hdmi_writeb(hdmi, 0xFF, HDMI_IH_I2CMPHY_STAT0);
740 hdmi_writeb(hdmi, addr, HDMI_PHY_I2CM_ADDRESS_ADDR);
741 hdmi_writeb(hdmi, (unsigned char)(data >> 8),
Andy Yanb5878332014-12-05 14:23:52 +0800742 HDMI_PHY_I2CM_DATAO_1_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200743 hdmi_writeb(hdmi, (unsigned char)(data >> 0),
Andy Yanb5878332014-12-05 14:23:52 +0800744 HDMI_PHY_I2CM_DATAO_0_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200745 hdmi_writeb(hdmi, HDMI_PHY_I2CM_OPERATION_ADDR_WRITE,
Andy Yanb5878332014-12-05 14:23:52 +0800746 HDMI_PHY_I2CM_OPERATION_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200747 hdmi_phy_wait_i2c_done(hdmi, 1000);
748}
749
Andy Yanb21f4b62014-12-05 14:26:31 +0800750static int hdmi_phy_i2c_write(struct dw_hdmi *hdmi, unsigned short data,
Andy Yanb5878332014-12-05 14:23:52 +0800751 unsigned char addr)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200752{
753 __hdmi_phy_i2c_write(hdmi, data, addr);
754 return 0;
755}
756
Russell King2fada102015-07-28 12:21:34 +0100757static void dw_hdmi_phy_enable_powerdown(struct dw_hdmi *hdmi, bool enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200758{
Russell King2fada102015-07-28 12:21:34 +0100759 hdmi_mask_writeb(hdmi, !enable, HDMI_PHY_CONF0,
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200760 HDMI_PHY_CONF0_PDZ_OFFSET,
761 HDMI_PHY_CONF0_PDZ_MASK);
762}
763
Andy Yanb21f4b62014-12-05 14:26:31 +0800764static void dw_hdmi_phy_enable_tmds(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200765{
766 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
767 HDMI_PHY_CONF0_ENTMDS_OFFSET,
768 HDMI_PHY_CONF0_ENTMDS_MASK);
769}
770
Andy Yand346c142014-12-05 14:31:53 +0800771static void dw_hdmi_phy_enable_spare(struct dw_hdmi *hdmi, u8 enable)
772{
773 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
774 HDMI_PHY_CONF0_SPARECTRL_OFFSET,
775 HDMI_PHY_CONF0_SPARECTRL_MASK);
776}
777
Andy Yanb21f4b62014-12-05 14:26:31 +0800778static void dw_hdmi_phy_gen2_pddq(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200779{
780 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
781 HDMI_PHY_CONF0_GEN2_PDDQ_OFFSET,
782 HDMI_PHY_CONF0_GEN2_PDDQ_MASK);
783}
784
Andy Yanb21f4b62014-12-05 14:26:31 +0800785static void dw_hdmi_phy_gen2_txpwron(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200786{
787 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
788 HDMI_PHY_CONF0_GEN2_TXPWRON_OFFSET,
789 HDMI_PHY_CONF0_GEN2_TXPWRON_MASK);
790}
791
Andy Yanb21f4b62014-12-05 14:26:31 +0800792static void dw_hdmi_phy_sel_data_en_pol(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200793{
794 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
795 HDMI_PHY_CONF0_SELDATAENPOL_OFFSET,
796 HDMI_PHY_CONF0_SELDATAENPOL_MASK);
797}
798
Andy Yanb21f4b62014-12-05 14:26:31 +0800799static void dw_hdmi_phy_sel_interface_control(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200800{
801 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
802 HDMI_PHY_CONF0_SELDIPIF_OFFSET,
803 HDMI_PHY_CONF0_SELDIPIF_MASK);
804}
805
Andy Yanb21f4b62014-12-05 14:26:31 +0800806static int hdmi_phy_configure(struct dw_hdmi *hdmi, unsigned char prep,
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200807 unsigned char res, int cscon)
808{
Russell King39cc1532015-03-31 18:34:11 +0100809 unsigned res_idx;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200810 u8 val, msec;
Russell King39cc1532015-03-31 18:34:11 +0100811 const struct dw_hdmi_plat_data *pdata = hdmi->plat_data;
812 const struct dw_hdmi_mpll_config *mpll_config = pdata->mpll_cfg;
813 const struct dw_hdmi_curr_ctrl *curr_ctrl = pdata->cur_ctr;
814 const struct dw_hdmi_phy_config *phy_config = pdata->phy_config;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200815
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200816 if (prep)
817 return -EINVAL;
Russell King3e46f152013-11-04 11:24:00 +0000818
819 switch (res) {
820 case 0: /* color resolution 0 is 8 bit colour depth */
821 case 8:
Andy Yanb21f4b62014-12-05 14:26:31 +0800822 res_idx = DW_HDMI_RES_8;
Russell King3e46f152013-11-04 11:24:00 +0000823 break;
824 case 10:
Andy Yanb21f4b62014-12-05 14:26:31 +0800825 res_idx = DW_HDMI_RES_10;
Russell King3e46f152013-11-04 11:24:00 +0000826 break;
827 case 12:
Andy Yanb21f4b62014-12-05 14:26:31 +0800828 res_idx = DW_HDMI_RES_12;
Russell King3e46f152013-11-04 11:24:00 +0000829 break;
830 default:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200831 return -EINVAL;
Russell King3e46f152013-11-04 11:24:00 +0000832 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200833
Russell King39cc1532015-03-31 18:34:11 +0100834 /* PLL/MPLL Cfg - always match on final entry */
835 for (; mpll_config->mpixelclock != ~0UL; mpll_config++)
836 if (hdmi->hdmi_data.video_mode.mpixelclock <=
837 mpll_config->mpixelclock)
838 break;
839
840 for (; curr_ctrl->mpixelclock != ~0UL; curr_ctrl++)
841 if (hdmi->hdmi_data.video_mode.mpixelclock <=
842 curr_ctrl->mpixelclock)
843 break;
844
845 for (; phy_config->mpixelclock != ~0UL; phy_config++)
846 if (hdmi->hdmi_data.video_mode.mpixelclock <=
847 phy_config->mpixelclock)
848 break;
849
850 if (mpll_config->mpixelclock == ~0UL ||
851 curr_ctrl->mpixelclock == ~0UL ||
852 phy_config->mpixelclock == ~0UL) {
853 dev_err(hdmi->dev, "Pixel clock %d - unsupported by HDMI\n",
854 hdmi->hdmi_data.video_mode.mpixelclock);
855 return -EINVAL;
856 }
857
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200858 /* Enable csc path */
859 if (cscon)
860 val = HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_IN_PATH;
861 else
862 val = HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_BYPASS;
863
864 hdmi_writeb(hdmi, val, HDMI_MC_FLOWCTRL);
865
866 /* gen2 tx power off */
Andy Yanb21f4b62014-12-05 14:26:31 +0800867 dw_hdmi_phy_gen2_txpwron(hdmi, 0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200868
869 /* gen2 pddq */
Andy Yanb21f4b62014-12-05 14:26:31 +0800870 dw_hdmi_phy_gen2_pddq(hdmi, 1);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200871
872 /* PHY reset */
873 hdmi_writeb(hdmi, HDMI_MC_PHYRSTZ_DEASSERT, HDMI_MC_PHYRSTZ);
874 hdmi_writeb(hdmi, HDMI_MC_PHYRSTZ_ASSERT, HDMI_MC_PHYRSTZ);
875
876 hdmi_writeb(hdmi, HDMI_MC_HEACPHY_RST_ASSERT, HDMI_MC_HEACPHY_RST);
877
878 hdmi_phy_test_clear(hdmi, 1);
879 hdmi_writeb(hdmi, HDMI_PHY_I2CM_SLAVE_ADDR_PHY_GEN2,
Andy Yanb5878332014-12-05 14:23:52 +0800880 HDMI_PHY_I2CM_SLAVE_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200881 hdmi_phy_test_clear(hdmi, 0);
882
Russell King39cc1532015-03-31 18:34:11 +0100883 hdmi_phy_i2c_write(hdmi, mpll_config->res[res_idx].cpce, 0x06);
884 hdmi_phy_i2c_write(hdmi, mpll_config->res[res_idx].gmp, 0x15);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200885
Russell King3e46f152013-11-04 11:24:00 +0000886 /* CURRCTRL */
Russell King39cc1532015-03-31 18:34:11 +0100887 hdmi_phy_i2c_write(hdmi, curr_ctrl->curr[res_idx], 0x10);
Russell King3e46f152013-11-04 11:24:00 +0000888
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200889 hdmi_phy_i2c_write(hdmi, 0x0000, 0x13); /* PLLPHBYCTRL */
890 hdmi_phy_i2c_write(hdmi, 0x0006, 0x17);
Andy Yanaaa757a2014-12-05 14:25:50 +0800891
Russell King39cc1532015-03-31 18:34:11 +0100892 hdmi_phy_i2c_write(hdmi, phy_config->term, 0x19); /* TXTERM */
893 hdmi_phy_i2c_write(hdmi, phy_config->sym_ctr, 0x09); /* CKSYMTXCTRL */
894 hdmi_phy_i2c_write(hdmi, phy_config->vlev_ctr, 0x0E); /* VLEVCTRL */
Yakir Yang034705a2015-03-31 23:56:10 -0400895
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200896 /* REMOVE CLK TERM */
897 hdmi_phy_i2c_write(hdmi, 0x8000, 0x05); /* CKCALCTRL */
898
Russell King2fada102015-07-28 12:21:34 +0100899 dw_hdmi_phy_enable_powerdown(hdmi, false);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200900
901 /* toggle TMDS enable */
Andy Yanb21f4b62014-12-05 14:26:31 +0800902 dw_hdmi_phy_enable_tmds(hdmi, 0);
903 dw_hdmi_phy_enable_tmds(hdmi, 1);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200904
905 /* gen2 tx power on */
Andy Yanb21f4b62014-12-05 14:26:31 +0800906 dw_hdmi_phy_gen2_txpwron(hdmi, 1);
907 dw_hdmi_phy_gen2_pddq(hdmi, 0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200908
Andy Yan12b9f202015-01-07 15:48:27 +0800909 if (hdmi->dev_type == RK3288_HDMI)
910 dw_hdmi_phy_enable_spare(hdmi, 1);
911
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200912 /*Wait for PHY PLL lock */
913 msec = 5;
914 do {
915 val = hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_TX_PHY_LOCK;
916 if (!val)
917 break;
918
919 if (msec == 0) {
920 dev_err(hdmi->dev, "PHY PLL not locked\n");
921 return -ETIMEDOUT;
922 }
923
924 udelay(1000);
925 msec--;
926 } while (1);
927
928 return 0;
929}
930
Andy Yanb21f4b62014-12-05 14:26:31 +0800931static int dw_hdmi_phy_init(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200932{
933 int i, ret;
Russell King05b13422015-07-21 15:35:52 +0100934 bool cscon;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200935
936 /*check csc whether needed activated in HDMI mode */
Russell King05b13422015-07-21 15:35:52 +0100937 cscon = hdmi->sink_is_hdmi && is_color_space_conversion(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200938
939 /* HDMI Phy spec says to do the phy initialization sequence twice */
940 for (i = 0; i < 2; i++) {
Andy Yanb21f4b62014-12-05 14:26:31 +0800941 dw_hdmi_phy_sel_data_en_pol(hdmi, 1);
942 dw_hdmi_phy_sel_interface_control(hdmi, 0);
943 dw_hdmi_phy_enable_tmds(hdmi, 0);
Russell King2fada102015-07-28 12:21:34 +0100944 dw_hdmi_phy_enable_powerdown(hdmi, true);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200945
946 /* Enable CSC */
947 ret = hdmi_phy_configure(hdmi, 0, 8, cscon);
948 if (ret)
949 return ret;
950 }
951
952 hdmi->phy_enabled = true;
953 return 0;
954}
955
Andy Yanb21f4b62014-12-05 14:26:31 +0800956static void hdmi_tx_hdcp_config(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200957{
Russell King812bc612013-11-04 12:42:02 +0000958 u8 de;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200959
960 if (hdmi->hdmi_data.video_mode.mdataenablepolarity)
961 de = HDMI_A_VIDPOLCFG_DATAENPOL_ACTIVE_HIGH;
962 else
963 de = HDMI_A_VIDPOLCFG_DATAENPOL_ACTIVE_LOW;
964
965 /* disable rx detect */
Russell King812bc612013-11-04 12:42:02 +0000966 hdmi_modb(hdmi, HDMI_A_HDCPCFG0_RXDETECT_DISABLE,
967 HDMI_A_HDCPCFG0_RXDETECT_MASK, HDMI_A_HDCPCFG0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200968
Russell King812bc612013-11-04 12:42:02 +0000969 hdmi_modb(hdmi, de, HDMI_A_VIDPOLCFG_DATAENPOL_MASK, HDMI_A_VIDPOLCFG);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200970
Russell King812bc612013-11-04 12:42:02 +0000971 hdmi_modb(hdmi, HDMI_A_HDCPCFG1_ENCRYPTIONDISABLE_DISABLE,
972 HDMI_A_HDCPCFG1_ENCRYPTIONDISABLE_MASK, HDMI_A_HDCPCFG1);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200973}
974
Russell Kingd4ac4cb2015-03-27 20:06:50 +0000975static void hdmi_config_AVI(struct dw_hdmi *hdmi, struct drm_display_mode *mode)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200976{
Russell Kingd4ac4cb2015-03-27 20:06:50 +0000977 struct hdmi_avi_infoframe frame;
978 u8 val;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200979
Russell Kingd4ac4cb2015-03-27 20:06:50 +0000980 /* Initialise info frame from DRM mode */
981 drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200982
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200983 if (hdmi->hdmi_data.enc_out_format == YCBCR444)
Russell Kingd4ac4cb2015-03-27 20:06:50 +0000984 frame.colorspace = HDMI_COLORSPACE_YUV444;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200985 else if (hdmi->hdmi_data.enc_out_format == YCBCR422_8BITS)
Russell Kingd4ac4cb2015-03-27 20:06:50 +0000986 frame.colorspace = HDMI_COLORSPACE_YUV422;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200987 else
Russell Kingd4ac4cb2015-03-27 20:06:50 +0000988 frame.colorspace = HDMI_COLORSPACE_RGB;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200989
990 /* Set up colorimetry */
991 if (hdmi->hdmi_data.enc_out_format == XVYCC444) {
Russell Kingd4ac4cb2015-03-27 20:06:50 +0000992 frame.colorimetry = HDMI_COLORIMETRY_EXTENDED;
Sachin Kamat5a819ed2014-01-28 10:33:16 +0530993 if (hdmi->hdmi_data.colorimetry == HDMI_COLORIMETRY_ITU_601)
Russell Kingd4ac4cb2015-03-27 20:06:50 +0000994 frame.extended_colorimetry =
995 HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
Sachin Kamat5a819ed2014-01-28 10:33:16 +0530996 else /*hdmi->hdmi_data.colorimetry == HDMI_COLORIMETRY_ITU_709*/
Russell Kingd4ac4cb2015-03-27 20:06:50 +0000997 frame.extended_colorimetry =
998 HDMI_EXTENDED_COLORIMETRY_XV_YCC_709;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200999 } else if (hdmi->hdmi_data.enc_out_format != RGB) {
Russell Kingd083c312015-03-27 23:14:16 +00001000 frame.colorimetry = hdmi->hdmi_data.colorimetry;
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001001 frame.extended_colorimetry = HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001002 } else { /* Carries no data */
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001003 frame.colorimetry = HDMI_COLORIMETRY_NONE;
1004 frame.extended_colorimetry = HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001005 }
1006
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001007 frame.scan_mode = HDMI_SCAN_MODE_NONE;
1008
1009 /*
1010 * The Designware IP uses a different byte format from standard
1011 * AVI info frames, though generally the bits are in the correct
1012 * bytes.
1013 */
1014
1015 /*
1016 * AVI data byte 1 differences: Colorspace in bits 4,5 rather than 5,6,
1017 * active aspect present in bit 6 rather than 4.
1018 */
1019 val = (frame.colorspace & 3) << 4 | (frame.scan_mode & 0x3);
1020 if (frame.active_aspect & 15)
1021 val |= HDMI_FC_AVICONF0_ACTIVE_FMT_INFO_PRESENT;
1022 if (frame.top_bar || frame.bottom_bar)
1023 val |= HDMI_FC_AVICONF0_BAR_DATA_HORIZ_BAR;
1024 if (frame.left_bar || frame.right_bar)
1025 val |= HDMI_FC_AVICONF0_BAR_DATA_VERT_BAR;
1026 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF0);
1027
1028 /* AVI data byte 2 differences: none */
1029 val = ((frame.colorimetry & 0x3) << 6) |
1030 ((frame.picture_aspect & 0x3) << 4) |
1031 (frame.active_aspect & 0xf);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001032 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF1);
1033
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001034 /* AVI data byte 3 differences: none */
1035 val = ((frame.extended_colorimetry & 0x7) << 4) |
1036 ((frame.quantization_range & 0x3) << 2) |
1037 (frame.nups & 0x3);
1038 if (frame.itc)
1039 val |= HDMI_FC_AVICONF2_IT_CONTENT_VALID;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001040 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF2);
1041
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001042 /* AVI data byte 4 differences: none */
1043 val = frame.video_code & 0x7f;
1044 hdmi_writeb(hdmi, val, HDMI_FC_AVIVID);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001045
1046 /* AVI Data Byte 5- set up input and output pixel repetition */
1047 val = (((hdmi->hdmi_data.video_mode.mpixelrepetitioninput + 1) <<
1048 HDMI_FC_PRCONF_INCOMING_PR_FACTOR_OFFSET) &
1049 HDMI_FC_PRCONF_INCOMING_PR_FACTOR_MASK) |
1050 ((hdmi->hdmi_data.video_mode.mpixelrepetitionoutput <<
1051 HDMI_FC_PRCONF_OUTPUT_PR_FACTOR_OFFSET) &
1052 HDMI_FC_PRCONF_OUTPUT_PR_FACTOR_MASK);
1053 hdmi_writeb(hdmi, val, HDMI_FC_PRCONF);
1054
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001055 /*
1056 * AVI data byte 5 differences: content type in 0,1 rather than 4,5,
1057 * ycc range in bits 2,3 rather than 6,7
1058 */
1059 val = ((frame.ycc_quantization_range & 0x3) << 2) |
1060 (frame.content_type & 0x3);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001061 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF3);
1062
1063 /* AVI Data Bytes 6-13 */
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001064 hdmi_writeb(hdmi, frame.top_bar & 0xff, HDMI_FC_AVIETB0);
1065 hdmi_writeb(hdmi, (frame.top_bar >> 8) & 0xff, HDMI_FC_AVIETB1);
1066 hdmi_writeb(hdmi, frame.bottom_bar & 0xff, HDMI_FC_AVISBB0);
1067 hdmi_writeb(hdmi, (frame.bottom_bar >> 8) & 0xff, HDMI_FC_AVISBB1);
1068 hdmi_writeb(hdmi, frame.left_bar & 0xff, HDMI_FC_AVIELB0);
1069 hdmi_writeb(hdmi, (frame.left_bar >> 8) & 0xff, HDMI_FC_AVIELB1);
1070 hdmi_writeb(hdmi, frame.right_bar & 0xff, HDMI_FC_AVISRB0);
1071 hdmi_writeb(hdmi, (frame.right_bar >> 8) & 0xff, HDMI_FC_AVISRB1);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001072}
1073
Andy Yanb21f4b62014-12-05 14:26:31 +08001074static void hdmi_av_composer(struct dw_hdmi *hdmi,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001075 const struct drm_display_mode *mode)
1076{
1077 u8 inv_val;
1078 struct hdmi_vmode *vmode = &hdmi->hdmi_data.video_mode;
1079 int hblank, vblank, h_de_hs, v_de_vs, hsync_len, vsync_len;
Russell Kinge80b9f42015-07-21 11:08:25 +01001080 unsigned int vdisplay;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001081
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001082 vmode->mpixelclock = mode->clock * 1000;
1083
1084 dev_dbg(hdmi->dev, "final pixclk = %d\n", vmode->mpixelclock);
1085
1086 /* Set up HDMI_FC_INVIDCONF */
1087 inv_val = (hdmi->hdmi_data.hdcp_enable ?
1088 HDMI_FC_INVIDCONF_HDCP_KEEPOUT_ACTIVE :
1089 HDMI_FC_INVIDCONF_HDCP_KEEPOUT_INACTIVE);
1090
Russell Kingb91eee82015-03-27 23:27:17 +00001091 inv_val |= mode->flags & DRM_MODE_FLAG_PVSYNC ?
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001092 HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_HIGH :
Russell Kingb91eee82015-03-27 23:27:17 +00001093 HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_LOW;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001094
Russell Kingb91eee82015-03-27 23:27:17 +00001095 inv_val |= mode->flags & DRM_MODE_FLAG_PHSYNC ?
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001096 HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_HIGH :
Russell Kingb91eee82015-03-27 23:27:17 +00001097 HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_LOW;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001098
1099 inv_val |= (vmode->mdataenablepolarity ?
1100 HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_HIGH :
1101 HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_LOW);
1102
1103 if (hdmi->vic == 39)
1104 inv_val |= HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_HIGH;
1105 else
Russell Kingb91eee82015-03-27 23:27:17 +00001106 inv_val |= mode->flags & DRM_MODE_FLAG_INTERLACE ?
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001107 HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_HIGH :
Russell Kingb91eee82015-03-27 23:27:17 +00001108 HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_LOW;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001109
Russell Kingb91eee82015-03-27 23:27:17 +00001110 inv_val |= mode->flags & DRM_MODE_FLAG_INTERLACE ?
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001111 HDMI_FC_INVIDCONF_IN_I_P_INTERLACED :
Russell Kingb91eee82015-03-27 23:27:17 +00001112 HDMI_FC_INVIDCONF_IN_I_P_PROGRESSIVE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001113
Russell King05b13422015-07-21 15:35:52 +01001114 inv_val |= hdmi->sink_is_hdmi ?
1115 HDMI_FC_INVIDCONF_DVI_MODEZ_HDMI_MODE :
1116 HDMI_FC_INVIDCONF_DVI_MODEZ_DVI_MODE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001117
1118 hdmi_writeb(hdmi, inv_val, HDMI_FC_INVIDCONF);
1119
Russell Kinge80b9f42015-07-21 11:08:25 +01001120 vdisplay = mode->vdisplay;
1121 vblank = mode->vtotal - mode->vdisplay;
1122 v_de_vs = mode->vsync_start - mode->vdisplay;
1123 vsync_len = mode->vsync_end - mode->vsync_start;
1124
1125 /*
1126 * When we're setting an interlaced mode, we need
1127 * to adjust the vertical timing to suit.
1128 */
1129 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
1130 vdisplay /= 2;
1131 vblank /= 2;
1132 v_de_vs /= 2;
1133 vsync_len /= 2;
1134 }
1135
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001136 /* Set up horizontal active pixel width */
1137 hdmi_writeb(hdmi, mode->hdisplay >> 8, HDMI_FC_INHACTV1);
1138 hdmi_writeb(hdmi, mode->hdisplay, HDMI_FC_INHACTV0);
1139
1140 /* Set up vertical active lines */
Russell Kinge80b9f42015-07-21 11:08:25 +01001141 hdmi_writeb(hdmi, vdisplay >> 8, HDMI_FC_INVACTV1);
1142 hdmi_writeb(hdmi, vdisplay, HDMI_FC_INVACTV0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001143
1144 /* Set up horizontal blanking pixel region width */
1145 hblank = mode->htotal - mode->hdisplay;
1146 hdmi_writeb(hdmi, hblank >> 8, HDMI_FC_INHBLANK1);
1147 hdmi_writeb(hdmi, hblank, HDMI_FC_INHBLANK0);
1148
1149 /* Set up vertical blanking pixel region width */
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001150 hdmi_writeb(hdmi, vblank, HDMI_FC_INVBLANK);
1151
1152 /* Set up HSYNC active edge delay width (in pixel clks) */
1153 h_de_hs = mode->hsync_start - mode->hdisplay;
1154 hdmi_writeb(hdmi, h_de_hs >> 8, HDMI_FC_HSYNCINDELAY1);
1155 hdmi_writeb(hdmi, h_de_hs, HDMI_FC_HSYNCINDELAY0);
1156
1157 /* Set up VSYNC active edge delay (in lines) */
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001158 hdmi_writeb(hdmi, v_de_vs, HDMI_FC_VSYNCINDELAY);
1159
1160 /* Set up HSYNC active pulse width (in pixel clks) */
1161 hsync_len = mode->hsync_end - mode->hsync_start;
1162 hdmi_writeb(hdmi, hsync_len >> 8, HDMI_FC_HSYNCINWIDTH1);
1163 hdmi_writeb(hdmi, hsync_len, HDMI_FC_HSYNCINWIDTH0);
1164
1165 /* Set up VSYNC active edge delay (in lines) */
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001166 hdmi_writeb(hdmi, vsync_len, HDMI_FC_VSYNCINWIDTH);
1167}
1168
Andy Yanb21f4b62014-12-05 14:26:31 +08001169static void dw_hdmi_phy_disable(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001170{
1171 if (!hdmi->phy_enabled)
1172 return;
1173
Andy Yanb21f4b62014-12-05 14:26:31 +08001174 dw_hdmi_phy_enable_tmds(hdmi, 0);
Russell King2fada102015-07-28 12:21:34 +01001175 dw_hdmi_phy_enable_powerdown(hdmi, true);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001176
1177 hdmi->phy_enabled = false;
1178}
1179
1180/* HDMI Initialization Step B.4 */
Andy Yanb21f4b62014-12-05 14:26:31 +08001181static void dw_hdmi_enable_video_path(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001182{
1183 u8 clkdis;
1184
1185 /* control period minimum duration */
1186 hdmi_writeb(hdmi, 12, HDMI_FC_CTRLDUR);
1187 hdmi_writeb(hdmi, 32, HDMI_FC_EXCTRLDUR);
1188 hdmi_writeb(hdmi, 1, HDMI_FC_EXCTRLSPAC);
1189
1190 /* Set to fill TMDS data channels */
1191 hdmi_writeb(hdmi, 0x0B, HDMI_FC_CH0PREAM);
1192 hdmi_writeb(hdmi, 0x16, HDMI_FC_CH1PREAM);
1193 hdmi_writeb(hdmi, 0x21, HDMI_FC_CH2PREAM);
1194
1195 /* Enable pixel clock and tmds data path */
1196 clkdis = 0x7F;
1197 clkdis &= ~HDMI_MC_CLKDIS_PIXELCLK_DISABLE;
1198 hdmi_writeb(hdmi, clkdis, HDMI_MC_CLKDIS);
1199
1200 clkdis &= ~HDMI_MC_CLKDIS_TMDSCLK_DISABLE;
1201 hdmi_writeb(hdmi, clkdis, HDMI_MC_CLKDIS);
1202
1203 /* Enable csc path */
1204 if (is_color_space_conversion(hdmi)) {
1205 clkdis &= ~HDMI_MC_CLKDIS_CSCCLK_DISABLE;
1206 hdmi_writeb(hdmi, clkdis, HDMI_MC_CLKDIS);
1207 }
1208}
1209
Andy Yanb21f4b62014-12-05 14:26:31 +08001210static void hdmi_enable_audio_clk(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001211{
Russell King812bc612013-11-04 12:42:02 +00001212 hdmi_modb(hdmi, 0, HDMI_MC_CLKDIS_AUDCLK_DISABLE, HDMI_MC_CLKDIS);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001213}
1214
1215/* Workaround to clear the overflow condition */
Andy Yanb21f4b62014-12-05 14:26:31 +08001216static void dw_hdmi_clear_overflow(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001217{
1218 int count;
1219 u8 val;
1220
1221 /* TMDS software reset */
1222 hdmi_writeb(hdmi, (u8)~HDMI_MC_SWRSTZ_TMDSSWRST_REQ, HDMI_MC_SWRSTZ);
1223
1224 val = hdmi_readb(hdmi, HDMI_FC_INVIDCONF);
1225 if (hdmi->dev_type == IMX6DL_HDMI) {
1226 hdmi_writeb(hdmi, val, HDMI_FC_INVIDCONF);
1227 return;
1228 }
1229
1230 for (count = 0; count < 4; count++)
1231 hdmi_writeb(hdmi, val, HDMI_FC_INVIDCONF);
1232}
1233
Andy Yanb21f4b62014-12-05 14:26:31 +08001234static void hdmi_enable_overflow_interrupts(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001235{
1236 hdmi_writeb(hdmi, 0, HDMI_FC_MASK2);
1237 hdmi_writeb(hdmi, 0, HDMI_IH_MUTE_FC_STAT2);
1238}
1239
Andy Yanb21f4b62014-12-05 14:26:31 +08001240static void hdmi_disable_overflow_interrupts(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001241{
1242 hdmi_writeb(hdmi, HDMI_IH_MUTE_FC_STAT2_OVERFLOW_MASK,
1243 HDMI_IH_MUTE_FC_STAT2);
1244}
1245
Andy Yanb21f4b62014-12-05 14:26:31 +08001246static int dw_hdmi_setup(struct dw_hdmi *hdmi, struct drm_display_mode *mode)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001247{
1248 int ret;
1249
1250 hdmi_disable_overflow_interrupts(hdmi);
1251
1252 hdmi->vic = drm_match_cea_mode(mode);
1253
1254 if (!hdmi->vic) {
1255 dev_dbg(hdmi->dev, "Non-CEA mode used in HDMI\n");
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001256 } else {
1257 dev_dbg(hdmi->dev, "CEA mode used vic=%d\n", hdmi->vic);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001258 }
1259
1260 if ((hdmi->vic == 6) || (hdmi->vic == 7) ||
Andy Yanb5878332014-12-05 14:23:52 +08001261 (hdmi->vic == 21) || (hdmi->vic == 22) ||
1262 (hdmi->vic == 2) || (hdmi->vic == 3) ||
1263 (hdmi->vic == 17) || (hdmi->vic == 18))
Sachin Kamat5a819ed2014-01-28 10:33:16 +05301264 hdmi->hdmi_data.colorimetry = HDMI_COLORIMETRY_ITU_601;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001265 else
Sachin Kamat5a819ed2014-01-28 10:33:16 +05301266 hdmi->hdmi_data.colorimetry = HDMI_COLORIMETRY_ITU_709;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001267
Russell Kingd10ca822015-07-21 11:25:00 +01001268 hdmi->hdmi_data.video_mode.mpixelrepetitionoutput = 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001269 hdmi->hdmi_data.video_mode.mpixelrepetitioninput = 0;
1270
1271 /* TODO: Get input format from IPU (via FB driver interface) */
1272 hdmi->hdmi_data.enc_in_format = RGB;
1273
1274 hdmi->hdmi_data.enc_out_format = RGB;
1275
1276 hdmi->hdmi_data.enc_color_depth = 8;
1277 hdmi->hdmi_data.pix_repet_factor = 0;
1278 hdmi->hdmi_data.hdcp_enable = 0;
1279 hdmi->hdmi_data.video_mode.mdataenablepolarity = true;
1280
1281 /* HDMI Initialization Step B.1 */
1282 hdmi_av_composer(hdmi, mode);
1283
1284 /* HDMI Initializateion Step B.2 */
Andy Yanb21f4b62014-12-05 14:26:31 +08001285 ret = dw_hdmi_phy_init(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001286 if (ret)
1287 return ret;
1288
1289 /* HDMI Initialization Step B.3 */
Andy Yanb21f4b62014-12-05 14:26:31 +08001290 dw_hdmi_enable_video_path(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001291
Russell Kingf709ec02015-07-21 16:09:39 +01001292 if (hdmi->sink_has_audio) {
1293 dev_dbg(hdmi->dev, "sink has audio support\n");
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001294
1295 /* HDMI Initialization Step E - Configure audio */
1296 hdmi_clk_regenerator_update_pixel_clock(hdmi);
1297 hdmi_enable_audio_clk(hdmi);
Russell Kingf709ec02015-07-21 16:09:39 +01001298 }
1299
1300 /* not for DVI mode */
1301 if (hdmi->sink_is_hdmi) {
1302 dev_dbg(hdmi->dev, "%s HDMI mode\n", __func__);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001303
1304 /* HDMI Initialization Step F - Configure AVI InfoFrame */
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001305 hdmi_config_AVI(hdmi, mode);
Russell King05b13422015-07-21 15:35:52 +01001306 } else {
1307 dev_dbg(hdmi->dev, "%s DVI mode\n", __func__);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001308 }
1309
1310 hdmi_video_packetize(hdmi);
1311 hdmi_video_csc(hdmi);
1312 hdmi_video_sample(hdmi);
1313 hdmi_tx_hdcp_config(hdmi);
1314
Andy Yanb21f4b62014-12-05 14:26:31 +08001315 dw_hdmi_clear_overflow(hdmi);
Russell King05b13422015-07-21 15:35:52 +01001316 if (hdmi->cable_plugin && hdmi->sink_is_hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001317 hdmi_enable_overflow_interrupts(hdmi);
1318
1319 return 0;
1320}
1321
1322/* Wait until we are registered to enable interrupts */
Andy Yanb21f4b62014-12-05 14:26:31 +08001323static int dw_hdmi_fb_registered(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001324{
1325 hdmi_writeb(hdmi, HDMI_PHY_I2CM_INT_ADDR_DONE_POL,
1326 HDMI_PHY_I2CM_INT_ADDR);
1327
1328 hdmi_writeb(hdmi, HDMI_PHY_I2CM_CTLINT_ADDR_NAC_POL |
1329 HDMI_PHY_I2CM_CTLINT_ADDR_ARBITRATION_POL,
1330 HDMI_PHY_I2CM_CTLINT_ADDR);
1331
1332 /* enable cable hot plug irq */
Russell Kingaeac23b2015-06-05 13:46:22 +01001333 hdmi_writeb(hdmi, hdmi->phy_mask, HDMI_PHY_MASK0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001334
1335 /* Clear Hotplug interrupts */
Russell Kingaeac23b2015-06-05 13:46:22 +01001336 hdmi_writeb(hdmi, HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE,
1337 HDMI_IH_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001338
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001339 return 0;
1340}
1341
Andy Yanb21f4b62014-12-05 14:26:31 +08001342static void initialize_hdmi_ih_mutes(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001343{
1344 u8 ih_mute;
1345
1346 /*
1347 * Boot up defaults are:
1348 * HDMI_IH_MUTE = 0x03 (disabled)
1349 * HDMI_IH_MUTE_* = 0x00 (enabled)
1350 *
1351 * Disable top level interrupt bits in HDMI block
1352 */
1353 ih_mute = hdmi_readb(hdmi, HDMI_IH_MUTE) |
1354 HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |
1355 HDMI_IH_MUTE_MUTE_ALL_INTERRUPT;
1356
1357 hdmi_writeb(hdmi, ih_mute, HDMI_IH_MUTE);
1358
1359 /* by default mask all interrupts */
1360 hdmi_writeb(hdmi, 0xff, HDMI_VP_MASK);
1361 hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK0);
1362 hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK1);
1363 hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK2);
1364 hdmi_writeb(hdmi, 0xff, HDMI_PHY_MASK0);
1365 hdmi_writeb(hdmi, 0xff, HDMI_PHY_I2CM_INT_ADDR);
1366 hdmi_writeb(hdmi, 0xff, HDMI_PHY_I2CM_CTLINT_ADDR);
1367 hdmi_writeb(hdmi, 0xff, HDMI_AUD_INT);
1368 hdmi_writeb(hdmi, 0xff, HDMI_AUD_SPDIFINT);
1369 hdmi_writeb(hdmi, 0xff, HDMI_AUD_HBR_MASK);
1370 hdmi_writeb(hdmi, 0xff, HDMI_GP_MASK);
1371 hdmi_writeb(hdmi, 0xff, HDMI_A_APIINTMSK);
1372 hdmi_writeb(hdmi, 0xff, HDMI_CEC_MASK);
1373 hdmi_writeb(hdmi, 0xff, HDMI_I2CM_INT);
1374 hdmi_writeb(hdmi, 0xff, HDMI_I2CM_CTLINT);
1375
1376 /* Disable interrupts in the IH_MUTE_* registers */
1377 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT0);
1378 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT1);
1379 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT2);
1380 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_AS_STAT0);
1381 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_PHY_STAT0);
1382 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_I2CM_STAT0);
1383 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_CEC_STAT0);
1384 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_VP_STAT0);
1385 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_I2CMPHY_STAT0);
1386 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_AHBDMAAUD_STAT0);
1387
1388 /* Enable top level interrupt bits in HDMI block */
1389 ih_mute &= ~(HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |
1390 HDMI_IH_MUTE_MUTE_ALL_INTERRUPT);
1391 hdmi_writeb(hdmi, ih_mute, HDMI_IH_MUTE);
1392}
1393
Andy Yanb21f4b62014-12-05 14:26:31 +08001394static void dw_hdmi_poweron(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001395{
Russell King381f05a2015-06-05 15:25:08 +01001396 hdmi->bridge_is_on = true;
Andy Yanb21f4b62014-12-05 14:26:31 +08001397 dw_hdmi_setup(hdmi, &hdmi->previous_mode);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001398}
1399
Andy Yanb21f4b62014-12-05 14:26:31 +08001400static void dw_hdmi_poweroff(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001401{
Andy Yanb21f4b62014-12-05 14:26:31 +08001402 dw_hdmi_phy_disable(hdmi);
Russell King381f05a2015-06-05 15:25:08 +01001403 hdmi->bridge_is_on = false;
1404}
1405
1406static void dw_hdmi_update_power(struct dw_hdmi *hdmi)
1407{
1408 int force = hdmi->force;
1409
1410 if (hdmi->disabled) {
1411 force = DRM_FORCE_OFF;
1412 } else if (force == DRM_FORCE_UNSPECIFIED) {
Russell Kingaeac23b2015-06-05 13:46:22 +01001413 if (hdmi->rxsense)
Russell King381f05a2015-06-05 15:25:08 +01001414 force = DRM_FORCE_ON;
1415 else
1416 force = DRM_FORCE_OFF;
1417 }
1418
1419 if (force == DRM_FORCE_OFF) {
1420 if (hdmi->bridge_is_on)
1421 dw_hdmi_poweroff(hdmi);
1422 } else {
1423 if (!hdmi->bridge_is_on)
1424 dw_hdmi_poweron(hdmi);
1425 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001426}
1427
Russell Kingaeac23b2015-06-05 13:46:22 +01001428/*
1429 * Adjust the detection of RXSENSE according to whether we have a forced
1430 * connection mode enabled, or whether we have been disabled. There is
1431 * no point processing RXSENSE interrupts if we have a forced connection
1432 * state, or DRM has us disabled.
1433 *
1434 * We also disable rxsense interrupts when we think we're disconnected
1435 * to avoid floating TDMS signals giving false rxsense interrupts.
1436 *
1437 * Note: we still need to listen for HPD interrupts even when DRM has us
1438 * disabled so that we can detect a connect event.
1439 */
1440static void dw_hdmi_update_phy_mask(struct dw_hdmi *hdmi)
1441{
1442 u8 old_mask = hdmi->phy_mask;
1443
1444 if (hdmi->force || hdmi->disabled || !hdmi->rxsense)
1445 hdmi->phy_mask |= HDMI_PHY_RX_SENSE;
1446 else
1447 hdmi->phy_mask &= ~HDMI_PHY_RX_SENSE;
1448
1449 if (old_mask != hdmi->phy_mask)
1450 hdmi_writeb(hdmi, hdmi->phy_mask, HDMI_PHY_MASK0);
1451}
1452
Andy Yanb21f4b62014-12-05 14:26:31 +08001453static void dw_hdmi_bridge_mode_set(struct drm_bridge *bridge,
Steve Longerbeameb10d632014-12-18 18:00:24 -08001454 struct drm_display_mode *orig_mode,
1455 struct drm_display_mode *mode)
Andy Yan3d1b35a2014-12-05 14:25:05 +08001456{
Andy Yanb21f4b62014-12-05 14:26:31 +08001457 struct dw_hdmi *hdmi = bridge->driver_private;
Andy Yan3d1b35a2014-12-05 14:25:05 +08001458
Russell Kingb872a8e2015-06-05 12:22:46 +01001459 mutex_lock(&hdmi->mutex);
Andy Yan3d1b35a2014-12-05 14:25:05 +08001460
1461 /* Store the display mode for plugin/DKMS poweron events */
1462 memcpy(&hdmi->previous_mode, mode, sizeof(hdmi->previous_mode));
Russell Kingb872a8e2015-06-05 12:22:46 +01001463
1464 mutex_unlock(&hdmi->mutex);
Andy Yan3d1b35a2014-12-05 14:25:05 +08001465}
1466
Andy Yanb21f4b62014-12-05 14:26:31 +08001467static bool dw_hdmi_bridge_mode_fixup(struct drm_bridge *bridge,
1468 const struct drm_display_mode *mode,
1469 struct drm_display_mode *adjusted_mode)
Andy Yan3d1b35a2014-12-05 14:25:05 +08001470{
1471 return true;
1472}
1473
Andy Yanb21f4b62014-12-05 14:26:31 +08001474static void dw_hdmi_bridge_disable(struct drm_bridge *bridge)
Andy Yan3d1b35a2014-12-05 14:25:05 +08001475{
Andy Yanb21f4b62014-12-05 14:26:31 +08001476 struct dw_hdmi *hdmi = bridge->driver_private;
Andy Yan3d1b35a2014-12-05 14:25:05 +08001477
Russell Kingb872a8e2015-06-05 12:22:46 +01001478 mutex_lock(&hdmi->mutex);
1479 hdmi->disabled = true;
Russell King381f05a2015-06-05 15:25:08 +01001480 dw_hdmi_update_power(hdmi);
Russell Kingaeac23b2015-06-05 13:46:22 +01001481 dw_hdmi_update_phy_mask(hdmi);
Russell Kingb872a8e2015-06-05 12:22:46 +01001482 mutex_unlock(&hdmi->mutex);
Andy Yan3d1b35a2014-12-05 14:25:05 +08001483}
1484
Andy Yanb21f4b62014-12-05 14:26:31 +08001485static void dw_hdmi_bridge_enable(struct drm_bridge *bridge)
Andy Yan3d1b35a2014-12-05 14:25:05 +08001486{
Andy Yanb21f4b62014-12-05 14:26:31 +08001487 struct dw_hdmi *hdmi = bridge->driver_private;
Andy Yan3d1b35a2014-12-05 14:25:05 +08001488
Russell Kingb872a8e2015-06-05 12:22:46 +01001489 mutex_lock(&hdmi->mutex);
Russell Kingb872a8e2015-06-05 12:22:46 +01001490 hdmi->disabled = false;
Russell King381f05a2015-06-05 15:25:08 +01001491 dw_hdmi_update_power(hdmi);
Russell Kingaeac23b2015-06-05 13:46:22 +01001492 dw_hdmi_update_phy_mask(hdmi);
Russell Kingb872a8e2015-06-05 12:22:46 +01001493 mutex_unlock(&hdmi->mutex);
Andy Yan3d1b35a2014-12-05 14:25:05 +08001494}
1495
Andy Yanb21f4b62014-12-05 14:26:31 +08001496static void dw_hdmi_bridge_nop(struct drm_bridge *bridge)
Andy Yan3d1b35a2014-12-05 14:25:05 +08001497{
1498 /* do nothing */
1499}
1500
Andy Yanb21f4b62014-12-05 14:26:31 +08001501static enum drm_connector_status
1502dw_hdmi_connector_detect(struct drm_connector *connector, bool force)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001503{
Andy Yanb21f4b62014-12-05 14:26:31 +08001504 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
Russell Kingd94905e2013-11-03 22:23:24 +00001505 connector);
Russell King98dbead2014-04-18 10:46:45 +01001506
Russell King381f05a2015-06-05 15:25:08 +01001507 mutex_lock(&hdmi->mutex);
1508 hdmi->force = DRM_FORCE_UNSPECIFIED;
1509 dw_hdmi_update_power(hdmi);
Russell Kingaeac23b2015-06-05 13:46:22 +01001510 dw_hdmi_update_phy_mask(hdmi);
Russell King381f05a2015-06-05 15:25:08 +01001511 mutex_unlock(&hdmi->mutex);
1512
Russell King98dbead2014-04-18 10:46:45 +01001513 return hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_HPD ?
1514 connector_status_connected : connector_status_disconnected;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001515}
1516
Andy Yanb21f4b62014-12-05 14:26:31 +08001517static int dw_hdmi_connector_get_modes(struct drm_connector *connector)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001518{
Andy Yanb21f4b62014-12-05 14:26:31 +08001519 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001520 connector);
1521 struct edid *edid;
Doug Anderson6c7e66e2015-06-04 11:04:36 -07001522 int ret = 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001523
1524 if (!hdmi->ddc)
1525 return 0;
1526
1527 edid = drm_get_edid(connector, hdmi->ddc);
1528 if (edid) {
1529 dev_dbg(hdmi->dev, "got edid: width[%d] x height[%d]\n",
1530 edid->width_cm, edid->height_cm);
1531
Russell King05b13422015-07-21 15:35:52 +01001532 hdmi->sink_is_hdmi = drm_detect_hdmi_monitor(edid);
Russell Kingf709ec02015-07-21 16:09:39 +01001533 hdmi->sink_has_audio = drm_detect_monitor_audio(edid);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001534 drm_mode_connector_update_edid_property(connector, edid);
1535 ret = drm_add_edid_modes(connector, edid);
Russell Kingf5ce4052013-11-07 16:06:01 +00001536 /* Store the ELD */
1537 drm_edid_to_eld(connector, edid);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001538 kfree(edid);
1539 } else {
1540 dev_dbg(hdmi->dev, "failed to get edid\n");
1541 }
1542
Doug Anderson6c7e66e2015-06-04 11:04:36 -07001543 return ret;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001544}
1545
Andy Yan632d0352014-12-05 14:30:21 +08001546static enum drm_mode_status
1547dw_hdmi_connector_mode_valid(struct drm_connector *connector,
1548 struct drm_display_mode *mode)
1549{
1550 struct dw_hdmi *hdmi = container_of(connector,
1551 struct dw_hdmi, connector);
1552 enum drm_mode_status mode_status = MODE_OK;
1553
Russell King8add4192015-07-22 11:14:00 +01001554 /* We don't support double-clocked modes */
1555 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
1556 return MODE_BAD;
1557
Andy Yan632d0352014-12-05 14:30:21 +08001558 if (hdmi->plat_data->mode_valid)
1559 mode_status = hdmi->plat_data->mode_valid(connector, mode);
1560
1561 return mode_status;
1562}
1563
Andy Yanb21f4b62014-12-05 14:26:31 +08001564static struct drm_encoder *dw_hdmi_connector_best_encoder(struct drm_connector
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001565 *connector)
1566{
Andy Yanb21f4b62014-12-05 14:26:31 +08001567 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001568 connector);
1569
Andy Yan3d1b35a2014-12-05 14:25:05 +08001570 return hdmi->encoder;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001571}
1572
Andy Yanb21f4b62014-12-05 14:26:31 +08001573static void dw_hdmi_connector_destroy(struct drm_connector *connector)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001574{
Andy Yan3d1b35a2014-12-05 14:25:05 +08001575 drm_connector_unregister(connector);
1576 drm_connector_cleanup(connector);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001577}
1578
Russell King381f05a2015-06-05 15:25:08 +01001579static void dw_hdmi_connector_force(struct drm_connector *connector)
1580{
1581 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
1582 connector);
1583
1584 mutex_lock(&hdmi->mutex);
1585 hdmi->force = connector->force;
1586 dw_hdmi_update_power(hdmi);
Russell Kingaeac23b2015-06-05 13:46:22 +01001587 dw_hdmi_update_phy_mask(hdmi);
Russell King381f05a2015-06-05 15:25:08 +01001588 mutex_unlock(&hdmi->mutex);
1589}
1590
Andy Yanb21f4b62014-12-05 14:26:31 +08001591static struct drm_connector_funcs dw_hdmi_connector_funcs = {
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001592 .dpms = drm_helper_connector_dpms,
1593 .fill_modes = drm_helper_probe_single_connector_modes,
Andy Yanb21f4b62014-12-05 14:26:31 +08001594 .detect = dw_hdmi_connector_detect,
1595 .destroy = dw_hdmi_connector_destroy,
Russell King381f05a2015-06-05 15:25:08 +01001596 .force = dw_hdmi_connector_force,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001597};
1598
Andy Yanb21f4b62014-12-05 14:26:31 +08001599static struct drm_connector_helper_funcs dw_hdmi_connector_helper_funcs = {
1600 .get_modes = dw_hdmi_connector_get_modes,
Andy Yan632d0352014-12-05 14:30:21 +08001601 .mode_valid = dw_hdmi_connector_mode_valid,
Andy Yanb21f4b62014-12-05 14:26:31 +08001602 .best_encoder = dw_hdmi_connector_best_encoder,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001603};
1604
Fabio Estevamcf88fca2015-04-02 19:11:04 -03001605static struct drm_bridge_funcs dw_hdmi_bridge_funcs = {
Andy Yanb21f4b62014-12-05 14:26:31 +08001606 .enable = dw_hdmi_bridge_enable,
1607 .disable = dw_hdmi_bridge_disable,
1608 .pre_enable = dw_hdmi_bridge_nop,
1609 .post_disable = dw_hdmi_bridge_nop,
1610 .mode_set = dw_hdmi_bridge_mode_set,
1611 .mode_fixup = dw_hdmi_bridge_mode_fixup,
Andy Yan3d1b35a2014-12-05 14:25:05 +08001612};
1613
Andy Yanb21f4b62014-12-05 14:26:31 +08001614static irqreturn_t dw_hdmi_hardirq(int irq, void *dev_id)
Russell Kingd94905e2013-11-03 22:23:24 +00001615{
Andy Yanb21f4b62014-12-05 14:26:31 +08001616 struct dw_hdmi *hdmi = dev_id;
Russell Kingd94905e2013-11-03 22:23:24 +00001617 u8 intr_stat;
1618
1619 intr_stat = hdmi_readb(hdmi, HDMI_IH_PHY_STAT0);
1620 if (intr_stat)
1621 hdmi_writeb(hdmi, ~0, HDMI_IH_MUTE_PHY_STAT0);
1622
1623 return intr_stat ? IRQ_WAKE_THREAD : IRQ_NONE;
1624}
1625
Andy Yanb21f4b62014-12-05 14:26:31 +08001626static irqreturn_t dw_hdmi_irq(int irq, void *dev_id)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001627{
Andy Yanb21f4b62014-12-05 14:26:31 +08001628 struct dw_hdmi *hdmi = dev_id;
Russell Kingaeac23b2015-06-05 13:46:22 +01001629 u8 intr_stat, phy_int_pol, phy_pol_mask, phy_stat;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001630
1631 intr_stat = hdmi_readb(hdmi, HDMI_IH_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001632 phy_int_pol = hdmi_readb(hdmi, HDMI_PHY_POL0);
Russell Kingaeac23b2015-06-05 13:46:22 +01001633 phy_stat = hdmi_readb(hdmi, HDMI_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001634
Russell Kingaeac23b2015-06-05 13:46:22 +01001635 phy_pol_mask = 0;
1636 if (intr_stat & HDMI_IH_PHY_STAT0_HPD)
1637 phy_pol_mask |= HDMI_PHY_HPD;
1638 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE0)
1639 phy_pol_mask |= HDMI_PHY_RX_SENSE0;
1640 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE1)
1641 phy_pol_mask |= HDMI_PHY_RX_SENSE1;
1642 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE2)
1643 phy_pol_mask |= HDMI_PHY_RX_SENSE2;
1644 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE3)
1645 phy_pol_mask |= HDMI_PHY_RX_SENSE3;
1646
1647 if (phy_pol_mask)
1648 hdmi_modb(hdmi, ~phy_int_pol, phy_pol_mask, HDMI_PHY_POL0);
1649
1650 /*
1651 * RX sense tells us whether the TDMS transmitters are detecting
1652 * load - in other words, there's something listening on the
1653 * other end of the link. Use this to decide whether we should
1654 * power on the phy as HPD may be toggled by the sink to merely
1655 * ask the source to re-read the EDID.
1656 */
1657 if (intr_stat &
1658 (HDMI_IH_PHY_STAT0_RX_SENSE | HDMI_IH_PHY_STAT0_HPD)) {
Russell Kingb872a8e2015-06-05 12:22:46 +01001659 mutex_lock(&hdmi->mutex);
Russell Kingaeac23b2015-06-05 13:46:22 +01001660 if (!hdmi->disabled && !hdmi->force) {
1661 /*
1662 * If the RX sense status indicates we're disconnected,
1663 * clear the software rxsense status.
1664 */
1665 if (!(phy_stat & HDMI_PHY_RX_SENSE))
1666 hdmi->rxsense = false;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001667
Russell Kingaeac23b2015-06-05 13:46:22 +01001668 /*
1669 * Only set the software rxsense status when both
1670 * rxsense and hpd indicates we're connected.
1671 * This avoids what seems to be bad behaviour in
1672 * at least iMX6S versions of the phy.
1673 */
1674 if (phy_stat & HDMI_PHY_HPD)
1675 hdmi->rxsense = true;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001676
Russell Kingaeac23b2015-06-05 13:46:22 +01001677 dw_hdmi_update_power(hdmi);
1678 dw_hdmi_update_phy_mask(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001679 }
Russell Kingb872a8e2015-06-05 12:22:46 +01001680 mutex_unlock(&hdmi->mutex);
Russell Kingaeac23b2015-06-05 13:46:22 +01001681 }
1682
1683 if (intr_stat & HDMI_IH_PHY_STAT0_HPD) {
1684 dev_dbg(hdmi->dev, "EVENT=%s\n",
1685 phy_int_pol & HDMI_PHY_HPD ? "plugin" : "plugout");
Russell King4b9bcaa2015-06-06 00:12:41 +01001686 drm_helper_hpd_irq_event(hdmi->bridge->dev);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001687 }
1688
1689 hdmi_writeb(hdmi, intr_stat, HDMI_IH_PHY_STAT0);
Russell Kingaeac23b2015-06-05 13:46:22 +01001690 hdmi_writeb(hdmi, ~(HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE),
1691 HDMI_IH_MUTE_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001692
1693 return IRQ_HANDLED;
1694}
1695
Andy Yanb21f4b62014-12-05 14:26:31 +08001696static int dw_hdmi_register(struct drm_device *drm, struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001697{
Andy Yan3d1b35a2014-12-05 14:25:05 +08001698 struct drm_encoder *encoder = hdmi->encoder;
1699 struct drm_bridge *bridge;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001700 int ret;
1701
Andy Yan3d1b35a2014-12-05 14:25:05 +08001702 bridge = devm_kzalloc(drm->dev, sizeof(*bridge), GFP_KERNEL);
1703 if (!bridge) {
1704 DRM_ERROR("Failed to allocate drm bridge\n");
1705 return -ENOMEM;
1706 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001707
Andy Yan3d1b35a2014-12-05 14:25:05 +08001708 hdmi->bridge = bridge;
1709 bridge->driver_private = hdmi;
Fabio Estevamb5217bf2015-01-27 10:21:49 -02001710 bridge->funcs = &dw_hdmi_bridge_funcs;
1711 ret = drm_bridge_attach(drm, bridge);
Andy Yan3d1b35a2014-12-05 14:25:05 +08001712 if (ret) {
1713 DRM_ERROR("Failed to initialize bridge with drm\n");
1714 return -EINVAL;
1715 }
1716
1717 encoder->bridge = bridge;
Russell Kingd94905e2013-11-03 22:23:24 +00001718 hdmi->connector.polled = DRM_CONNECTOR_POLL_HPD;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001719
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001720 drm_connector_helper_add(&hdmi->connector,
Andy Yanb21f4b62014-12-05 14:26:31 +08001721 &dw_hdmi_connector_helper_funcs);
1722 drm_connector_init(drm, &hdmi->connector, &dw_hdmi_connector_funcs,
Russell King1b3f7672013-11-03 13:30:48 +00001723 DRM_MODE_CONNECTOR_HDMIA);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001724
Andy Yan3d1b35a2014-12-05 14:25:05 +08001725 hdmi->connector.encoder = encoder;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001726
Andy Yan3d1b35a2014-12-05 14:25:05 +08001727 drm_mode_connector_attach_encoder(&hdmi->connector, encoder);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001728
1729 return 0;
1730}
1731
Andy Yanb21f4b62014-12-05 14:26:31 +08001732int dw_hdmi_bind(struct device *dev, struct device *master,
Andy Yan3d1b35a2014-12-05 14:25:05 +08001733 void *data, struct drm_encoder *encoder,
1734 struct resource *iores, int irq,
1735 const struct dw_hdmi_plat_data *plat_data)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001736{
Russell King1b3f7672013-11-03 13:30:48 +00001737 struct drm_device *drm = data;
Russell King17b50012013-11-03 11:23:34 +00001738 struct device_node *np = dev->of_node;
Russell King7ed6c662013-11-07 16:01:45 +00001739 struct platform_device_info pdevinfo;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001740 struct device_node *ddc_node;
Russell King7ed6c662013-11-07 16:01:45 +00001741 struct dw_hdmi_audio_data audio;
Andy Yanb21f4b62014-12-05 14:26:31 +08001742 struct dw_hdmi *hdmi;
Andy Yan3d1b35a2014-12-05 14:25:05 +08001743 int ret;
Andy Yan0cd9d142014-12-05 14:28:24 +08001744 u32 val = 1;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001745
Russell King17b50012013-11-03 11:23:34 +00001746 hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001747 if (!hdmi)
1748 return -ENOMEM;
1749
Russell Kinge80b9f42015-07-21 11:08:25 +01001750 hdmi->connector.interlace_allowed = 1;
1751
Andy Yan3d1b35a2014-12-05 14:25:05 +08001752 hdmi->plat_data = plat_data;
Russell King17b50012013-11-03 11:23:34 +00001753 hdmi->dev = dev;
Andy Yan3d1b35a2014-12-05 14:25:05 +08001754 hdmi->dev_type = plat_data->dev_type;
Russell King40678382013-11-07 15:35:06 +00001755 hdmi->sample_rate = 48000;
1756 hdmi->ratio = 100;
Andy Yan3d1b35a2014-12-05 14:25:05 +08001757 hdmi->encoder = encoder;
Russell Kingb872a8e2015-06-05 12:22:46 +01001758 hdmi->disabled = true;
Russell Kingaeac23b2015-06-05 13:46:22 +01001759 hdmi->rxsense = true;
1760 hdmi->phy_mask = (u8)~(HDMI_PHY_HPD | HDMI_PHY_RX_SENSE);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001761
Russell Kingb872a8e2015-06-05 12:22:46 +01001762 mutex_init(&hdmi->mutex);
Russell King6bcf4952015-02-02 11:01:08 +00001763 mutex_init(&hdmi->audio_mutex);
Russell Kingb90120a2015-03-27 12:59:58 +00001764 spin_lock_init(&hdmi->audio_lock);
Russell King6bcf4952015-02-02 11:01:08 +00001765
Andy Yan0cd9d142014-12-05 14:28:24 +08001766 of_property_read_u32(np, "reg-io-width", &val);
1767
1768 switch (val) {
1769 case 4:
1770 hdmi->write = dw_hdmi_writel;
1771 hdmi->read = dw_hdmi_readl;
1772 break;
1773 case 1:
1774 hdmi->write = dw_hdmi_writeb;
1775 hdmi->read = dw_hdmi_readb;
1776 break;
1777 default:
1778 dev_err(dev, "reg-io-width must be 1 or 4\n");
1779 return -EINVAL;
1780 }
1781
Philipp Zabelb5d45902014-03-05 10:20:56 +01001782 ddc_node = of_parse_phandle(np, "ddc-i2c-bus", 0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001783 if (ddc_node) {
1784 hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001785 of_node_put(ddc_node);
Andy Yanc2c38482014-12-05 14:24:28 +08001786 if (!hdmi->ddc) {
1787 dev_dbg(hdmi->dev, "failed to read ddc node\n");
1788 return -EPROBE_DEFER;
1789 }
1790
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001791 } else {
1792 dev_dbg(hdmi->dev, "no ddc property found\n");
1793 }
1794
Russell King17b50012013-11-03 11:23:34 +00001795 hdmi->regs = devm_ioremap_resource(dev, iores);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001796 if (IS_ERR(hdmi->regs))
1797 return PTR_ERR(hdmi->regs);
1798
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001799 hdmi->isfr_clk = devm_clk_get(hdmi->dev, "isfr");
1800 if (IS_ERR(hdmi->isfr_clk)) {
1801 ret = PTR_ERR(hdmi->isfr_clk);
Andy Yanb5878332014-12-05 14:23:52 +08001802 dev_err(hdmi->dev, "Unable to get HDMI isfr clk: %d\n", ret);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001803 return ret;
1804 }
1805
1806 ret = clk_prepare_enable(hdmi->isfr_clk);
1807 if (ret) {
Andy Yanb5878332014-12-05 14:23:52 +08001808 dev_err(hdmi->dev, "Cannot enable HDMI isfr clock: %d\n", ret);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001809 return ret;
1810 }
1811
1812 hdmi->iahb_clk = devm_clk_get(hdmi->dev, "iahb");
1813 if (IS_ERR(hdmi->iahb_clk)) {
1814 ret = PTR_ERR(hdmi->iahb_clk);
Andy Yanb5878332014-12-05 14:23:52 +08001815 dev_err(hdmi->dev, "Unable to get HDMI iahb clk: %d\n", ret);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001816 goto err_isfr;
1817 }
1818
1819 ret = clk_prepare_enable(hdmi->iahb_clk);
1820 if (ret) {
Andy Yanb5878332014-12-05 14:23:52 +08001821 dev_err(hdmi->dev, "Cannot enable HDMI iahb clock: %d\n", ret);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001822 goto err_isfr;
1823 }
1824
1825 /* Product and revision IDs */
Russell King17b50012013-11-03 11:23:34 +00001826 dev_info(dev,
Andy Yanb5878332014-12-05 14:23:52 +08001827 "Detected HDMI controller 0x%x:0x%x:0x%x:0x%x\n",
1828 hdmi_readb(hdmi, HDMI_DESIGN_ID),
1829 hdmi_readb(hdmi, HDMI_REVISION_ID),
1830 hdmi_readb(hdmi, HDMI_PRODUCT_ID0),
1831 hdmi_readb(hdmi, HDMI_PRODUCT_ID1));
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001832
1833 initialize_hdmi_ih_mutes(hdmi);
1834
Philipp Zabel639a2022015-01-07 13:43:50 +01001835 ret = devm_request_threaded_irq(dev, irq, dw_hdmi_hardirq,
1836 dw_hdmi_irq, IRQF_SHARED,
1837 dev_name(dev), hdmi);
1838 if (ret)
Fabio Estevamb33ef612015-01-27 10:54:12 -02001839 goto err_iahb;
Philipp Zabel639a2022015-01-07 13:43:50 +01001840
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001841 /*
1842 * To prevent overflows in HDMI_IH_FC_STAT2, set the clk regenerator
1843 * N and cts values before enabling phy
1844 */
1845 hdmi_init_clk_regenerator(hdmi);
1846
1847 /*
1848 * Configure registers related to HDMI interrupt
1849 * generation before registering IRQ.
1850 */
Russell Kingaeac23b2015-06-05 13:46:22 +01001851 hdmi_writeb(hdmi, HDMI_PHY_HPD | HDMI_PHY_RX_SENSE, HDMI_PHY_POL0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001852
1853 /* Clear Hotplug interrupts */
Russell Kingaeac23b2015-06-05 13:46:22 +01001854 hdmi_writeb(hdmi, HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE,
1855 HDMI_IH_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001856
Andy Yanb21f4b62014-12-05 14:26:31 +08001857 ret = dw_hdmi_fb_registered(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001858 if (ret)
1859 goto err_iahb;
1860
Andy Yanb21f4b62014-12-05 14:26:31 +08001861 ret = dw_hdmi_register(drm, hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001862 if (ret)
1863 goto err_iahb;
1864
Russell Kingd94905e2013-11-03 22:23:24 +00001865 /* Unmute interrupts */
Russell Kingaeac23b2015-06-05 13:46:22 +01001866 hdmi_writeb(hdmi, ~(HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE),
1867 HDMI_IH_MUTE_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001868
Russell King7ed6c662013-11-07 16:01:45 +00001869 memset(&pdevinfo, 0, sizeof(pdevinfo));
1870 pdevinfo.parent = dev;
1871 pdevinfo.id = PLATFORM_DEVID_AUTO;
1872
1873 if (hdmi_readb(hdmi, HDMI_CONFIG1_ID) & HDMI_CONFIG1_AHB) {
1874 audio.phys = iores->start;
1875 audio.base = hdmi->regs;
1876 audio.irq = irq;
1877 audio.hdmi = hdmi;
Russell Kingf5ce4052013-11-07 16:06:01 +00001878 audio.eld = hdmi->connector.eld;
Russell King7ed6c662013-11-07 16:01:45 +00001879
1880 pdevinfo.name = "dw-hdmi-ahb-audio";
1881 pdevinfo.data = &audio;
1882 pdevinfo.size_data = sizeof(audio);
1883 pdevinfo.dma_mask = DMA_BIT_MASK(32);
1884 hdmi->audio = platform_device_register_full(&pdevinfo);
1885 }
1886
Russell King17b50012013-11-03 11:23:34 +00001887 dev_set_drvdata(dev, hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001888
1889 return 0;
1890
1891err_iahb:
1892 clk_disable_unprepare(hdmi->iahb_clk);
1893err_isfr:
1894 clk_disable_unprepare(hdmi->isfr_clk);
1895
1896 return ret;
1897}
Andy Yanb21f4b62014-12-05 14:26:31 +08001898EXPORT_SYMBOL_GPL(dw_hdmi_bind);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001899
Andy Yanb21f4b62014-12-05 14:26:31 +08001900void dw_hdmi_unbind(struct device *dev, struct device *master, void *data)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001901{
Andy Yanb21f4b62014-12-05 14:26:31 +08001902 struct dw_hdmi *hdmi = dev_get_drvdata(dev);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001903
Russell King7ed6c662013-11-07 16:01:45 +00001904 if (hdmi->audio && !IS_ERR(hdmi->audio))
1905 platform_device_unregister(hdmi->audio);
1906
Russell Kingd94905e2013-11-03 22:23:24 +00001907 /* Disable all interrupts */
1908 hdmi_writeb(hdmi, ~0, HDMI_IH_MUTE_PHY_STAT0);
1909
Russell King1b3f7672013-11-03 13:30:48 +00001910 hdmi->connector.funcs->destroy(&hdmi->connector);
Andy Yan3d1b35a2014-12-05 14:25:05 +08001911 hdmi->encoder->funcs->destroy(hdmi->encoder);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001912
1913 clk_disable_unprepare(hdmi->iahb_clk);
1914 clk_disable_unprepare(hdmi->isfr_clk);
1915 i2c_put_adapter(hdmi->ddc);
Russell King17b50012013-11-03 11:23:34 +00001916}
Andy Yanb21f4b62014-12-05 14:26:31 +08001917EXPORT_SYMBOL_GPL(dw_hdmi_unbind);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001918
1919MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
Andy Yan3d1b35a2014-12-05 14:25:05 +08001920MODULE_AUTHOR("Andy Yan <andy.yan@rock-chips.com>");
1921MODULE_AUTHOR("Yakir Yang <ykk@rock-chips.com>");
Andy Yanb21f4b62014-12-05 14:26:31 +08001922MODULE_DESCRIPTION("DW HDMI transmitter driver");
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001923MODULE_LICENSE("GPL");
Andy Yanb21f4b62014-12-05 14:26:31 +08001924MODULE_ALIAS("platform:dw-hdmi");