blob: e5ee92370f49e73d73c414359d1eb2b76e499a43 [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;
Russell Kingd0c96d12015-07-22 10:35:41 +0100224 unsigned int mult = 1;
225
226 while (freq > 48000) {
227 mult *= 2;
228 freq /= 2;
229 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200230
231 switch (freq) {
232 case 32000:
233 if (pixel_clk == 25170000)
234 n = (ratio == 150) ? 9152 : 4576;
235 else if (pixel_clk == 27020000)
236 n = (ratio == 150) ? 8192 : 4096;
237 else if (pixel_clk == 74170000 || pixel_clk == 148350000)
238 n = 11648;
239 else
240 n = 4096;
Russell Kingd0c96d12015-07-22 10:35:41 +0100241 n *= mult;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200242 break;
243
244 case 44100:
245 if (pixel_clk == 25170000)
246 n = 7007;
247 else if (pixel_clk == 74170000)
248 n = 17836;
249 else if (pixel_clk == 148350000)
250 n = (ratio == 150) ? 17836 : 8918;
251 else
252 n = 6272;
Russell Kingd0c96d12015-07-22 10:35:41 +0100253 n *= mult;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200254 break;
255
256 case 48000:
257 if (pixel_clk == 25170000)
258 n = (ratio == 150) ? 9152 : 6864;
259 else if (pixel_clk == 27020000)
260 n = (ratio == 150) ? 8192 : 6144;
261 else if (pixel_clk == 74170000)
262 n = 11648;
263 else if (pixel_clk == 148350000)
264 n = (ratio == 150) ? 11648 : 5824;
265 else
266 n = 6144;
Russell Kingd0c96d12015-07-22 10:35:41 +0100267 n *= mult;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200268 break;
269
270 default:
271 break;
272 }
273
274 return n;
275}
276
277static unsigned int hdmi_compute_cts(unsigned int freq, unsigned long pixel_clk,
278 unsigned int ratio)
279{
280 unsigned int cts = 0;
281
282 pr_debug("%s: freq: %d pixel_clk: %ld ratio: %d\n", __func__, freq,
283 pixel_clk, ratio);
284
285 switch (freq) {
286 case 32000:
287 if (pixel_clk == 297000000) {
288 cts = 222750;
289 break;
290 }
291 case 48000:
292 case 96000:
293 case 192000:
294 switch (pixel_clk) {
295 case 25200000:
296 case 27000000:
297 case 54000000:
298 case 74250000:
299 case 148500000:
300 cts = pixel_clk / 1000;
301 break;
302 case 297000000:
303 cts = 247500;
304 break;
305 /*
306 * All other TMDS clocks are not supported by
307 * DWC_hdmi_tx. The TMDS clocks divided or
308 * multiplied by 1,001 coefficients are not
309 * supported.
310 */
311 default:
312 break;
313 }
314 break;
315 case 44100:
316 case 88200:
317 case 176400:
318 switch (pixel_clk) {
319 case 25200000:
320 cts = 28000;
321 break;
322 case 27000000:
323 cts = 30000;
324 break;
325 case 54000000:
326 cts = 60000;
327 break;
328 case 74250000:
329 cts = 82500;
330 break;
331 case 148500000:
332 cts = 165000;
333 break;
334 case 297000000:
335 cts = 247500;
336 break;
337 default:
338 break;
339 }
340 break;
341 default:
342 break;
343 }
344 if (ratio == 100)
345 return cts;
Catalina Mocanu7557b6e2014-09-24 14:27:36 -0700346 return (cts * ratio) / 100;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200347}
348
Andy Yanb21f4b62014-12-05 14:26:31 +0800349static void hdmi_set_clk_regenerator(struct dw_hdmi *hdmi,
Russell Kingf879b382015-03-27 12:53:29 +0000350 unsigned long pixel_clk, unsigned int sample_rate, unsigned int ratio)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200351{
Russell Kingf879b382015-03-27 12:53:29 +0000352 unsigned int n, cts;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200353
Russell Kingf879b382015-03-27 12:53:29 +0000354 n = hdmi_compute_n(sample_rate, pixel_clk, ratio);
355 cts = hdmi_compute_cts(sample_rate, pixel_clk, ratio);
356 if (!cts) {
357 dev_err(hdmi->dev,
358 "%s: pixel clock/sample rate not supported: %luMHz / %ukHz\n",
359 __func__, pixel_clk, sample_rate);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200360 }
361
Russell Kingf879b382015-03-27 12:53:29 +0000362 dev_dbg(hdmi->dev, "%s: samplerate=%ukHz ratio=%d pixelclk=%luMHz N=%d cts=%d\n",
363 __func__, sample_rate, ratio, pixel_clk, n, cts);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200364
Russell Kingb90120a2015-03-27 12:59:58 +0000365 spin_lock_irq(&hdmi->audio_lock);
366 hdmi->audio_n = n;
367 hdmi->audio_cts = cts;
368 hdmi_set_cts_n(hdmi, cts, hdmi->audio_enable ? n : 0);
369 spin_unlock_irq(&hdmi->audio_lock);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200370}
371
Andy Yanb21f4b62014-12-05 14:26:31 +0800372static void hdmi_init_clk_regenerator(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200373{
Russell King6bcf4952015-02-02 11:01:08 +0000374 mutex_lock(&hdmi->audio_mutex);
Russell Kingf879b382015-03-27 12:53:29 +0000375 hdmi_set_clk_regenerator(hdmi, 74250000, hdmi->sample_rate,
376 hdmi->ratio);
Russell King6bcf4952015-02-02 11:01:08 +0000377 mutex_unlock(&hdmi->audio_mutex);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200378}
379
Andy Yanb21f4b62014-12-05 14:26:31 +0800380static void hdmi_clk_regenerator_update_pixel_clock(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200381{
Russell King6bcf4952015-02-02 11:01:08 +0000382 mutex_lock(&hdmi->audio_mutex);
Russell Kingf879b382015-03-27 12:53:29 +0000383 hdmi_set_clk_regenerator(hdmi, hdmi->hdmi_data.video_mode.mpixelclock,
384 hdmi->sample_rate, hdmi->ratio);
Russell King6bcf4952015-02-02 11:01:08 +0000385 mutex_unlock(&hdmi->audio_mutex);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200386}
387
Russell Kingb5814ff2015-03-27 12:50:58 +0000388void dw_hdmi_set_sample_rate(struct dw_hdmi *hdmi, unsigned int rate)
389{
390 mutex_lock(&hdmi->audio_mutex);
391 hdmi->sample_rate = rate;
392 hdmi_set_clk_regenerator(hdmi, hdmi->hdmi_data.video_mode.mpixelclock,
393 hdmi->sample_rate, hdmi->ratio);
394 mutex_unlock(&hdmi->audio_mutex);
395}
396EXPORT_SYMBOL_GPL(dw_hdmi_set_sample_rate);
397
Russell Kingb90120a2015-03-27 12:59:58 +0000398void dw_hdmi_audio_enable(struct dw_hdmi *hdmi)
399{
400 unsigned long flags;
401
402 spin_lock_irqsave(&hdmi->audio_lock, flags);
403 hdmi->audio_enable = true;
404 hdmi_set_cts_n(hdmi, hdmi->audio_cts, hdmi->audio_n);
405 spin_unlock_irqrestore(&hdmi->audio_lock, flags);
406}
407EXPORT_SYMBOL_GPL(dw_hdmi_audio_enable);
408
409void dw_hdmi_audio_disable(struct dw_hdmi *hdmi)
410{
411 unsigned long flags;
412
413 spin_lock_irqsave(&hdmi->audio_lock, flags);
414 hdmi->audio_enable = false;
415 hdmi_set_cts_n(hdmi, hdmi->audio_cts, 0);
416 spin_unlock_irqrestore(&hdmi->audio_lock, flags);
417}
418EXPORT_SYMBOL_GPL(dw_hdmi_audio_disable);
419
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200420/*
421 * this submodule is responsible for the video data synchronization.
422 * for example, for RGB 4:4:4 input, the data map is defined as
423 * pin{47~40} <==> R[7:0]
424 * pin{31~24} <==> G[7:0]
425 * pin{15~8} <==> B[7:0]
426 */
Andy Yanb21f4b62014-12-05 14:26:31 +0800427static void hdmi_video_sample(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200428{
429 int color_format = 0;
430 u8 val;
431
432 if (hdmi->hdmi_data.enc_in_format == RGB) {
433 if (hdmi->hdmi_data.enc_color_depth == 8)
434 color_format = 0x01;
435 else if (hdmi->hdmi_data.enc_color_depth == 10)
436 color_format = 0x03;
437 else if (hdmi->hdmi_data.enc_color_depth == 12)
438 color_format = 0x05;
439 else if (hdmi->hdmi_data.enc_color_depth == 16)
440 color_format = 0x07;
441 else
442 return;
443 } else if (hdmi->hdmi_data.enc_in_format == YCBCR444) {
444 if (hdmi->hdmi_data.enc_color_depth == 8)
445 color_format = 0x09;
446 else if (hdmi->hdmi_data.enc_color_depth == 10)
447 color_format = 0x0B;
448 else if (hdmi->hdmi_data.enc_color_depth == 12)
449 color_format = 0x0D;
450 else if (hdmi->hdmi_data.enc_color_depth == 16)
451 color_format = 0x0F;
452 else
453 return;
454 } else if (hdmi->hdmi_data.enc_in_format == YCBCR422_8BITS) {
455 if (hdmi->hdmi_data.enc_color_depth == 8)
456 color_format = 0x16;
457 else if (hdmi->hdmi_data.enc_color_depth == 10)
458 color_format = 0x14;
459 else if (hdmi->hdmi_data.enc_color_depth == 12)
460 color_format = 0x12;
461 else
462 return;
463 }
464
465 val = HDMI_TX_INVID0_INTERNAL_DE_GENERATOR_DISABLE |
466 ((color_format << HDMI_TX_INVID0_VIDEO_MAPPING_OFFSET) &
467 HDMI_TX_INVID0_VIDEO_MAPPING_MASK);
468 hdmi_writeb(hdmi, val, HDMI_TX_INVID0);
469
470 /* Enable TX stuffing: When DE is inactive, fix the output data to 0 */
471 val = HDMI_TX_INSTUFFING_BDBDATA_STUFFING_ENABLE |
472 HDMI_TX_INSTUFFING_RCRDATA_STUFFING_ENABLE |
473 HDMI_TX_INSTUFFING_GYDATA_STUFFING_ENABLE;
474 hdmi_writeb(hdmi, val, HDMI_TX_INSTUFFING);
475 hdmi_writeb(hdmi, 0x0, HDMI_TX_GYDATA0);
476 hdmi_writeb(hdmi, 0x0, HDMI_TX_GYDATA1);
477 hdmi_writeb(hdmi, 0x0, HDMI_TX_RCRDATA0);
478 hdmi_writeb(hdmi, 0x0, HDMI_TX_RCRDATA1);
479 hdmi_writeb(hdmi, 0x0, HDMI_TX_BCBDATA0);
480 hdmi_writeb(hdmi, 0x0, HDMI_TX_BCBDATA1);
481}
482
Andy Yanb21f4b62014-12-05 14:26:31 +0800483static int is_color_space_conversion(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200484{
Fabio Estevamba92b222014-02-06 10:12:03 -0200485 return hdmi->hdmi_data.enc_in_format != hdmi->hdmi_data.enc_out_format;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200486}
487
Andy Yanb21f4b62014-12-05 14:26:31 +0800488static int is_color_space_decimation(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200489{
Fabio Estevamba92b222014-02-06 10:12:03 -0200490 if (hdmi->hdmi_data.enc_out_format != YCBCR422_8BITS)
491 return 0;
492 if (hdmi->hdmi_data.enc_in_format == RGB ||
493 hdmi->hdmi_data.enc_in_format == YCBCR444)
494 return 1;
495 return 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200496}
497
Andy Yanb21f4b62014-12-05 14:26:31 +0800498static int is_color_space_interpolation(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200499{
Fabio Estevamba92b222014-02-06 10:12:03 -0200500 if (hdmi->hdmi_data.enc_in_format != YCBCR422_8BITS)
501 return 0;
502 if (hdmi->hdmi_data.enc_out_format == RGB ||
503 hdmi->hdmi_data.enc_out_format == YCBCR444)
504 return 1;
505 return 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200506}
507
Andy Yanb21f4b62014-12-05 14:26:31 +0800508static void dw_hdmi_update_csc_coeffs(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200509{
510 const u16 (*csc_coeff)[3][4] = &csc_coeff_default;
Russell Kingc082f9d2013-11-04 12:10:40 +0000511 unsigned i;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200512 u32 csc_scale = 1;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200513
514 if (is_color_space_conversion(hdmi)) {
515 if (hdmi->hdmi_data.enc_out_format == RGB) {
Gulsah Kose256a38b2014-03-09 20:11:07 +0200516 if (hdmi->hdmi_data.colorimetry ==
517 HDMI_COLORIMETRY_ITU_601)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200518 csc_coeff = &csc_coeff_rgb_out_eitu601;
519 else
520 csc_coeff = &csc_coeff_rgb_out_eitu709;
521 } else if (hdmi->hdmi_data.enc_in_format == RGB) {
Gulsah Kose256a38b2014-03-09 20:11:07 +0200522 if (hdmi->hdmi_data.colorimetry ==
523 HDMI_COLORIMETRY_ITU_601)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200524 csc_coeff = &csc_coeff_rgb_in_eitu601;
525 else
526 csc_coeff = &csc_coeff_rgb_in_eitu709;
527 csc_scale = 0;
528 }
529 }
530
Russell Kingc082f9d2013-11-04 12:10:40 +0000531 /* The CSC registers are sequential, alternating MSB then LSB */
532 for (i = 0; i < ARRAY_SIZE(csc_coeff_default[0]); i++) {
533 u16 coeff_a = (*csc_coeff)[0][i];
534 u16 coeff_b = (*csc_coeff)[1][i];
535 u16 coeff_c = (*csc_coeff)[2][i];
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200536
Andy Yanb5878332014-12-05 14:23:52 +0800537 hdmi_writeb(hdmi, coeff_a & 0xff, HDMI_CSC_COEF_A1_LSB + i * 2);
Russell Kingc082f9d2013-11-04 12:10:40 +0000538 hdmi_writeb(hdmi, coeff_a >> 8, HDMI_CSC_COEF_A1_MSB + i * 2);
539 hdmi_writeb(hdmi, coeff_b & 0xff, HDMI_CSC_COEF_B1_LSB + i * 2);
540 hdmi_writeb(hdmi, coeff_b >> 8, HDMI_CSC_COEF_B1_MSB + i * 2);
Andy Yanb5878332014-12-05 14:23:52 +0800541 hdmi_writeb(hdmi, coeff_c & 0xff, HDMI_CSC_COEF_C1_LSB + i * 2);
Russell Kingc082f9d2013-11-04 12:10:40 +0000542 hdmi_writeb(hdmi, coeff_c >> 8, HDMI_CSC_COEF_C1_MSB + i * 2);
543 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200544
Russell King812bc612013-11-04 12:42:02 +0000545 hdmi_modb(hdmi, csc_scale, HDMI_CSC_SCALE_CSCSCALE_MASK,
546 HDMI_CSC_SCALE);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200547}
548
Andy Yanb21f4b62014-12-05 14:26:31 +0800549static void hdmi_video_csc(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200550{
551 int color_depth = 0;
552 int interpolation = HDMI_CSC_CFG_INTMODE_DISABLE;
553 int decimation = 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200554
555 /* YCC422 interpolation to 444 mode */
556 if (is_color_space_interpolation(hdmi))
557 interpolation = HDMI_CSC_CFG_INTMODE_CHROMA_INT_FORMULA1;
558 else if (is_color_space_decimation(hdmi))
559 decimation = HDMI_CSC_CFG_DECMODE_CHROMA_INT_FORMULA3;
560
561 if (hdmi->hdmi_data.enc_color_depth == 8)
562 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_24BPP;
563 else if (hdmi->hdmi_data.enc_color_depth == 10)
564 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_30BPP;
565 else if (hdmi->hdmi_data.enc_color_depth == 12)
566 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_36BPP;
567 else if (hdmi->hdmi_data.enc_color_depth == 16)
568 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_48BPP;
569 else
570 return;
571
572 /* Configure the CSC registers */
573 hdmi_writeb(hdmi, interpolation | decimation, HDMI_CSC_CFG);
Russell King812bc612013-11-04 12:42:02 +0000574 hdmi_modb(hdmi, color_depth, HDMI_CSC_SCALE_CSC_COLORDE_PTH_MASK,
575 HDMI_CSC_SCALE);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200576
Andy Yanb21f4b62014-12-05 14:26:31 +0800577 dw_hdmi_update_csc_coeffs(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200578}
579
580/*
581 * HDMI video packetizer is used to packetize the data.
582 * for example, if input is YCC422 mode or repeater is used,
583 * data should be repacked this module can be bypassed.
584 */
Andy Yanb21f4b62014-12-05 14:26:31 +0800585static void hdmi_video_packetize(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200586{
587 unsigned int color_depth = 0;
588 unsigned int remap_size = HDMI_VP_REMAP_YCC422_16bit;
589 unsigned int output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_PP;
590 struct hdmi_data_info *hdmi_data = &hdmi->hdmi_data;
Russell Kingbebdf662013-11-04 12:55:30 +0000591 u8 val, vp_conf;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200592
Andy Yanb5878332014-12-05 14:23:52 +0800593 if (hdmi_data->enc_out_format == RGB ||
594 hdmi_data->enc_out_format == YCBCR444) {
595 if (!hdmi_data->enc_color_depth) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200596 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;
Andy Yanb5878332014-12-05 14:23:52 +0800597 } else if (hdmi_data->enc_color_depth == 8) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200598 color_depth = 4;
599 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;
Andy Yanb5878332014-12-05 14:23:52 +0800600 } else if (hdmi_data->enc_color_depth == 10) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200601 color_depth = 5;
Andy Yanb5878332014-12-05 14:23:52 +0800602 } else if (hdmi_data->enc_color_depth == 12) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200603 color_depth = 6;
Andy Yanb5878332014-12-05 14:23:52 +0800604 } else if (hdmi_data->enc_color_depth == 16) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200605 color_depth = 7;
Andy Yanb5878332014-12-05 14:23:52 +0800606 } else {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200607 return;
Andy Yanb5878332014-12-05 14:23:52 +0800608 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200609 } else if (hdmi_data->enc_out_format == YCBCR422_8BITS) {
610 if (!hdmi_data->enc_color_depth ||
611 hdmi_data->enc_color_depth == 8)
612 remap_size = HDMI_VP_REMAP_YCC422_16bit;
613 else if (hdmi_data->enc_color_depth == 10)
614 remap_size = HDMI_VP_REMAP_YCC422_20bit;
615 else if (hdmi_data->enc_color_depth == 12)
616 remap_size = HDMI_VP_REMAP_YCC422_24bit;
617 else
618 return;
619 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_YCC422;
Andy Yanb5878332014-12-05 14:23:52 +0800620 } else {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200621 return;
Andy Yanb5878332014-12-05 14:23:52 +0800622 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200623
624 /* set the packetizer registers */
625 val = ((color_depth << HDMI_VP_PR_CD_COLOR_DEPTH_OFFSET) &
626 HDMI_VP_PR_CD_COLOR_DEPTH_MASK) |
627 ((hdmi_data->pix_repet_factor <<
628 HDMI_VP_PR_CD_DESIRED_PR_FACTOR_OFFSET) &
629 HDMI_VP_PR_CD_DESIRED_PR_FACTOR_MASK);
630 hdmi_writeb(hdmi, val, HDMI_VP_PR_CD);
631
Russell King812bc612013-11-04 12:42:02 +0000632 hdmi_modb(hdmi, HDMI_VP_STUFF_PR_STUFFING_STUFFING_MODE,
633 HDMI_VP_STUFF_PR_STUFFING_MASK, HDMI_VP_STUFF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200634
635 /* Data from pixel repeater block */
636 if (hdmi_data->pix_repet_factor > 1) {
Russell Kingbebdf662013-11-04 12:55:30 +0000637 vp_conf = HDMI_VP_CONF_PR_EN_ENABLE |
638 HDMI_VP_CONF_BYPASS_SELECT_PIX_REPEATER;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200639 } else { /* data from packetizer block */
Russell Kingbebdf662013-11-04 12:55:30 +0000640 vp_conf = HDMI_VP_CONF_PR_EN_DISABLE |
641 HDMI_VP_CONF_BYPASS_SELECT_VID_PACKETIZER;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200642 }
643
Russell Kingbebdf662013-11-04 12:55:30 +0000644 hdmi_modb(hdmi, vp_conf,
645 HDMI_VP_CONF_PR_EN_MASK |
646 HDMI_VP_CONF_BYPASS_SELECT_MASK, HDMI_VP_CONF);
647
Russell King812bc612013-11-04 12:42:02 +0000648 hdmi_modb(hdmi, 1 << HDMI_VP_STUFF_IDEFAULT_PHASE_OFFSET,
649 HDMI_VP_STUFF_IDEFAULT_PHASE_MASK, HDMI_VP_STUFF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200650
651 hdmi_writeb(hdmi, remap_size, HDMI_VP_REMAP);
652
653 if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_PP) {
Russell Kingbebdf662013-11-04 12:55:30 +0000654 vp_conf = HDMI_VP_CONF_BYPASS_EN_DISABLE |
655 HDMI_VP_CONF_PP_EN_ENABLE |
656 HDMI_VP_CONF_YCC422_EN_DISABLE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200657 } else if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_YCC422) {
Russell Kingbebdf662013-11-04 12:55:30 +0000658 vp_conf = HDMI_VP_CONF_BYPASS_EN_DISABLE |
659 HDMI_VP_CONF_PP_EN_DISABLE |
660 HDMI_VP_CONF_YCC422_EN_ENABLE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200661 } else if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS) {
Russell Kingbebdf662013-11-04 12:55:30 +0000662 vp_conf = HDMI_VP_CONF_BYPASS_EN_ENABLE |
663 HDMI_VP_CONF_PP_EN_DISABLE |
664 HDMI_VP_CONF_YCC422_EN_DISABLE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200665 } else {
666 return;
667 }
668
Russell Kingbebdf662013-11-04 12:55:30 +0000669 hdmi_modb(hdmi, vp_conf,
670 HDMI_VP_CONF_BYPASS_EN_MASK | HDMI_VP_CONF_PP_EN_ENMASK |
671 HDMI_VP_CONF_YCC422_EN_MASK, HDMI_VP_CONF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200672
Russell King812bc612013-11-04 12:42:02 +0000673 hdmi_modb(hdmi, HDMI_VP_STUFF_PP_STUFFING_STUFFING_MODE |
674 HDMI_VP_STUFF_YCC422_STUFFING_STUFFING_MODE,
675 HDMI_VP_STUFF_PP_STUFFING_MASK |
676 HDMI_VP_STUFF_YCC422_STUFFING_MASK, HDMI_VP_STUFF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200677
Russell King812bc612013-11-04 12:42:02 +0000678 hdmi_modb(hdmi, output_select, HDMI_VP_CONF_OUTPUT_SELECTOR_MASK,
679 HDMI_VP_CONF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200680}
681
Andy Yanb21f4b62014-12-05 14:26:31 +0800682static inline void hdmi_phy_test_clear(struct dw_hdmi *hdmi,
Andy Yanb5878332014-12-05 14:23:52 +0800683 unsigned char bit)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200684{
Russell King812bc612013-11-04 12:42:02 +0000685 hdmi_modb(hdmi, bit << HDMI_PHY_TST0_TSTCLR_OFFSET,
686 HDMI_PHY_TST0_TSTCLR_MASK, HDMI_PHY_TST0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200687}
688
Andy Yanb21f4b62014-12-05 14:26:31 +0800689static inline void hdmi_phy_test_enable(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_TSTEN_OFFSET,
693 HDMI_PHY_TST0_TSTEN_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_clock(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_TSTCLK_OFFSET,
700 HDMI_PHY_TST0_TSTCLK_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_din(struct dw_hdmi *hdmi,
Andy Yanb5878332014-12-05 14:23:52 +0800704 unsigned char bit)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200705{
706 hdmi_writeb(hdmi, bit, HDMI_PHY_TST1);
707}
708
Andy Yanb21f4b62014-12-05 14:26:31 +0800709static inline void hdmi_phy_test_dout(struct dw_hdmi *hdmi,
Andy Yanb5878332014-12-05 14:23:52 +0800710 unsigned char bit)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200711{
712 hdmi_writeb(hdmi, bit, HDMI_PHY_TST2);
713}
714
Andy Yanb21f4b62014-12-05 14:26:31 +0800715static bool hdmi_phy_wait_i2c_done(struct dw_hdmi *hdmi, int msec)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200716{
Andy Yana4d3b8b2014-12-05 14:31:09 +0800717 u32 val;
718
719 while ((val = hdmi_readb(hdmi, HDMI_IH_I2CMPHY_STAT0) & 0x3) == 0) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200720 if (msec-- == 0)
721 return false;
Emil Renner Berthing0e6bcf32014-03-30 00:21:21 +0100722 udelay(1000);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200723 }
Andy Yana4d3b8b2014-12-05 14:31:09 +0800724 hdmi_writeb(hdmi, val, HDMI_IH_I2CMPHY_STAT0);
725
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200726 return true;
727}
728
Andy Yanb21f4b62014-12-05 14:26:31 +0800729static void __hdmi_phy_i2c_write(struct dw_hdmi *hdmi, unsigned short data,
Andy Yanb5878332014-12-05 14:23:52 +0800730 unsigned char addr)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200731{
732 hdmi_writeb(hdmi, 0xFF, HDMI_IH_I2CMPHY_STAT0);
733 hdmi_writeb(hdmi, addr, HDMI_PHY_I2CM_ADDRESS_ADDR);
734 hdmi_writeb(hdmi, (unsigned char)(data >> 8),
Andy Yanb5878332014-12-05 14:23:52 +0800735 HDMI_PHY_I2CM_DATAO_1_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200736 hdmi_writeb(hdmi, (unsigned char)(data >> 0),
Andy Yanb5878332014-12-05 14:23:52 +0800737 HDMI_PHY_I2CM_DATAO_0_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200738 hdmi_writeb(hdmi, HDMI_PHY_I2CM_OPERATION_ADDR_WRITE,
Andy Yanb5878332014-12-05 14:23:52 +0800739 HDMI_PHY_I2CM_OPERATION_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200740 hdmi_phy_wait_i2c_done(hdmi, 1000);
741}
742
Andy Yanb21f4b62014-12-05 14:26:31 +0800743static int hdmi_phy_i2c_write(struct dw_hdmi *hdmi, unsigned short data,
Andy Yanb5878332014-12-05 14:23:52 +0800744 unsigned char addr)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200745{
746 __hdmi_phy_i2c_write(hdmi, data, addr);
747 return 0;
748}
749
Russell King2fada102015-07-28 12:21:34 +0100750static void dw_hdmi_phy_enable_powerdown(struct dw_hdmi *hdmi, bool enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200751{
Russell King2fada102015-07-28 12:21:34 +0100752 hdmi_mask_writeb(hdmi, !enable, HDMI_PHY_CONF0,
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200753 HDMI_PHY_CONF0_PDZ_OFFSET,
754 HDMI_PHY_CONF0_PDZ_MASK);
755}
756
Andy Yanb21f4b62014-12-05 14:26:31 +0800757static void dw_hdmi_phy_enable_tmds(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200758{
759 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
760 HDMI_PHY_CONF0_ENTMDS_OFFSET,
761 HDMI_PHY_CONF0_ENTMDS_MASK);
762}
763
Andy Yand346c142014-12-05 14:31:53 +0800764static void dw_hdmi_phy_enable_spare(struct dw_hdmi *hdmi, u8 enable)
765{
766 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
767 HDMI_PHY_CONF0_SPARECTRL_OFFSET,
768 HDMI_PHY_CONF0_SPARECTRL_MASK);
769}
770
Andy Yanb21f4b62014-12-05 14:26:31 +0800771static void dw_hdmi_phy_gen2_pddq(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200772{
773 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
774 HDMI_PHY_CONF0_GEN2_PDDQ_OFFSET,
775 HDMI_PHY_CONF0_GEN2_PDDQ_MASK);
776}
777
Andy Yanb21f4b62014-12-05 14:26:31 +0800778static void dw_hdmi_phy_gen2_txpwron(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_TXPWRON_OFFSET,
782 HDMI_PHY_CONF0_GEN2_TXPWRON_MASK);
783}
784
Andy Yanb21f4b62014-12-05 14:26:31 +0800785static void dw_hdmi_phy_sel_data_en_pol(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_SELDATAENPOL_OFFSET,
789 HDMI_PHY_CONF0_SELDATAENPOL_MASK);
790}
791
Andy Yanb21f4b62014-12-05 14:26:31 +0800792static void dw_hdmi_phy_sel_interface_control(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_SELDIPIF_OFFSET,
796 HDMI_PHY_CONF0_SELDIPIF_MASK);
797}
798
Andy Yanb21f4b62014-12-05 14:26:31 +0800799static int hdmi_phy_configure(struct dw_hdmi *hdmi, unsigned char prep,
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200800 unsigned char res, int cscon)
801{
Russell King39cc1532015-03-31 18:34:11 +0100802 unsigned res_idx;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200803 u8 val, msec;
Russell King39cc1532015-03-31 18:34:11 +0100804 const struct dw_hdmi_plat_data *pdata = hdmi->plat_data;
805 const struct dw_hdmi_mpll_config *mpll_config = pdata->mpll_cfg;
806 const struct dw_hdmi_curr_ctrl *curr_ctrl = pdata->cur_ctr;
807 const struct dw_hdmi_phy_config *phy_config = pdata->phy_config;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200808
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200809 if (prep)
810 return -EINVAL;
Russell King3e46f152013-11-04 11:24:00 +0000811
812 switch (res) {
813 case 0: /* color resolution 0 is 8 bit colour depth */
814 case 8:
Andy Yanb21f4b62014-12-05 14:26:31 +0800815 res_idx = DW_HDMI_RES_8;
Russell King3e46f152013-11-04 11:24:00 +0000816 break;
817 case 10:
Andy Yanb21f4b62014-12-05 14:26:31 +0800818 res_idx = DW_HDMI_RES_10;
Russell King3e46f152013-11-04 11:24:00 +0000819 break;
820 case 12:
Andy Yanb21f4b62014-12-05 14:26:31 +0800821 res_idx = DW_HDMI_RES_12;
Russell King3e46f152013-11-04 11:24:00 +0000822 break;
823 default:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200824 return -EINVAL;
Russell King3e46f152013-11-04 11:24:00 +0000825 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200826
Russell King39cc1532015-03-31 18:34:11 +0100827 /* PLL/MPLL Cfg - always match on final entry */
828 for (; mpll_config->mpixelclock != ~0UL; mpll_config++)
829 if (hdmi->hdmi_data.video_mode.mpixelclock <=
830 mpll_config->mpixelclock)
831 break;
832
833 for (; curr_ctrl->mpixelclock != ~0UL; curr_ctrl++)
834 if (hdmi->hdmi_data.video_mode.mpixelclock <=
835 curr_ctrl->mpixelclock)
836 break;
837
838 for (; phy_config->mpixelclock != ~0UL; phy_config++)
839 if (hdmi->hdmi_data.video_mode.mpixelclock <=
840 phy_config->mpixelclock)
841 break;
842
843 if (mpll_config->mpixelclock == ~0UL ||
844 curr_ctrl->mpixelclock == ~0UL ||
845 phy_config->mpixelclock == ~0UL) {
846 dev_err(hdmi->dev, "Pixel clock %d - unsupported by HDMI\n",
847 hdmi->hdmi_data.video_mode.mpixelclock);
848 return -EINVAL;
849 }
850
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200851 /* Enable csc path */
852 if (cscon)
853 val = HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_IN_PATH;
854 else
855 val = HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_BYPASS;
856
857 hdmi_writeb(hdmi, val, HDMI_MC_FLOWCTRL);
858
859 /* gen2 tx power off */
Andy Yanb21f4b62014-12-05 14:26:31 +0800860 dw_hdmi_phy_gen2_txpwron(hdmi, 0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200861
862 /* gen2 pddq */
Andy Yanb21f4b62014-12-05 14:26:31 +0800863 dw_hdmi_phy_gen2_pddq(hdmi, 1);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200864
865 /* PHY reset */
866 hdmi_writeb(hdmi, HDMI_MC_PHYRSTZ_DEASSERT, HDMI_MC_PHYRSTZ);
867 hdmi_writeb(hdmi, HDMI_MC_PHYRSTZ_ASSERT, HDMI_MC_PHYRSTZ);
868
869 hdmi_writeb(hdmi, HDMI_MC_HEACPHY_RST_ASSERT, HDMI_MC_HEACPHY_RST);
870
871 hdmi_phy_test_clear(hdmi, 1);
872 hdmi_writeb(hdmi, HDMI_PHY_I2CM_SLAVE_ADDR_PHY_GEN2,
Andy Yanb5878332014-12-05 14:23:52 +0800873 HDMI_PHY_I2CM_SLAVE_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200874 hdmi_phy_test_clear(hdmi, 0);
875
Russell King39cc1532015-03-31 18:34:11 +0100876 hdmi_phy_i2c_write(hdmi, mpll_config->res[res_idx].cpce, 0x06);
877 hdmi_phy_i2c_write(hdmi, mpll_config->res[res_idx].gmp, 0x15);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200878
Russell King3e46f152013-11-04 11:24:00 +0000879 /* CURRCTRL */
Russell King39cc1532015-03-31 18:34:11 +0100880 hdmi_phy_i2c_write(hdmi, curr_ctrl->curr[res_idx], 0x10);
Russell King3e46f152013-11-04 11:24:00 +0000881
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200882 hdmi_phy_i2c_write(hdmi, 0x0000, 0x13); /* PLLPHBYCTRL */
883 hdmi_phy_i2c_write(hdmi, 0x0006, 0x17);
Andy Yanaaa757a2014-12-05 14:25:50 +0800884
Russell King39cc1532015-03-31 18:34:11 +0100885 hdmi_phy_i2c_write(hdmi, phy_config->term, 0x19); /* TXTERM */
886 hdmi_phy_i2c_write(hdmi, phy_config->sym_ctr, 0x09); /* CKSYMTXCTRL */
887 hdmi_phy_i2c_write(hdmi, phy_config->vlev_ctr, 0x0E); /* VLEVCTRL */
Yakir Yang034705a2015-03-31 23:56:10 -0400888
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200889 /* REMOVE CLK TERM */
890 hdmi_phy_i2c_write(hdmi, 0x8000, 0x05); /* CKCALCTRL */
891
Russell King2fada102015-07-28 12:21:34 +0100892 dw_hdmi_phy_enable_powerdown(hdmi, false);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200893
894 /* toggle TMDS enable */
Andy Yanb21f4b62014-12-05 14:26:31 +0800895 dw_hdmi_phy_enable_tmds(hdmi, 0);
896 dw_hdmi_phy_enable_tmds(hdmi, 1);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200897
898 /* gen2 tx power on */
Andy Yanb21f4b62014-12-05 14:26:31 +0800899 dw_hdmi_phy_gen2_txpwron(hdmi, 1);
900 dw_hdmi_phy_gen2_pddq(hdmi, 0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200901
Andy Yan12b9f202015-01-07 15:48:27 +0800902 if (hdmi->dev_type == RK3288_HDMI)
903 dw_hdmi_phy_enable_spare(hdmi, 1);
904
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200905 /*Wait for PHY PLL lock */
906 msec = 5;
907 do {
908 val = hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_TX_PHY_LOCK;
909 if (!val)
910 break;
911
912 if (msec == 0) {
913 dev_err(hdmi->dev, "PHY PLL not locked\n");
914 return -ETIMEDOUT;
915 }
916
917 udelay(1000);
918 msec--;
919 } while (1);
920
921 return 0;
922}
923
Andy Yanb21f4b62014-12-05 14:26:31 +0800924static int dw_hdmi_phy_init(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200925{
926 int i, ret;
Russell King05b13422015-07-21 15:35:52 +0100927 bool cscon;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200928
929 /*check csc whether needed activated in HDMI mode */
Russell King05b13422015-07-21 15:35:52 +0100930 cscon = hdmi->sink_is_hdmi && is_color_space_conversion(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200931
932 /* HDMI Phy spec says to do the phy initialization sequence twice */
933 for (i = 0; i < 2; i++) {
Andy Yanb21f4b62014-12-05 14:26:31 +0800934 dw_hdmi_phy_sel_data_en_pol(hdmi, 1);
935 dw_hdmi_phy_sel_interface_control(hdmi, 0);
936 dw_hdmi_phy_enable_tmds(hdmi, 0);
Russell King2fada102015-07-28 12:21:34 +0100937 dw_hdmi_phy_enable_powerdown(hdmi, true);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200938
939 /* Enable CSC */
940 ret = hdmi_phy_configure(hdmi, 0, 8, cscon);
941 if (ret)
942 return ret;
943 }
944
945 hdmi->phy_enabled = true;
946 return 0;
947}
948
Andy Yanb21f4b62014-12-05 14:26:31 +0800949static void hdmi_tx_hdcp_config(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200950{
Russell King812bc612013-11-04 12:42:02 +0000951 u8 de;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200952
953 if (hdmi->hdmi_data.video_mode.mdataenablepolarity)
954 de = HDMI_A_VIDPOLCFG_DATAENPOL_ACTIVE_HIGH;
955 else
956 de = HDMI_A_VIDPOLCFG_DATAENPOL_ACTIVE_LOW;
957
958 /* disable rx detect */
Russell King812bc612013-11-04 12:42:02 +0000959 hdmi_modb(hdmi, HDMI_A_HDCPCFG0_RXDETECT_DISABLE,
960 HDMI_A_HDCPCFG0_RXDETECT_MASK, HDMI_A_HDCPCFG0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200961
Russell King812bc612013-11-04 12:42:02 +0000962 hdmi_modb(hdmi, de, HDMI_A_VIDPOLCFG_DATAENPOL_MASK, HDMI_A_VIDPOLCFG);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200963
Russell King812bc612013-11-04 12:42:02 +0000964 hdmi_modb(hdmi, HDMI_A_HDCPCFG1_ENCRYPTIONDISABLE_DISABLE,
965 HDMI_A_HDCPCFG1_ENCRYPTIONDISABLE_MASK, HDMI_A_HDCPCFG1);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200966}
967
Russell Kingd4ac4cb2015-03-27 20:06:50 +0000968static void hdmi_config_AVI(struct dw_hdmi *hdmi, struct drm_display_mode *mode)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200969{
Russell Kingd4ac4cb2015-03-27 20:06:50 +0000970 struct hdmi_avi_infoframe frame;
971 u8 val;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200972
Russell Kingd4ac4cb2015-03-27 20:06:50 +0000973 /* Initialise info frame from DRM mode */
974 drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200975
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200976 if (hdmi->hdmi_data.enc_out_format == YCBCR444)
Russell Kingd4ac4cb2015-03-27 20:06:50 +0000977 frame.colorspace = HDMI_COLORSPACE_YUV444;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200978 else if (hdmi->hdmi_data.enc_out_format == YCBCR422_8BITS)
Russell Kingd4ac4cb2015-03-27 20:06:50 +0000979 frame.colorspace = HDMI_COLORSPACE_YUV422;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200980 else
Russell Kingd4ac4cb2015-03-27 20:06:50 +0000981 frame.colorspace = HDMI_COLORSPACE_RGB;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200982
983 /* Set up colorimetry */
984 if (hdmi->hdmi_data.enc_out_format == XVYCC444) {
Russell Kingd4ac4cb2015-03-27 20:06:50 +0000985 frame.colorimetry = HDMI_COLORIMETRY_EXTENDED;
Sachin Kamat5a819ed2014-01-28 10:33:16 +0530986 if (hdmi->hdmi_data.colorimetry == HDMI_COLORIMETRY_ITU_601)
Russell Kingd4ac4cb2015-03-27 20:06:50 +0000987 frame.extended_colorimetry =
988 HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
Sachin Kamat5a819ed2014-01-28 10:33:16 +0530989 else /*hdmi->hdmi_data.colorimetry == HDMI_COLORIMETRY_ITU_709*/
Russell Kingd4ac4cb2015-03-27 20:06:50 +0000990 frame.extended_colorimetry =
991 HDMI_EXTENDED_COLORIMETRY_XV_YCC_709;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200992 } else if (hdmi->hdmi_data.enc_out_format != RGB) {
Russell Kingd083c312015-03-27 23:14:16 +0000993 frame.colorimetry = hdmi->hdmi_data.colorimetry;
Russell Kingd4ac4cb2015-03-27 20:06:50 +0000994 frame.extended_colorimetry = HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200995 } else { /* Carries no data */
Russell Kingd4ac4cb2015-03-27 20:06:50 +0000996 frame.colorimetry = HDMI_COLORIMETRY_NONE;
997 frame.extended_colorimetry = HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200998 }
999
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001000 frame.scan_mode = HDMI_SCAN_MODE_NONE;
1001
1002 /*
1003 * The Designware IP uses a different byte format from standard
1004 * AVI info frames, though generally the bits are in the correct
1005 * bytes.
1006 */
1007
1008 /*
1009 * AVI data byte 1 differences: Colorspace in bits 4,5 rather than 5,6,
1010 * active aspect present in bit 6 rather than 4.
1011 */
1012 val = (frame.colorspace & 3) << 4 | (frame.scan_mode & 0x3);
1013 if (frame.active_aspect & 15)
1014 val |= HDMI_FC_AVICONF0_ACTIVE_FMT_INFO_PRESENT;
1015 if (frame.top_bar || frame.bottom_bar)
1016 val |= HDMI_FC_AVICONF0_BAR_DATA_HORIZ_BAR;
1017 if (frame.left_bar || frame.right_bar)
1018 val |= HDMI_FC_AVICONF0_BAR_DATA_VERT_BAR;
1019 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF0);
1020
1021 /* AVI data byte 2 differences: none */
1022 val = ((frame.colorimetry & 0x3) << 6) |
1023 ((frame.picture_aspect & 0x3) << 4) |
1024 (frame.active_aspect & 0xf);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001025 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF1);
1026
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001027 /* AVI data byte 3 differences: none */
1028 val = ((frame.extended_colorimetry & 0x7) << 4) |
1029 ((frame.quantization_range & 0x3) << 2) |
1030 (frame.nups & 0x3);
1031 if (frame.itc)
1032 val |= HDMI_FC_AVICONF2_IT_CONTENT_VALID;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001033 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF2);
1034
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001035 /* AVI data byte 4 differences: none */
1036 val = frame.video_code & 0x7f;
1037 hdmi_writeb(hdmi, val, HDMI_FC_AVIVID);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001038
1039 /* AVI Data Byte 5- set up input and output pixel repetition */
1040 val = (((hdmi->hdmi_data.video_mode.mpixelrepetitioninput + 1) <<
1041 HDMI_FC_PRCONF_INCOMING_PR_FACTOR_OFFSET) &
1042 HDMI_FC_PRCONF_INCOMING_PR_FACTOR_MASK) |
1043 ((hdmi->hdmi_data.video_mode.mpixelrepetitionoutput <<
1044 HDMI_FC_PRCONF_OUTPUT_PR_FACTOR_OFFSET) &
1045 HDMI_FC_PRCONF_OUTPUT_PR_FACTOR_MASK);
1046 hdmi_writeb(hdmi, val, HDMI_FC_PRCONF);
1047
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001048 /*
1049 * AVI data byte 5 differences: content type in 0,1 rather than 4,5,
1050 * ycc range in bits 2,3 rather than 6,7
1051 */
1052 val = ((frame.ycc_quantization_range & 0x3) << 2) |
1053 (frame.content_type & 0x3);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001054 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF3);
1055
1056 /* AVI Data Bytes 6-13 */
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001057 hdmi_writeb(hdmi, frame.top_bar & 0xff, HDMI_FC_AVIETB0);
1058 hdmi_writeb(hdmi, (frame.top_bar >> 8) & 0xff, HDMI_FC_AVIETB1);
1059 hdmi_writeb(hdmi, frame.bottom_bar & 0xff, HDMI_FC_AVISBB0);
1060 hdmi_writeb(hdmi, (frame.bottom_bar >> 8) & 0xff, HDMI_FC_AVISBB1);
1061 hdmi_writeb(hdmi, frame.left_bar & 0xff, HDMI_FC_AVIELB0);
1062 hdmi_writeb(hdmi, (frame.left_bar >> 8) & 0xff, HDMI_FC_AVIELB1);
1063 hdmi_writeb(hdmi, frame.right_bar & 0xff, HDMI_FC_AVISRB0);
1064 hdmi_writeb(hdmi, (frame.right_bar >> 8) & 0xff, HDMI_FC_AVISRB1);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001065}
1066
Andy Yanb21f4b62014-12-05 14:26:31 +08001067static void hdmi_av_composer(struct dw_hdmi *hdmi,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001068 const struct drm_display_mode *mode)
1069{
1070 u8 inv_val;
1071 struct hdmi_vmode *vmode = &hdmi->hdmi_data.video_mode;
1072 int hblank, vblank, h_de_hs, v_de_vs, hsync_len, vsync_len;
Russell Kinge80b9f42015-07-21 11:08:25 +01001073 unsigned int vdisplay;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001074
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001075 vmode->mpixelclock = mode->clock * 1000;
1076
1077 dev_dbg(hdmi->dev, "final pixclk = %d\n", vmode->mpixelclock);
1078
1079 /* Set up HDMI_FC_INVIDCONF */
1080 inv_val = (hdmi->hdmi_data.hdcp_enable ?
1081 HDMI_FC_INVIDCONF_HDCP_KEEPOUT_ACTIVE :
1082 HDMI_FC_INVIDCONF_HDCP_KEEPOUT_INACTIVE);
1083
Russell Kingb91eee82015-03-27 23:27:17 +00001084 inv_val |= mode->flags & DRM_MODE_FLAG_PVSYNC ?
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001085 HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_HIGH :
Russell Kingb91eee82015-03-27 23:27:17 +00001086 HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_LOW;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001087
Russell Kingb91eee82015-03-27 23:27:17 +00001088 inv_val |= mode->flags & DRM_MODE_FLAG_PHSYNC ?
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001089 HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_HIGH :
Russell Kingb91eee82015-03-27 23:27:17 +00001090 HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_LOW;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001091
1092 inv_val |= (vmode->mdataenablepolarity ?
1093 HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_HIGH :
1094 HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_LOW);
1095
1096 if (hdmi->vic == 39)
1097 inv_val |= HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_HIGH;
1098 else
Russell Kingb91eee82015-03-27 23:27:17 +00001099 inv_val |= mode->flags & DRM_MODE_FLAG_INTERLACE ?
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001100 HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_HIGH :
Russell Kingb91eee82015-03-27 23:27:17 +00001101 HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_LOW;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001102
Russell Kingb91eee82015-03-27 23:27:17 +00001103 inv_val |= mode->flags & DRM_MODE_FLAG_INTERLACE ?
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001104 HDMI_FC_INVIDCONF_IN_I_P_INTERLACED :
Russell Kingb91eee82015-03-27 23:27:17 +00001105 HDMI_FC_INVIDCONF_IN_I_P_PROGRESSIVE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001106
Russell King05b13422015-07-21 15:35:52 +01001107 inv_val |= hdmi->sink_is_hdmi ?
1108 HDMI_FC_INVIDCONF_DVI_MODEZ_HDMI_MODE :
1109 HDMI_FC_INVIDCONF_DVI_MODEZ_DVI_MODE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001110
1111 hdmi_writeb(hdmi, inv_val, HDMI_FC_INVIDCONF);
1112
Russell Kinge80b9f42015-07-21 11:08:25 +01001113 vdisplay = mode->vdisplay;
1114 vblank = mode->vtotal - mode->vdisplay;
1115 v_de_vs = mode->vsync_start - mode->vdisplay;
1116 vsync_len = mode->vsync_end - mode->vsync_start;
1117
1118 /*
1119 * When we're setting an interlaced mode, we need
1120 * to adjust the vertical timing to suit.
1121 */
1122 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
1123 vdisplay /= 2;
1124 vblank /= 2;
1125 v_de_vs /= 2;
1126 vsync_len /= 2;
1127 }
1128
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001129 /* Set up horizontal active pixel width */
1130 hdmi_writeb(hdmi, mode->hdisplay >> 8, HDMI_FC_INHACTV1);
1131 hdmi_writeb(hdmi, mode->hdisplay, HDMI_FC_INHACTV0);
1132
1133 /* Set up vertical active lines */
Russell Kinge80b9f42015-07-21 11:08:25 +01001134 hdmi_writeb(hdmi, vdisplay >> 8, HDMI_FC_INVACTV1);
1135 hdmi_writeb(hdmi, vdisplay, HDMI_FC_INVACTV0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001136
1137 /* Set up horizontal blanking pixel region width */
1138 hblank = mode->htotal - mode->hdisplay;
1139 hdmi_writeb(hdmi, hblank >> 8, HDMI_FC_INHBLANK1);
1140 hdmi_writeb(hdmi, hblank, HDMI_FC_INHBLANK0);
1141
1142 /* Set up vertical blanking pixel region width */
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001143 hdmi_writeb(hdmi, vblank, HDMI_FC_INVBLANK);
1144
1145 /* Set up HSYNC active edge delay width (in pixel clks) */
1146 h_de_hs = mode->hsync_start - mode->hdisplay;
1147 hdmi_writeb(hdmi, h_de_hs >> 8, HDMI_FC_HSYNCINDELAY1);
1148 hdmi_writeb(hdmi, h_de_hs, HDMI_FC_HSYNCINDELAY0);
1149
1150 /* Set up VSYNC active edge delay (in lines) */
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001151 hdmi_writeb(hdmi, v_de_vs, HDMI_FC_VSYNCINDELAY);
1152
1153 /* Set up HSYNC active pulse width (in pixel clks) */
1154 hsync_len = mode->hsync_end - mode->hsync_start;
1155 hdmi_writeb(hdmi, hsync_len >> 8, HDMI_FC_HSYNCINWIDTH1);
1156 hdmi_writeb(hdmi, hsync_len, HDMI_FC_HSYNCINWIDTH0);
1157
1158 /* Set up VSYNC active edge delay (in lines) */
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001159 hdmi_writeb(hdmi, vsync_len, HDMI_FC_VSYNCINWIDTH);
1160}
1161
Andy Yanb21f4b62014-12-05 14:26:31 +08001162static void dw_hdmi_phy_disable(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001163{
1164 if (!hdmi->phy_enabled)
1165 return;
1166
Andy Yanb21f4b62014-12-05 14:26:31 +08001167 dw_hdmi_phy_enable_tmds(hdmi, 0);
Russell King2fada102015-07-28 12:21:34 +01001168 dw_hdmi_phy_enable_powerdown(hdmi, true);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001169
1170 hdmi->phy_enabled = false;
1171}
1172
1173/* HDMI Initialization Step B.4 */
Andy Yanb21f4b62014-12-05 14:26:31 +08001174static void dw_hdmi_enable_video_path(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001175{
1176 u8 clkdis;
1177
1178 /* control period minimum duration */
1179 hdmi_writeb(hdmi, 12, HDMI_FC_CTRLDUR);
1180 hdmi_writeb(hdmi, 32, HDMI_FC_EXCTRLDUR);
1181 hdmi_writeb(hdmi, 1, HDMI_FC_EXCTRLSPAC);
1182
1183 /* Set to fill TMDS data channels */
1184 hdmi_writeb(hdmi, 0x0B, HDMI_FC_CH0PREAM);
1185 hdmi_writeb(hdmi, 0x16, HDMI_FC_CH1PREAM);
1186 hdmi_writeb(hdmi, 0x21, HDMI_FC_CH2PREAM);
1187
1188 /* Enable pixel clock and tmds data path */
1189 clkdis = 0x7F;
1190 clkdis &= ~HDMI_MC_CLKDIS_PIXELCLK_DISABLE;
1191 hdmi_writeb(hdmi, clkdis, HDMI_MC_CLKDIS);
1192
1193 clkdis &= ~HDMI_MC_CLKDIS_TMDSCLK_DISABLE;
1194 hdmi_writeb(hdmi, clkdis, HDMI_MC_CLKDIS);
1195
1196 /* Enable csc path */
1197 if (is_color_space_conversion(hdmi)) {
1198 clkdis &= ~HDMI_MC_CLKDIS_CSCCLK_DISABLE;
1199 hdmi_writeb(hdmi, clkdis, HDMI_MC_CLKDIS);
1200 }
1201}
1202
Andy Yanb21f4b62014-12-05 14:26:31 +08001203static void hdmi_enable_audio_clk(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001204{
Russell King812bc612013-11-04 12:42:02 +00001205 hdmi_modb(hdmi, 0, HDMI_MC_CLKDIS_AUDCLK_DISABLE, HDMI_MC_CLKDIS);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001206}
1207
1208/* Workaround to clear the overflow condition */
Andy Yanb21f4b62014-12-05 14:26:31 +08001209static void dw_hdmi_clear_overflow(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001210{
1211 int count;
1212 u8 val;
1213
1214 /* TMDS software reset */
1215 hdmi_writeb(hdmi, (u8)~HDMI_MC_SWRSTZ_TMDSSWRST_REQ, HDMI_MC_SWRSTZ);
1216
1217 val = hdmi_readb(hdmi, HDMI_FC_INVIDCONF);
1218 if (hdmi->dev_type == IMX6DL_HDMI) {
1219 hdmi_writeb(hdmi, val, HDMI_FC_INVIDCONF);
1220 return;
1221 }
1222
1223 for (count = 0; count < 4; count++)
1224 hdmi_writeb(hdmi, val, HDMI_FC_INVIDCONF);
1225}
1226
Andy Yanb21f4b62014-12-05 14:26:31 +08001227static void hdmi_enable_overflow_interrupts(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001228{
1229 hdmi_writeb(hdmi, 0, HDMI_FC_MASK2);
1230 hdmi_writeb(hdmi, 0, HDMI_IH_MUTE_FC_STAT2);
1231}
1232
Andy Yanb21f4b62014-12-05 14:26:31 +08001233static void hdmi_disable_overflow_interrupts(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001234{
1235 hdmi_writeb(hdmi, HDMI_IH_MUTE_FC_STAT2_OVERFLOW_MASK,
1236 HDMI_IH_MUTE_FC_STAT2);
1237}
1238
Andy Yanb21f4b62014-12-05 14:26:31 +08001239static int dw_hdmi_setup(struct dw_hdmi *hdmi, struct drm_display_mode *mode)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001240{
1241 int ret;
1242
1243 hdmi_disable_overflow_interrupts(hdmi);
1244
1245 hdmi->vic = drm_match_cea_mode(mode);
1246
1247 if (!hdmi->vic) {
1248 dev_dbg(hdmi->dev, "Non-CEA mode used in HDMI\n");
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001249 } else {
1250 dev_dbg(hdmi->dev, "CEA mode used vic=%d\n", hdmi->vic);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001251 }
1252
1253 if ((hdmi->vic == 6) || (hdmi->vic == 7) ||
Andy Yanb5878332014-12-05 14:23:52 +08001254 (hdmi->vic == 21) || (hdmi->vic == 22) ||
1255 (hdmi->vic == 2) || (hdmi->vic == 3) ||
1256 (hdmi->vic == 17) || (hdmi->vic == 18))
Sachin Kamat5a819ed2014-01-28 10:33:16 +05301257 hdmi->hdmi_data.colorimetry = HDMI_COLORIMETRY_ITU_601;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001258 else
Sachin Kamat5a819ed2014-01-28 10:33:16 +05301259 hdmi->hdmi_data.colorimetry = HDMI_COLORIMETRY_ITU_709;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001260
Russell Kingd10ca822015-07-21 11:25:00 +01001261 hdmi->hdmi_data.video_mode.mpixelrepetitionoutput = 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001262 hdmi->hdmi_data.video_mode.mpixelrepetitioninput = 0;
1263
1264 /* TODO: Get input format from IPU (via FB driver interface) */
1265 hdmi->hdmi_data.enc_in_format = RGB;
1266
1267 hdmi->hdmi_data.enc_out_format = RGB;
1268
1269 hdmi->hdmi_data.enc_color_depth = 8;
1270 hdmi->hdmi_data.pix_repet_factor = 0;
1271 hdmi->hdmi_data.hdcp_enable = 0;
1272 hdmi->hdmi_data.video_mode.mdataenablepolarity = true;
1273
1274 /* HDMI Initialization Step B.1 */
1275 hdmi_av_composer(hdmi, mode);
1276
1277 /* HDMI Initializateion Step B.2 */
Andy Yanb21f4b62014-12-05 14:26:31 +08001278 ret = dw_hdmi_phy_init(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001279 if (ret)
1280 return ret;
1281
1282 /* HDMI Initialization Step B.3 */
Andy Yanb21f4b62014-12-05 14:26:31 +08001283 dw_hdmi_enable_video_path(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001284
Russell Kingf709ec02015-07-21 16:09:39 +01001285 if (hdmi->sink_has_audio) {
1286 dev_dbg(hdmi->dev, "sink has audio support\n");
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001287
1288 /* HDMI Initialization Step E - Configure audio */
1289 hdmi_clk_regenerator_update_pixel_clock(hdmi);
1290 hdmi_enable_audio_clk(hdmi);
Russell Kingf709ec02015-07-21 16:09:39 +01001291 }
1292
1293 /* not for DVI mode */
1294 if (hdmi->sink_is_hdmi) {
1295 dev_dbg(hdmi->dev, "%s HDMI mode\n", __func__);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001296
1297 /* HDMI Initialization Step F - Configure AVI InfoFrame */
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001298 hdmi_config_AVI(hdmi, mode);
Russell King05b13422015-07-21 15:35:52 +01001299 } else {
1300 dev_dbg(hdmi->dev, "%s DVI mode\n", __func__);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001301 }
1302
1303 hdmi_video_packetize(hdmi);
1304 hdmi_video_csc(hdmi);
1305 hdmi_video_sample(hdmi);
1306 hdmi_tx_hdcp_config(hdmi);
1307
Andy Yanb21f4b62014-12-05 14:26:31 +08001308 dw_hdmi_clear_overflow(hdmi);
Russell King05b13422015-07-21 15:35:52 +01001309 if (hdmi->cable_plugin && hdmi->sink_is_hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001310 hdmi_enable_overflow_interrupts(hdmi);
1311
1312 return 0;
1313}
1314
1315/* Wait until we are registered to enable interrupts */
Andy Yanb21f4b62014-12-05 14:26:31 +08001316static int dw_hdmi_fb_registered(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001317{
1318 hdmi_writeb(hdmi, HDMI_PHY_I2CM_INT_ADDR_DONE_POL,
1319 HDMI_PHY_I2CM_INT_ADDR);
1320
1321 hdmi_writeb(hdmi, HDMI_PHY_I2CM_CTLINT_ADDR_NAC_POL |
1322 HDMI_PHY_I2CM_CTLINT_ADDR_ARBITRATION_POL,
1323 HDMI_PHY_I2CM_CTLINT_ADDR);
1324
1325 /* enable cable hot plug irq */
Russell Kingaeac23b2015-06-05 13:46:22 +01001326 hdmi_writeb(hdmi, hdmi->phy_mask, HDMI_PHY_MASK0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001327
1328 /* Clear Hotplug interrupts */
Russell Kingaeac23b2015-06-05 13:46:22 +01001329 hdmi_writeb(hdmi, HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE,
1330 HDMI_IH_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001331
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001332 return 0;
1333}
1334
Andy Yanb21f4b62014-12-05 14:26:31 +08001335static void initialize_hdmi_ih_mutes(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001336{
1337 u8 ih_mute;
1338
1339 /*
1340 * Boot up defaults are:
1341 * HDMI_IH_MUTE = 0x03 (disabled)
1342 * HDMI_IH_MUTE_* = 0x00 (enabled)
1343 *
1344 * Disable top level interrupt bits in HDMI block
1345 */
1346 ih_mute = hdmi_readb(hdmi, HDMI_IH_MUTE) |
1347 HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |
1348 HDMI_IH_MUTE_MUTE_ALL_INTERRUPT;
1349
1350 hdmi_writeb(hdmi, ih_mute, HDMI_IH_MUTE);
1351
1352 /* by default mask all interrupts */
1353 hdmi_writeb(hdmi, 0xff, HDMI_VP_MASK);
1354 hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK0);
1355 hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK1);
1356 hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK2);
1357 hdmi_writeb(hdmi, 0xff, HDMI_PHY_MASK0);
1358 hdmi_writeb(hdmi, 0xff, HDMI_PHY_I2CM_INT_ADDR);
1359 hdmi_writeb(hdmi, 0xff, HDMI_PHY_I2CM_CTLINT_ADDR);
1360 hdmi_writeb(hdmi, 0xff, HDMI_AUD_INT);
1361 hdmi_writeb(hdmi, 0xff, HDMI_AUD_SPDIFINT);
1362 hdmi_writeb(hdmi, 0xff, HDMI_AUD_HBR_MASK);
1363 hdmi_writeb(hdmi, 0xff, HDMI_GP_MASK);
1364 hdmi_writeb(hdmi, 0xff, HDMI_A_APIINTMSK);
1365 hdmi_writeb(hdmi, 0xff, HDMI_CEC_MASK);
1366 hdmi_writeb(hdmi, 0xff, HDMI_I2CM_INT);
1367 hdmi_writeb(hdmi, 0xff, HDMI_I2CM_CTLINT);
1368
1369 /* Disable interrupts in the IH_MUTE_* registers */
1370 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT0);
1371 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT1);
1372 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT2);
1373 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_AS_STAT0);
1374 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_PHY_STAT0);
1375 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_I2CM_STAT0);
1376 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_CEC_STAT0);
1377 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_VP_STAT0);
1378 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_I2CMPHY_STAT0);
1379 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_AHBDMAAUD_STAT0);
1380
1381 /* Enable top level interrupt bits in HDMI block */
1382 ih_mute &= ~(HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |
1383 HDMI_IH_MUTE_MUTE_ALL_INTERRUPT);
1384 hdmi_writeb(hdmi, ih_mute, HDMI_IH_MUTE);
1385}
1386
Andy Yanb21f4b62014-12-05 14:26:31 +08001387static void dw_hdmi_poweron(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001388{
Russell King381f05a2015-06-05 15:25:08 +01001389 hdmi->bridge_is_on = true;
Andy Yanb21f4b62014-12-05 14:26:31 +08001390 dw_hdmi_setup(hdmi, &hdmi->previous_mode);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001391}
1392
Andy Yanb21f4b62014-12-05 14:26:31 +08001393static void dw_hdmi_poweroff(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001394{
Andy Yanb21f4b62014-12-05 14:26:31 +08001395 dw_hdmi_phy_disable(hdmi);
Russell King381f05a2015-06-05 15:25:08 +01001396 hdmi->bridge_is_on = false;
1397}
1398
1399static void dw_hdmi_update_power(struct dw_hdmi *hdmi)
1400{
1401 int force = hdmi->force;
1402
1403 if (hdmi->disabled) {
1404 force = DRM_FORCE_OFF;
1405 } else if (force == DRM_FORCE_UNSPECIFIED) {
Russell Kingaeac23b2015-06-05 13:46:22 +01001406 if (hdmi->rxsense)
Russell King381f05a2015-06-05 15:25:08 +01001407 force = DRM_FORCE_ON;
1408 else
1409 force = DRM_FORCE_OFF;
1410 }
1411
1412 if (force == DRM_FORCE_OFF) {
1413 if (hdmi->bridge_is_on)
1414 dw_hdmi_poweroff(hdmi);
1415 } else {
1416 if (!hdmi->bridge_is_on)
1417 dw_hdmi_poweron(hdmi);
1418 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001419}
1420
Russell Kingaeac23b2015-06-05 13:46:22 +01001421/*
1422 * Adjust the detection of RXSENSE according to whether we have a forced
1423 * connection mode enabled, or whether we have been disabled. There is
1424 * no point processing RXSENSE interrupts if we have a forced connection
1425 * state, or DRM has us disabled.
1426 *
1427 * We also disable rxsense interrupts when we think we're disconnected
1428 * to avoid floating TDMS signals giving false rxsense interrupts.
1429 *
1430 * Note: we still need to listen for HPD interrupts even when DRM has us
1431 * disabled so that we can detect a connect event.
1432 */
1433static void dw_hdmi_update_phy_mask(struct dw_hdmi *hdmi)
1434{
1435 u8 old_mask = hdmi->phy_mask;
1436
1437 if (hdmi->force || hdmi->disabled || !hdmi->rxsense)
1438 hdmi->phy_mask |= HDMI_PHY_RX_SENSE;
1439 else
1440 hdmi->phy_mask &= ~HDMI_PHY_RX_SENSE;
1441
1442 if (old_mask != hdmi->phy_mask)
1443 hdmi_writeb(hdmi, hdmi->phy_mask, HDMI_PHY_MASK0);
1444}
1445
Andy Yanb21f4b62014-12-05 14:26:31 +08001446static void dw_hdmi_bridge_mode_set(struct drm_bridge *bridge,
Steve Longerbeameb10d632014-12-18 18:00:24 -08001447 struct drm_display_mode *orig_mode,
1448 struct drm_display_mode *mode)
Andy Yan3d1b35a2014-12-05 14:25:05 +08001449{
Andy Yanb21f4b62014-12-05 14:26:31 +08001450 struct dw_hdmi *hdmi = bridge->driver_private;
Andy Yan3d1b35a2014-12-05 14:25:05 +08001451
Russell Kingb872a8e2015-06-05 12:22:46 +01001452 mutex_lock(&hdmi->mutex);
Andy Yan3d1b35a2014-12-05 14:25:05 +08001453
1454 /* Store the display mode for plugin/DKMS poweron events */
1455 memcpy(&hdmi->previous_mode, mode, sizeof(hdmi->previous_mode));
Russell Kingb872a8e2015-06-05 12:22:46 +01001456
1457 mutex_unlock(&hdmi->mutex);
Andy Yan3d1b35a2014-12-05 14:25:05 +08001458}
1459
Andy Yanb21f4b62014-12-05 14:26:31 +08001460static bool dw_hdmi_bridge_mode_fixup(struct drm_bridge *bridge,
1461 const struct drm_display_mode *mode,
1462 struct drm_display_mode *adjusted_mode)
Andy Yan3d1b35a2014-12-05 14:25:05 +08001463{
1464 return true;
1465}
1466
Andy Yanb21f4b62014-12-05 14:26:31 +08001467static void dw_hdmi_bridge_disable(struct drm_bridge *bridge)
Andy Yan3d1b35a2014-12-05 14:25:05 +08001468{
Andy Yanb21f4b62014-12-05 14:26:31 +08001469 struct dw_hdmi *hdmi = bridge->driver_private;
Andy Yan3d1b35a2014-12-05 14:25:05 +08001470
Russell Kingb872a8e2015-06-05 12:22:46 +01001471 mutex_lock(&hdmi->mutex);
1472 hdmi->disabled = true;
Russell King381f05a2015-06-05 15:25:08 +01001473 dw_hdmi_update_power(hdmi);
Russell Kingaeac23b2015-06-05 13:46:22 +01001474 dw_hdmi_update_phy_mask(hdmi);
Russell Kingb872a8e2015-06-05 12:22:46 +01001475 mutex_unlock(&hdmi->mutex);
Andy Yan3d1b35a2014-12-05 14:25:05 +08001476}
1477
Andy Yanb21f4b62014-12-05 14:26:31 +08001478static void dw_hdmi_bridge_enable(struct drm_bridge *bridge)
Andy Yan3d1b35a2014-12-05 14:25:05 +08001479{
Andy Yanb21f4b62014-12-05 14:26:31 +08001480 struct dw_hdmi *hdmi = bridge->driver_private;
Andy Yan3d1b35a2014-12-05 14:25:05 +08001481
Russell Kingb872a8e2015-06-05 12:22:46 +01001482 mutex_lock(&hdmi->mutex);
Russell Kingb872a8e2015-06-05 12:22:46 +01001483 hdmi->disabled = false;
Russell King381f05a2015-06-05 15:25:08 +01001484 dw_hdmi_update_power(hdmi);
Russell Kingaeac23b2015-06-05 13:46:22 +01001485 dw_hdmi_update_phy_mask(hdmi);
Russell Kingb872a8e2015-06-05 12:22:46 +01001486 mutex_unlock(&hdmi->mutex);
Andy Yan3d1b35a2014-12-05 14:25:05 +08001487}
1488
Andy Yanb21f4b62014-12-05 14:26:31 +08001489static void dw_hdmi_bridge_nop(struct drm_bridge *bridge)
Andy Yan3d1b35a2014-12-05 14:25:05 +08001490{
1491 /* do nothing */
1492}
1493
Andy Yanb21f4b62014-12-05 14:26:31 +08001494static enum drm_connector_status
1495dw_hdmi_connector_detect(struct drm_connector *connector, bool force)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001496{
Andy Yanb21f4b62014-12-05 14:26:31 +08001497 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
Russell Kingd94905e2013-11-03 22:23:24 +00001498 connector);
Russell King98dbead2014-04-18 10:46:45 +01001499
Russell King381f05a2015-06-05 15:25:08 +01001500 mutex_lock(&hdmi->mutex);
1501 hdmi->force = DRM_FORCE_UNSPECIFIED;
1502 dw_hdmi_update_power(hdmi);
Russell Kingaeac23b2015-06-05 13:46:22 +01001503 dw_hdmi_update_phy_mask(hdmi);
Russell King381f05a2015-06-05 15:25:08 +01001504 mutex_unlock(&hdmi->mutex);
1505
Russell King98dbead2014-04-18 10:46:45 +01001506 return hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_HPD ?
1507 connector_status_connected : connector_status_disconnected;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001508}
1509
Andy Yanb21f4b62014-12-05 14:26:31 +08001510static int dw_hdmi_connector_get_modes(struct drm_connector *connector)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001511{
Andy Yanb21f4b62014-12-05 14:26:31 +08001512 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001513 connector);
1514 struct edid *edid;
Doug Anderson6c7e66e2015-06-04 11:04:36 -07001515 int ret = 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001516
1517 if (!hdmi->ddc)
1518 return 0;
1519
1520 edid = drm_get_edid(connector, hdmi->ddc);
1521 if (edid) {
1522 dev_dbg(hdmi->dev, "got edid: width[%d] x height[%d]\n",
1523 edid->width_cm, edid->height_cm);
1524
Russell King05b13422015-07-21 15:35:52 +01001525 hdmi->sink_is_hdmi = drm_detect_hdmi_monitor(edid);
Russell Kingf709ec02015-07-21 16:09:39 +01001526 hdmi->sink_has_audio = drm_detect_monitor_audio(edid);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001527 drm_mode_connector_update_edid_property(connector, edid);
1528 ret = drm_add_edid_modes(connector, edid);
Russell Kingf5ce4052013-11-07 16:06:01 +00001529 /* Store the ELD */
1530 drm_edid_to_eld(connector, edid);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001531 kfree(edid);
1532 } else {
1533 dev_dbg(hdmi->dev, "failed to get edid\n");
1534 }
1535
Doug Anderson6c7e66e2015-06-04 11:04:36 -07001536 return ret;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001537}
1538
Andy Yan632d0352014-12-05 14:30:21 +08001539static enum drm_mode_status
1540dw_hdmi_connector_mode_valid(struct drm_connector *connector,
1541 struct drm_display_mode *mode)
1542{
1543 struct dw_hdmi *hdmi = container_of(connector,
1544 struct dw_hdmi, connector);
1545 enum drm_mode_status mode_status = MODE_OK;
1546
Russell King8add4192015-07-22 11:14:00 +01001547 /* We don't support double-clocked modes */
1548 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
1549 return MODE_BAD;
1550
Andy Yan632d0352014-12-05 14:30:21 +08001551 if (hdmi->plat_data->mode_valid)
1552 mode_status = hdmi->plat_data->mode_valid(connector, mode);
1553
1554 return mode_status;
1555}
1556
Andy Yanb21f4b62014-12-05 14:26:31 +08001557static struct drm_encoder *dw_hdmi_connector_best_encoder(struct drm_connector
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001558 *connector)
1559{
Andy Yanb21f4b62014-12-05 14:26:31 +08001560 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001561 connector);
1562
Andy Yan3d1b35a2014-12-05 14:25:05 +08001563 return hdmi->encoder;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001564}
1565
Andy Yanb21f4b62014-12-05 14:26:31 +08001566static void dw_hdmi_connector_destroy(struct drm_connector *connector)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001567{
Andy Yan3d1b35a2014-12-05 14:25:05 +08001568 drm_connector_unregister(connector);
1569 drm_connector_cleanup(connector);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001570}
1571
Russell King381f05a2015-06-05 15:25:08 +01001572static void dw_hdmi_connector_force(struct drm_connector *connector)
1573{
1574 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
1575 connector);
1576
1577 mutex_lock(&hdmi->mutex);
1578 hdmi->force = connector->force;
1579 dw_hdmi_update_power(hdmi);
Russell Kingaeac23b2015-06-05 13:46:22 +01001580 dw_hdmi_update_phy_mask(hdmi);
Russell King381f05a2015-06-05 15:25:08 +01001581 mutex_unlock(&hdmi->mutex);
1582}
1583
Andy Yanb21f4b62014-12-05 14:26:31 +08001584static struct drm_connector_funcs dw_hdmi_connector_funcs = {
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001585 .dpms = drm_helper_connector_dpms,
1586 .fill_modes = drm_helper_probe_single_connector_modes,
Andy Yanb21f4b62014-12-05 14:26:31 +08001587 .detect = dw_hdmi_connector_detect,
1588 .destroy = dw_hdmi_connector_destroy,
Russell King381f05a2015-06-05 15:25:08 +01001589 .force = dw_hdmi_connector_force,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001590};
1591
Andy Yanb21f4b62014-12-05 14:26:31 +08001592static struct drm_connector_helper_funcs dw_hdmi_connector_helper_funcs = {
1593 .get_modes = dw_hdmi_connector_get_modes,
Andy Yan632d0352014-12-05 14:30:21 +08001594 .mode_valid = dw_hdmi_connector_mode_valid,
Andy Yanb21f4b62014-12-05 14:26:31 +08001595 .best_encoder = dw_hdmi_connector_best_encoder,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001596};
1597
Fabio Estevamcf88fca2015-04-02 19:11:04 -03001598static struct drm_bridge_funcs dw_hdmi_bridge_funcs = {
Andy Yanb21f4b62014-12-05 14:26:31 +08001599 .enable = dw_hdmi_bridge_enable,
1600 .disable = dw_hdmi_bridge_disable,
1601 .pre_enable = dw_hdmi_bridge_nop,
1602 .post_disable = dw_hdmi_bridge_nop,
1603 .mode_set = dw_hdmi_bridge_mode_set,
1604 .mode_fixup = dw_hdmi_bridge_mode_fixup,
Andy Yan3d1b35a2014-12-05 14:25:05 +08001605};
1606
Andy Yanb21f4b62014-12-05 14:26:31 +08001607static irqreturn_t dw_hdmi_hardirq(int irq, void *dev_id)
Russell Kingd94905e2013-11-03 22:23:24 +00001608{
Andy Yanb21f4b62014-12-05 14:26:31 +08001609 struct dw_hdmi *hdmi = dev_id;
Russell Kingd94905e2013-11-03 22:23:24 +00001610 u8 intr_stat;
1611
1612 intr_stat = hdmi_readb(hdmi, HDMI_IH_PHY_STAT0);
1613 if (intr_stat)
1614 hdmi_writeb(hdmi, ~0, HDMI_IH_MUTE_PHY_STAT0);
1615
1616 return intr_stat ? IRQ_WAKE_THREAD : IRQ_NONE;
1617}
1618
Andy Yanb21f4b62014-12-05 14:26:31 +08001619static irqreturn_t dw_hdmi_irq(int irq, void *dev_id)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001620{
Andy Yanb21f4b62014-12-05 14:26:31 +08001621 struct dw_hdmi *hdmi = dev_id;
Russell Kingaeac23b2015-06-05 13:46:22 +01001622 u8 intr_stat, phy_int_pol, phy_pol_mask, phy_stat;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001623
1624 intr_stat = hdmi_readb(hdmi, HDMI_IH_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001625 phy_int_pol = hdmi_readb(hdmi, HDMI_PHY_POL0);
Russell Kingaeac23b2015-06-05 13:46:22 +01001626 phy_stat = hdmi_readb(hdmi, HDMI_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001627
Russell Kingaeac23b2015-06-05 13:46:22 +01001628 phy_pol_mask = 0;
1629 if (intr_stat & HDMI_IH_PHY_STAT0_HPD)
1630 phy_pol_mask |= HDMI_PHY_HPD;
1631 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE0)
1632 phy_pol_mask |= HDMI_PHY_RX_SENSE0;
1633 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE1)
1634 phy_pol_mask |= HDMI_PHY_RX_SENSE1;
1635 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE2)
1636 phy_pol_mask |= HDMI_PHY_RX_SENSE2;
1637 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE3)
1638 phy_pol_mask |= HDMI_PHY_RX_SENSE3;
1639
1640 if (phy_pol_mask)
1641 hdmi_modb(hdmi, ~phy_int_pol, phy_pol_mask, HDMI_PHY_POL0);
1642
1643 /*
1644 * RX sense tells us whether the TDMS transmitters are detecting
1645 * load - in other words, there's something listening on the
1646 * other end of the link. Use this to decide whether we should
1647 * power on the phy as HPD may be toggled by the sink to merely
1648 * ask the source to re-read the EDID.
1649 */
1650 if (intr_stat &
1651 (HDMI_IH_PHY_STAT0_RX_SENSE | HDMI_IH_PHY_STAT0_HPD)) {
Russell Kingb872a8e2015-06-05 12:22:46 +01001652 mutex_lock(&hdmi->mutex);
Russell Kingaeac23b2015-06-05 13:46:22 +01001653 if (!hdmi->disabled && !hdmi->force) {
1654 /*
1655 * If the RX sense status indicates we're disconnected,
1656 * clear the software rxsense status.
1657 */
1658 if (!(phy_stat & HDMI_PHY_RX_SENSE))
1659 hdmi->rxsense = false;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001660
Russell Kingaeac23b2015-06-05 13:46:22 +01001661 /*
1662 * Only set the software rxsense status when both
1663 * rxsense and hpd indicates we're connected.
1664 * This avoids what seems to be bad behaviour in
1665 * at least iMX6S versions of the phy.
1666 */
1667 if (phy_stat & HDMI_PHY_HPD)
1668 hdmi->rxsense = true;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001669
Russell Kingaeac23b2015-06-05 13:46:22 +01001670 dw_hdmi_update_power(hdmi);
1671 dw_hdmi_update_phy_mask(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001672 }
Russell Kingb872a8e2015-06-05 12:22:46 +01001673 mutex_unlock(&hdmi->mutex);
Russell Kingaeac23b2015-06-05 13:46:22 +01001674 }
1675
1676 if (intr_stat & HDMI_IH_PHY_STAT0_HPD) {
1677 dev_dbg(hdmi->dev, "EVENT=%s\n",
1678 phy_int_pol & HDMI_PHY_HPD ? "plugin" : "plugout");
Russell King4b9bcaa2015-06-06 00:12:41 +01001679 drm_helper_hpd_irq_event(hdmi->bridge->dev);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001680 }
1681
1682 hdmi_writeb(hdmi, intr_stat, HDMI_IH_PHY_STAT0);
Russell Kingaeac23b2015-06-05 13:46:22 +01001683 hdmi_writeb(hdmi, ~(HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE),
1684 HDMI_IH_MUTE_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001685
1686 return IRQ_HANDLED;
1687}
1688
Andy Yanb21f4b62014-12-05 14:26:31 +08001689static int dw_hdmi_register(struct drm_device *drm, struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001690{
Andy Yan3d1b35a2014-12-05 14:25:05 +08001691 struct drm_encoder *encoder = hdmi->encoder;
1692 struct drm_bridge *bridge;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001693 int ret;
1694
Andy Yan3d1b35a2014-12-05 14:25:05 +08001695 bridge = devm_kzalloc(drm->dev, sizeof(*bridge), GFP_KERNEL);
1696 if (!bridge) {
1697 DRM_ERROR("Failed to allocate drm bridge\n");
1698 return -ENOMEM;
1699 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001700
Andy Yan3d1b35a2014-12-05 14:25:05 +08001701 hdmi->bridge = bridge;
1702 bridge->driver_private = hdmi;
Fabio Estevamb5217bf2015-01-27 10:21:49 -02001703 bridge->funcs = &dw_hdmi_bridge_funcs;
1704 ret = drm_bridge_attach(drm, bridge);
Andy Yan3d1b35a2014-12-05 14:25:05 +08001705 if (ret) {
1706 DRM_ERROR("Failed to initialize bridge with drm\n");
1707 return -EINVAL;
1708 }
1709
1710 encoder->bridge = bridge;
Russell Kingd94905e2013-11-03 22:23:24 +00001711 hdmi->connector.polled = DRM_CONNECTOR_POLL_HPD;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001712
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001713 drm_connector_helper_add(&hdmi->connector,
Andy Yanb21f4b62014-12-05 14:26:31 +08001714 &dw_hdmi_connector_helper_funcs);
1715 drm_connector_init(drm, &hdmi->connector, &dw_hdmi_connector_funcs,
Russell King1b3f7672013-11-03 13:30:48 +00001716 DRM_MODE_CONNECTOR_HDMIA);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001717
Andy Yan3d1b35a2014-12-05 14:25:05 +08001718 hdmi->connector.encoder = encoder;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001719
Andy Yan3d1b35a2014-12-05 14:25:05 +08001720 drm_mode_connector_attach_encoder(&hdmi->connector, encoder);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001721
1722 return 0;
1723}
1724
Andy Yanb21f4b62014-12-05 14:26:31 +08001725int dw_hdmi_bind(struct device *dev, struct device *master,
Andy Yan3d1b35a2014-12-05 14:25:05 +08001726 void *data, struct drm_encoder *encoder,
1727 struct resource *iores, int irq,
1728 const struct dw_hdmi_plat_data *plat_data)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001729{
Russell King1b3f7672013-11-03 13:30:48 +00001730 struct drm_device *drm = data;
Russell King17b50012013-11-03 11:23:34 +00001731 struct device_node *np = dev->of_node;
Russell King7ed6c662013-11-07 16:01:45 +00001732 struct platform_device_info pdevinfo;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001733 struct device_node *ddc_node;
Russell King7ed6c662013-11-07 16:01:45 +00001734 struct dw_hdmi_audio_data audio;
Andy Yanb21f4b62014-12-05 14:26:31 +08001735 struct dw_hdmi *hdmi;
Andy Yan3d1b35a2014-12-05 14:25:05 +08001736 int ret;
Andy Yan0cd9d142014-12-05 14:28:24 +08001737 u32 val = 1;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001738
Russell King17b50012013-11-03 11:23:34 +00001739 hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001740 if (!hdmi)
1741 return -ENOMEM;
1742
Russell Kinge80b9f42015-07-21 11:08:25 +01001743 hdmi->connector.interlace_allowed = 1;
1744
Andy Yan3d1b35a2014-12-05 14:25:05 +08001745 hdmi->plat_data = plat_data;
Russell King17b50012013-11-03 11:23:34 +00001746 hdmi->dev = dev;
Andy Yan3d1b35a2014-12-05 14:25:05 +08001747 hdmi->dev_type = plat_data->dev_type;
Russell King40678382013-11-07 15:35:06 +00001748 hdmi->sample_rate = 48000;
1749 hdmi->ratio = 100;
Andy Yan3d1b35a2014-12-05 14:25:05 +08001750 hdmi->encoder = encoder;
Russell Kingb872a8e2015-06-05 12:22:46 +01001751 hdmi->disabled = true;
Russell Kingaeac23b2015-06-05 13:46:22 +01001752 hdmi->rxsense = true;
1753 hdmi->phy_mask = (u8)~(HDMI_PHY_HPD | HDMI_PHY_RX_SENSE);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001754
Russell Kingb872a8e2015-06-05 12:22:46 +01001755 mutex_init(&hdmi->mutex);
Russell King6bcf4952015-02-02 11:01:08 +00001756 mutex_init(&hdmi->audio_mutex);
Russell Kingb90120a2015-03-27 12:59:58 +00001757 spin_lock_init(&hdmi->audio_lock);
Russell King6bcf4952015-02-02 11:01:08 +00001758
Andy Yan0cd9d142014-12-05 14:28:24 +08001759 of_property_read_u32(np, "reg-io-width", &val);
1760
1761 switch (val) {
1762 case 4:
1763 hdmi->write = dw_hdmi_writel;
1764 hdmi->read = dw_hdmi_readl;
1765 break;
1766 case 1:
1767 hdmi->write = dw_hdmi_writeb;
1768 hdmi->read = dw_hdmi_readb;
1769 break;
1770 default:
1771 dev_err(dev, "reg-io-width must be 1 or 4\n");
1772 return -EINVAL;
1773 }
1774
Philipp Zabelb5d45902014-03-05 10:20:56 +01001775 ddc_node = of_parse_phandle(np, "ddc-i2c-bus", 0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001776 if (ddc_node) {
1777 hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001778 of_node_put(ddc_node);
Andy Yanc2c38482014-12-05 14:24:28 +08001779 if (!hdmi->ddc) {
1780 dev_dbg(hdmi->dev, "failed to read ddc node\n");
1781 return -EPROBE_DEFER;
1782 }
1783
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001784 } else {
1785 dev_dbg(hdmi->dev, "no ddc property found\n");
1786 }
1787
Russell King17b50012013-11-03 11:23:34 +00001788 hdmi->regs = devm_ioremap_resource(dev, iores);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001789 if (IS_ERR(hdmi->regs))
1790 return PTR_ERR(hdmi->regs);
1791
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001792 hdmi->isfr_clk = devm_clk_get(hdmi->dev, "isfr");
1793 if (IS_ERR(hdmi->isfr_clk)) {
1794 ret = PTR_ERR(hdmi->isfr_clk);
Andy Yanb5878332014-12-05 14:23:52 +08001795 dev_err(hdmi->dev, "Unable to get HDMI isfr clk: %d\n", ret);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001796 return ret;
1797 }
1798
1799 ret = clk_prepare_enable(hdmi->isfr_clk);
1800 if (ret) {
Andy Yanb5878332014-12-05 14:23:52 +08001801 dev_err(hdmi->dev, "Cannot enable HDMI isfr clock: %d\n", ret);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001802 return ret;
1803 }
1804
1805 hdmi->iahb_clk = devm_clk_get(hdmi->dev, "iahb");
1806 if (IS_ERR(hdmi->iahb_clk)) {
1807 ret = PTR_ERR(hdmi->iahb_clk);
Andy Yanb5878332014-12-05 14:23:52 +08001808 dev_err(hdmi->dev, "Unable to get HDMI iahb clk: %d\n", ret);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001809 goto err_isfr;
1810 }
1811
1812 ret = clk_prepare_enable(hdmi->iahb_clk);
1813 if (ret) {
Andy Yanb5878332014-12-05 14:23:52 +08001814 dev_err(hdmi->dev, "Cannot enable HDMI iahb clock: %d\n", ret);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001815 goto err_isfr;
1816 }
1817
1818 /* Product and revision IDs */
Russell King17b50012013-11-03 11:23:34 +00001819 dev_info(dev,
Andy Yanb5878332014-12-05 14:23:52 +08001820 "Detected HDMI controller 0x%x:0x%x:0x%x:0x%x\n",
1821 hdmi_readb(hdmi, HDMI_DESIGN_ID),
1822 hdmi_readb(hdmi, HDMI_REVISION_ID),
1823 hdmi_readb(hdmi, HDMI_PRODUCT_ID0),
1824 hdmi_readb(hdmi, HDMI_PRODUCT_ID1));
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001825
1826 initialize_hdmi_ih_mutes(hdmi);
1827
Philipp Zabel639a2022015-01-07 13:43:50 +01001828 ret = devm_request_threaded_irq(dev, irq, dw_hdmi_hardirq,
1829 dw_hdmi_irq, IRQF_SHARED,
1830 dev_name(dev), hdmi);
1831 if (ret)
Fabio Estevamb33ef612015-01-27 10:54:12 -02001832 goto err_iahb;
Philipp Zabel639a2022015-01-07 13:43:50 +01001833
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001834 /*
1835 * To prevent overflows in HDMI_IH_FC_STAT2, set the clk regenerator
1836 * N and cts values before enabling phy
1837 */
1838 hdmi_init_clk_regenerator(hdmi);
1839
1840 /*
1841 * Configure registers related to HDMI interrupt
1842 * generation before registering IRQ.
1843 */
Russell Kingaeac23b2015-06-05 13:46:22 +01001844 hdmi_writeb(hdmi, HDMI_PHY_HPD | HDMI_PHY_RX_SENSE, HDMI_PHY_POL0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001845
1846 /* Clear Hotplug interrupts */
Russell Kingaeac23b2015-06-05 13:46:22 +01001847 hdmi_writeb(hdmi, HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE,
1848 HDMI_IH_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001849
Andy Yanb21f4b62014-12-05 14:26:31 +08001850 ret = dw_hdmi_fb_registered(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001851 if (ret)
1852 goto err_iahb;
1853
Andy Yanb21f4b62014-12-05 14:26:31 +08001854 ret = dw_hdmi_register(drm, hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001855 if (ret)
1856 goto err_iahb;
1857
Russell Kingd94905e2013-11-03 22:23:24 +00001858 /* Unmute interrupts */
Russell Kingaeac23b2015-06-05 13:46:22 +01001859 hdmi_writeb(hdmi, ~(HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE),
1860 HDMI_IH_MUTE_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001861
Russell King7ed6c662013-11-07 16:01:45 +00001862 memset(&pdevinfo, 0, sizeof(pdevinfo));
1863 pdevinfo.parent = dev;
1864 pdevinfo.id = PLATFORM_DEVID_AUTO;
1865
1866 if (hdmi_readb(hdmi, HDMI_CONFIG1_ID) & HDMI_CONFIG1_AHB) {
1867 audio.phys = iores->start;
1868 audio.base = hdmi->regs;
1869 audio.irq = irq;
1870 audio.hdmi = hdmi;
Russell Kingf5ce4052013-11-07 16:06:01 +00001871 audio.eld = hdmi->connector.eld;
Russell King7ed6c662013-11-07 16:01:45 +00001872
1873 pdevinfo.name = "dw-hdmi-ahb-audio";
1874 pdevinfo.data = &audio;
1875 pdevinfo.size_data = sizeof(audio);
1876 pdevinfo.dma_mask = DMA_BIT_MASK(32);
1877 hdmi->audio = platform_device_register_full(&pdevinfo);
1878 }
1879
Russell King17b50012013-11-03 11:23:34 +00001880 dev_set_drvdata(dev, hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001881
1882 return 0;
1883
1884err_iahb:
1885 clk_disable_unprepare(hdmi->iahb_clk);
1886err_isfr:
1887 clk_disable_unprepare(hdmi->isfr_clk);
1888
1889 return ret;
1890}
Andy Yanb21f4b62014-12-05 14:26:31 +08001891EXPORT_SYMBOL_GPL(dw_hdmi_bind);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001892
Andy Yanb21f4b62014-12-05 14:26:31 +08001893void dw_hdmi_unbind(struct device *dev, struct device *master, void *data)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001894{
Andy Yanb21f4b62014-12-05 14:26:31 +08001895 struct dw_hdmi *hdmi = dev_get_drvdata(dev);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001896
Russell King7ed6c662013-11-07 16:01:45 +00001897 if (hdmi->audio && !IS_ERR(hdmi->audio))
1898 platform_device_unregister(hdmi->audio);
1899
Russell Kingd94905e2013-11-03 22:23:24 +00001900 /* Disable all interrupts */
1901 hdmi_writeb(hdmi, ~0, HDMI_IH_MUTE_PHY_STAT0);
1902
Russell King1b3f7672013-11-03 13:30:48 +00001903 hdmi->connector.funcs->destroy(&hdmi->connector);
Andy Yan3d1b35a2014-12-05 14:25:05 +08001904 hdmi->encoder->funcs->destroy(hdmi->encoder);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001905
1906 clk_disable_unprepare(hdmi->iahb_clk);
1907 clk_disable_unprepare(hdmi->isfr_clk);
1908 i2c_put_adapter(hdmi->ddc);
Russell King17b50012013-11-03 11:23:34 +00001909}
Andy Yanb21f4b62014-12-05 14:26:31 +08001910EXPORT_SYMBOL_GPL(dw_hdmi_unbind);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001911
1912MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
Andy Yan3d1b35a2014-12-05 14:25:05 +08001913MODULE_AUTHOR("Andy Yan <andy.yan@rock-chips.com>");
1914MODULE_AUTHOR("Yakir Yang <ykk@rock-chips.com>");
Andy Yanb21f4b62014-12-05 14:26:31 +08001915MODULE_DESCRIPTION("DW HDMI transmitter driver");
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001916MODULE_LICENSE("GPL");
Andy Yanb21f4b62014-12-05 14:26:31 +08001917MODULE_ALIAS("platform:dw-hdmi");