blob: 3a1cd4c7ac648945cf38e9712655a3a7084b1e1a [file] [log] [blame]
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001/*
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002 * DesignWare High-Definition Multimedia Interface (HDMI) driver
3 *
4 * Copyright (C) 2013-2015 Mentor Graphics Inc.
Fabio Estevam9aaf8802013-11-29 08:46:32 -02005 * Copyright (C) 2011-2013 Freescale Semiconductor, Inc.
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03006 * Copyright (C) 2010, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
Fabio Estevam9aaf8802013-11-29 08:46:32 -02007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
Fabio Estevam9aaf8802013-11-29 08:46:32 -020013 */
Andy Yanb21f4b62014-12-05 14:26:31 +080014#include <linux/module.h>
Fabio Estevam9aaf8802013-11-29 08:46:32 -020015#include <linux/irq.h>
16#include <linux/delay.h>
17#include <linux/err.h>
18#include <linux/clk.h>
Sachin Kamat5a819ed2014-01-28 10:33:16 +053019#include <linux/hdmi.h>
Russell King6bcf4952015-02-02 11:01:08 +000020#include <linux/mutex.h>
Fabio Estevam9aaf8802013-11-29 08:46:32 -020021#include <linux/of_device.h>
Russell Kingb90120a2015-03-27 12:59:58 +000022#include <linux/spinlock.h>
Fabio Estevam9aaf8802013-11-29 08:46:32 -020023
Andy Yan3d1b35a2014-12-05 14:25:05 +080024#include <drm/drm_of.h>
Fabio Estevam9aaf8802013-11-29 08:46:32 -020025#include <drm/drmP.h>
Mark Yao2c5b2cc2015-11-30 18:33:40 +080026#include <drm/drm_atomic_helper.h>
Fabio Estevam9aaf8802013-11-29 08:46:32 -020027#include <drm/drm_crtc_helper.h>
28#include <drm/drm_edid.h>
29#include <drm/drm_encoder_slave.h>
Andy Yanb21f4b62014-12-05 14:26:31 +080030#include <drm/bridge/dw_hdmi.h>
Fabio Estevam9aaf8802013-11-29 08:46:32 -020031
Thierry Reding248a86f2015-11-24 17:52:58 +010032#include "dw-hdmi.h"
33#include "dw-hdmi-audio.h"
Fabio Estevam9aaf8802013-11-29 08:46:32 -020034
35#define HDMI_EDID_LEN 512
36
37#define RGB 0
38#define YCBCR444 1
39#define YCBCR422_16BITS 2
40#define YCBCR422_8BITS 3
41#define XVYCC444 4
42
43enum hdmi_datamap {
44 RGB444_8B = 0x01,
45 RGB444_10B = 0x03,
46 RGB444_12B = 0x05,
47 RGB444_16B = 0x07,
48 YCbCr444_8B = 0x09,
49 YCbCr444_10B = 0x0B,
50 YCbCr444_12B = 0x0D,
51 YCbCr444_16B = 0x0F,
52 YCbCr422_8B = 0x16,
53 YCbCr422_10B = 0x14,
54 YCbCr422_12B = 0x12,
55};
56
Fabio Estevam9aaf8802013-11-29 08:46:32 -020057static const u16 csc_coeff_default[3][4] = {
58 { 0x2000, 0x0000, 0x0000, 0x0000 },
59 { 0x0000, 0x2000, 0x0000, 0x0000 },
60 { 0x0000, 0x0000, 0x2000, 0x0000 }
61};
62
63static const u16 csc_coeff_rgb_out_eitu601[3][4] = {
64 { 0x2000, 0x6926, 0x74fd, 0x010e },
65 { 0x2000, 0x2cdd, 0x0000, 0x7e9a },
66 { 0x2000, 0x0000, 0x38b4, 0x7e3b }
67};
68
69static const u16 csc_coeff_rgb_out_eitu709[3][4] = {
70 { 0x2000, 0x7106, 0x7a02, 0x00a7 },
71 { 0x2000, 0x3264, 0x0000, 0x7e6d },
72 { 0x2000, 0x0000, 0x3b61, 0x7e25 }
73};
74
75static const u16 csc_coeff_rgb_in_eitu601[3][4] = {
76 { 0x2591, 0x1322, 0x074b, 0x0000 },
77 { 0x6535, 0x2000, 0x7acc, 0x0200 },
78 { 0x6acd, 0x7534, 0x2000, 0x0200 }
79};
80
81static const u16 csc_coeff_rgb_in_eitu709[3][4] = {
82 { 0x2dc5, 0x0d9b, 0x049e, 0x0000 },
83 { 0x62f0, 0x2000, 0x7d11, 0x0200 },
84 { 0x6756, 0x78ab, 0x2000, 0x0200 }
85};
86
87struct hdmi_vmode {
Fabio Estevam9aaf8802013-11-29 08:46:32 -020088 bool mdataenablepolarity;
89
90 unsigned int mpixelclock;
91 unsigned int mpixelrepetitioninput;
92 unsigned int mpixelrepetitionoutput;
93};
94
95struct hdmi_data_info {
96 unsigned int enc_in_format;
97 unsigned int enc_out_format;
98 unsigned int enc_color_depth;
99 unsigned int colorimetry;
100 unsigned int pix_repet_factor;
101 unsigned int hdcp_enable;
102 struct hdmi_vmode video_mode;
103};
104
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +0300105struct dw_hdmi_i2c {
106 struct i2c_adapter adap;
107
108 struct mutex lock; /* used to serialize data transfers */
109 struct completion cmp;
110 u8 stat;
111
112 u8 slave_reg;
113 bool is_regaddr;
114};
115
Laurent Pinchartfaba6c32017-01-17 10:29:06 +0200116struct dw_hdmi_phy_data {
117 enum dw_hdmi_phy_type type;
118 const char *name;
Laurent Pinchartb0e583e2017-03-06 01:35:39 +0200119 unsigned int gen;
Laurent Pinchartfaba6c32017-01-17 10:29:06 +0200120 bool has_svsret;
121};
122
Andy Yanb21f4b62014-12-05 14:26:31 +0800123struct dw_hdmi {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200124 struct drm_connector connector;
Laurent Pinchart70c963e2017-01-17 10:28:54 +0200125 struct drm_bridge bridge;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200126
Andy Yanb21f4b62014-12-05 14:26:31 +0800127 enum dw_hdmi_devtype dev_type;
Laurent Pinchartbe41fc52017-01-17 10:29:05 +0200128 unsigned int version;
129
130 struct platform_device *audio;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200131 struct device *dev;
132 struct clk *isfr_clk;
133 struct clk *iahb_clk;
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +0300134 struct dw_hdmi_i2c *i2c;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200135
136 struct hdmi_data_info hdmi_data;
Andy Yanb21f4b62014-12-05 14:26:31 +0800137 const struct dw_hdmi_plat_data *plat_data;
138
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200139 int vic;
140
141 u8 edid[HDMI_EDID_LEN];
142 bool cable_plugin;
143
Laurent Pinchartfaba6c32017-01-17 10:29:06 +0200144 const struct dw_hdmi_phy_data *phy;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200145 bool phy_enabled;
Laurent Pinchartfaba6c32017-01-17 10:29:06 +0200146
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200147 struct drm_display_mode previous_mode;
148
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200149 struct i2c_adapter *ddc;
150 void __iomem *regs;
Russell King05b13422015-07-21 15:35:52 +0100151 bool sink_is_hdmi;
Russell Kingf709ec02015-07-21 16:09:39 +0100152 bool sink_has_audio;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200153
Russell Kingb872a8e2015-06-05 12:22:46 +0100154 struct mutex mutex; /* for state below and previous_mode */
Russell King381f05a2015-06-05 15:25:08 +0100155 enum drm_connector_force force; /* mutex-protected force state */
Russell Kingb872a8e2015-06-05 12:22:46 +0100156 bool disabled; /* DRM has disabled our bridge */
Russell King381f05a2015-06-05 15:25:08 +0100157 bool bridge_is_on; /* indicates the bridge is on */
Russell Kingaeac23b2015-06-05 13:46:22 +0100158 bool rxsense; /* rxsense state */
159 u8 phy_mask; /* desired phy int mask settings */
Russell Kingb872a8e2015-06-05 12:22:46 +0100160
Russell Kingb90120a2015-03-27 12:59:58 +0000161 spinlock_t audio_lock;
Russell King6bcf4952015-02-02 11:01:08 +0000162 struct mutex audio_mutex;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200163 unsigned int sample_rate;
Russell Kingb90120a2015-03-27 12:59:58 +0000164 unsigned int audio_cts;
165 unsigned int audio_n;
166 bool audio_enable;
Andy Yan0cd9d142014-12-05 14:28:24 +0800167
168 void (*write)(struct dw_hdmi *hdmi, u8 val, int offset);
169 u8 (*read)(struct dw_hdmi *hdmi, int offset);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200170};
171
Russell Kingaeac23b2015-06-05 13:46:22 +0100172#define HDMI_IH_PHY_STAT0_RX_SENSE \
173 (HDMI_IH_PHY_STAT0_RX_SENSE0 | HDMI_IH_PHY_STAT0_RX_SENSE1 | \
174 HDMI_IH_PHY_STAT0_RX_SENSE2 | HDMI_IH_PHY_STAT0_RX_SENSE3)
175
176#define HDMI_PHY_RX_SENSE \
177 (HDMI_PHY_RX_SENSE0 | HDMI_PHY_RX_SENSE1 | \
178 HDMI_PHY_RX_SENSE2 | HDMI_PHY_RX_SENSE3)
179
Andy Yan0cd9d142014-12-05 14:28:24 +0800180static void dw_hdmi_writel(struct dw_hdmi *hdmi, u8 val, int offset)
181{
182 writel(val, hdmi->regs + (offset << 2));
183}
184
185static u8 dw_hdmi_readl(struct dw_hdmi *hdmi, int offset)
186{
187 return readl(hdmi->regs + (offset << 2));
188}
189
190static void dw_hdmi_writeb(struct dw_hdmi *hdmi, u8 val, int offset)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200191{
192 writeb(val, hdmi->regs + offset);
193}
194
Andy Yan0cd9d142014-12-05 14:28:24 +0800195static u8 dw_hdmi_readb(struct dw_hdmi *hdmi, int offset)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200196{
197 return readb(hdmi->regs + offset);
198}
199
Andy Yan0cd9d142014-12-05 14:28:24 +0800200static inline void hdmi_writeb(struct dw_hdmi *hdmi, u8 val, int offset)
201{
202 hdmi->write(hdmi, val, offset);
203}
204
205static inline u8 hdmi_readb(struct dw_hdmi *hdmi, int offset)
206{
207 return hdmi->read(hdmi, offset);
208}
209
Andy Yanb21f4b62014-12-05 14:26:31 +0800210static void hdmi_modb(struct dw_hdmi *hdmi, u8 data, u8 mask, unsigned reg)
Russell King812bc612013-11-04 12:42:02 +0000211{
212 u8 val = hdmi_readb(hdmi, reg) & ~mask;
Fabio Estevamb44ab1b2014-04-28 08:01:07 -0300213
Russell King812bc612013-11-04 12:42:02 +0000214 val |= data & mask;
215 hdmi_writeb(hdmi, val, reg);
216}
217
Andy Yanb21f4b62014-12-05 14:26:31 +0800218static void hdmi_mask_writeb(struct dw_hdmi *hdmi, u8 data, unsigned int reg,
Andy Yanb5878332014-12-05 14:23:52 +0800219 u8 shift, u8 mask)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200220{
Russell King812bc612013-11-04 12:42:02 +0000221 hdmi_modb(hdmi, data << shift, mask, reg);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200222}
223
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +0300224static void dw_hdmi_i2c_init(struct dw_hdmi *hdmi)
225{
226 /* Software reset */
227 hdmi_writeb(hdmi, 0x00, HDMI_I2CM_SOFTRSTZ);
228
229 /* Set Standard Mode speed (determined to be 100KHz on iMX6) */
230 hdmi_writeb(hdmi, 0x00, HDMI_I2CM_DIV);
231
232 /* Set done, not acknowledged and arbitration interrupt polarities */
233 hdmi_writeb(hdmi, HDMI_I2CM_INT_DONE_POL, HDMI_I2CM_INT);
234 hdmi_writeb(hdmi, HDMI_I2CM_CTLINT_NAC_POL | HDMI_I2CM_CTLINT_ARB_POL,
235 HDMI_I2CM_CTLINT);
236
237 /* Clear DONE and ERROR interrupts */
238 hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,
239 HDMI_IH_I2CM_STAT0);
240
241 /* Mute DONE and ERROR interrupts */
242 hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,
243 HDMI_IH_MUTE_I2CM_STAT0);
244}
245
246static int dw_hdmi_i2c_read(struct dw_hdmi *hdmi,
247 unsigned char *buf, unsigned int length)
248{
249 struct dw_hdmi_i2c *i2c = hdmi->i2c;
250 int stat;
251
252 if (!i2c->is_regaddr) {
253 dev_dbg(hdmi->dev, "set read register address to 0\n");
254 i2c->slave_reg = 0x00;
255 i2c->is_regaddr = true;
256 }
257
258 while (length--) {
259 reinit_completion(&i2c->cmp);
260
261 hdmi_writeb(hdmi, i2c->slave_reg++, HDMI_I2CM_ADDRESS);
262 hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_READ,
263 HDMI_I2CM_OPERATION);
264
265 stat = wait_for_completion_timeout(&i2c->cmp, HZ / 10);
266 if (!stat)
267 return -EAGAIN;
268
269 /* Check for error condition on the bus */
270 if (i2c->stat & HDMI_IH_I2CM_STAT0_ERROR)
271 return -EIO;
272
273 *buf++ = hdmi_readb(hdmi, HDMI_I2CM_DATAI);
274 }
275
276 return 0;
277}
278
279static int dw_hdmi_i2c_write(struct dw_hdmi *hdmi,
280 unsigned char *buf, unsigned int length)
281{
282 struct dw_hdmi_i2c *i2c = hdmi->i2c;
283 int stat;
284
285 if (!i2c->is_regaddr) {
286 /* Use the first write byte as register address */
287 i2c->slave_reg = buf[0];
288 length--;
289 buf++;
290 i2c->is_regaddr = true;
291 }
292
293 while (length--) {
294 reinit_completion(&i2c->cmp);
295
296 hdmi_writeb(hdmi, *buf++, HDMI_I2CM_DATAO);
297 hdmi_writeb(hdmi, i2c->slave_reg++, HDMI_I2CM_ADDRESS);
298 hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_WRITE,
299 HDMI_I2CM_OPERATION);
300
301 stat = wait_for_completion_timeout(&i2c->cmp, HZ / 10);
302 if (!stat)
303 return -EAGAIN;
304
305 /* Check for error condition on the bus */
306 if (i2c->stat & HDMI_IH_I2CM_STAT0_ERROR)
307 return -EIO;
308 }
309
310 return 0;
311}
312
313static int dw_hdmi_i2c_xfer(struct i2c_adapter *adap,
314 struct i2c_msg *msgs, int num)
315{
316 struct dw_hdmi *hdmi = i2c_get_adapdata(adap);
317 struct dw_hdmi_i2c *i2c = hdmi->i2c;
318 u8 addr = msgs[0].addr;
319 int i, ret = 0;
320
321 dev_dbg(hdmi->dev, "xfer: num: %d, addr: %#x\n", num, addr);
322
323 for (i = 0; i < num; i++) {
324 if (msgs[i].addr != addr) {
325 dev_warn(hdmi->dev,
326 "unsupported transfer, changed slave address\n");
327 return -EOPNOTSUPP;
328 }
329
330 if (msgs[i].len == 0) {
331 dev_dbg(hdmi->dev,
332 "unsupported transfer %d/%d, no data\n",
333 i + 1, num);
334 return -EOPNOTSUPP;
335 }
336 }
337
338 mutex_lock(&i2c->lock);
339
340 /* Unmute DONE and ERROR interrupts */
341 hdmi_writeb(hdmi, 0x00, HDMI_IH_MUTE_I2CM_STAT0);
342
343 /* Set slave device address taken from the first I2C message */
344 hdmi_writeb(hdmi, addr, HDMI_I2CM_SLAVE);
345
346 /* Set slave device register address on transfer */
347 i2c->is_regaddr = false;
348
349 for (i = 0; i < num; i++) {
350 dev_dbg(hdmi->dev, "xfer: num: %d/%d, len: %d, flags: %#x\n",
351 i + 1, num, msgs[i].len, msgs[i].flags);
352
353 if (msgs[i].flags & I2C_M_RD)
354 ret = dw_hdmi_i2c_read(hdmi, msgs[i].buf, msgs[i].len);
355 else
356 ret = dw_hdmi_i2c_write(hdmi, msgs[i].buf, msgs[i].len);
357
358 if (ret < 0)
359 break;
360 }
361
362 if (!ret)
363 ret = num;
364
365 /* Mute DONE and ERROR interrupts */
366 hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,
367 HDMI_IH_MUTE_I2CM_STAT0);
368
369 mutex_unlock(&i2c->lock);
370
371 return ret;
372}
373
374static u32 dw_hdmi_i2c_func(struct i2c_adapter *adapter)
375{
376 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
377}
378
379static const struct i2c_algorithm dw_hdmi_algorithm = {
380 .master_xfer = dw_hdmi_i2c_xfer,
381 .functionality = dw_hdmi_i2c_func,
382};
383
384static struct i2c_adapter *dw_hdmi_i2c_adapter(struct dw_hdmi *hdmi)
385{
386 struct i2c_adapter *adap;
387 struct dw_hdmi_i2c *i2c;
388 int ret;
389
390 i2c = devm_kzalloc(hdmi->dev, sizeof(*i2c), GFP_KERNEL);
391 if (!i2c)
392 return ERR_PTR(-ENOMEM);
393
394 mutex_init(&i2c->lock);
395 init_completion(&i2c->cmp);
396
397 adap = &i2c->adap;
398 adap->class = I2C_CLASS_DDC;
399 adap->owner = THIS_MODULE;
400 adap->dev.parent = hdmi->dev;
401 adap->algo = &dw_hdmi_algorithm;
402 strlcpy(adap->name, "DesignWare HDMI", sizeof(adap->name));
403 i2c_set_adapdata(adap, hdmi);
404
405 ret = i2c_add_adapter(adap);
406 if (ret) {
407 dev_warn(hdmi->dev, "cannot add %s I2C adapter\n", adap->name);
408 devm_kfree(hdmi->dev, i2c);
409 return ERR_PTR(ret);
410 }
411
412 hdmi->i2c = i2c;
413
414 dev_info(hdmi->dev, "registered %s I2C bus driver\n", adap->name);
415
416 return adap;
417}
418
Russell King351e1352015-01-31 14:50:23 +0000419static void hdmi_set_cts_n(struct dw_hdmi *hdmi, unsigned int cts,
420 unsigned int n)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200421{
Russell King622494a2015-02-02 10:55:38 +0000422 /* Must be set/cleared first */
423 hdmi_modb(hdmi, 0, HDMI_AUD_CTS3_CTS_MANUAL, HDMI_AUD_CTS3);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200424
425 /* nshift factor = 0 */
Russell King812bc612013-11-04 12:42:02 +0000426 hdmi_modb(hdmi, 0, HDMI_AUD_CTS3_N_SHIFT_MASK, HDMI_AUD_CTS3);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200427
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200428 hdmi_writeb(hdmi, ((cts >> 16) & HDMI_AUD_CTS3_AUDCTS19_16_MASK) |
429 HDMI_AUD_CTS3_CTS_MANUAL, HDMI_AUD_CTS3);
Russell King622494a2015-02-02 10:55:38 +0000430 hdmi_writeb(hdmi, (cts >> 8) & 0xff, HDMI_AUD_CTS2);
431 hdmi_writeb(hdmi, cts & 0xff, HDMI_AUD_CTS1);
432
433 hdmi_writeb(hdmi, (n >> 16) & 0x0f, HDMI_AUD_N3);
434 hdmi_writeb(hdmi, (n >> 8) & 0xff, HDMI_AUD_N2);
435 hdmi_writeb(hdmi, n & 0xff, HDMI_AUD_N1);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200436}
437
Russell Kingb195fbd2015-07-22 11:28:16 +0100438static unsigned int hdmi_compute_n(unsigned int freq, unsigned long pixel_clk)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200439{
440 unsigned int n = (128 * freq) / 1000;
Russell Kingd0c96d12015-07-22 10:35:41 +0100441 unsigned int mult = 1;
442
443 while (freq > 48000) {
444 mult *= 2;
445 freq /= 2;
446 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200447
448 switch (freq) {
449 case 32000:
Russell King426701d2015-07-22 10:39:27 +0100450 if (pixel_clk == 25175000)
Russell Kingb195fbd2015-07-22 11:28:16 +0100451 n = 4576;
Russell King426701d2015-07-22 10:39:27 +0100452 else if (pixel_clk == 27027000)
Russell Kingb195fbd2015-07-22 11:28:16 +0100453 n = 4096;
Russell King426701d2015-07-22 10:39:27 +0100454 else if (pixel_clk == 74176000 || pixel_clk == 148352000)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200455 n = 11648;
456 else
457 n = 4096;
Russell Kingd0c96d12015-07-22 10:35:41 +0100458 n *= mult;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200459 break;
460
461 case 44100:
Russell King426701d2015-07-22 10:39:27 +0100462 if (pixel_clk == 25175000)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200463 n = 7007;
Russell King426701d2015-07-22 10:39:27 +0100464 else if (pixel_clk == 74176000)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200465 n = 17836;
Russell King426701d2015-07-22 10:39:27 +0100466 else if (pixel_clk == 148352000)
Russell Kingb195fbd2015-07-22 11:28:16 +0100467 n = 8918;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200468 else
469 n = 6272;
Russell Kingd0c96d12015-07-22 10:35:41 +0100470 n *= mult;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200471 break;
472
473 case 48000:
Russell King426701d2015-07-22 10:39:27 +0100474 if (pixel_clk == 25175000)
Russell Kingb195fbd2015-07-22 11:28:16 +0100475 n = 6864;
Russell King426701d2015-07-22 10:39:27 +0100476 else if (pixel_clk == 27027000)
Russell Kingb195fbd2015-07-22 11:28:16 +0100477 n = 6144;
Russell King426701d2015-07-22 10:39:27 +0100478 else if (pixel_clk == 74176000)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200479 n = 11648;
Russell King426701d2015-07-22 10:39:27 +0100480 else if (pixel_clk == 148352000)
Russell Kingb195fbd2015-07-22 11:28:16 +0100481 n = 5824;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200482 else
483 n = 6144;
Russell Kingd0c96d12015-07-22 10:35:41 +0100484 n *= mult;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200485 break;
486
487 default:
488 break;
489 }
490
491 return n;
492}
493
Andy Yanb21f4b62014-12-05 14:26:31 +0800494static void hdmi_set_clk_regenerator(struct dw_hdmi *hdmi,
Russell Kingb195fbd2015-07-22 11:28:16 +0100495 unsigned long pixel_clk, unsigned int sample_rate)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200496{
Russell Kingdfbdaf52015-07-22 16:54:37 +0100497 unsigned long ftdms = pixel_clk;
Russell Kingf879b382015-03-27 12:53:29 +0000498 unsigned int n, cts;
Russell Kingdfbdaf52015-07-22 16:54:37 +0100499 u64 tmp;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200500
Russell Kingb195fbd2015-07-22 11:28:16 +0100501 n = hdmi_compute_n(sample_rate, pixel_clk);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200502
Russell Kingdfbdaf52015-07-22 16:54:37 +0100503 /*
504 * Compute the CTS value from the N value. Note that CTS and N
505 * can be up to 20 bits in total, so we need 64-bit math. Also
506 * note that our TDMS clock is not fully accurate; it is accurate
507 * to kHz. This can introduce an unnecessary remainder in the
508 * calculation below, so we don't try to warn about that.
509 */
510 tmp = (u64)ftdms * n;
511 do_div(tmp, 128 * sample_rate);
512 cts = tmp;
513
514 dev_dbg(hdmi->dev, "%s: fs=%uHz ftdms=%lu.%03luMHz N=%d cts=%d\n",
515 __func__, sample_rate, ftdms / 1000000, (ftdms / 1000) % 1000,
516 n, cts);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200517
Russell Kingb90120a2015-03-27 12:59:58 +0000518 spin_lock_irq(&hdmi->audio_lock);
519 hdmi->audio_n = n;
520 hdmi->audio_cts = cts;
521 hdmi_set_cts_n(hdmi, cts, hdmi->audio_enable ? n : 0);
522 spin_unlock_irq(&hdmi->audio_lock);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200523}
524
Andy Yanb21f4b62014-12-05 14:26:31 +0800525static void hdmi_init_clk_regenerator(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200526{
Russell King6bcf4952015-02-02 11:01:08 +0000527 mutex_lock(&hdmi->audio_mutex);
Russell Kingb195fbd2015-07-22 11:28:16 +0100528 hdmi_set_clk_regenerator(hdmi, 74250000, hdmi->sample_rate);
Russell King6bcf4952015-02-02 11:01:08 +0000529 mutex_unlock(&hdmi->audio_mutex);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200530}
531
Andy Yanb21f4b62014-12-05 14:26:31 +0800532static void hdmi_clk_regenerator_update_pixel_clock(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200533{
Russell King6bcf4952015-02-02 11:01:08 +0000534 mutex_lock(&hdmi->audio_mutex);
Russell Kingf879b382015-03-27 12:53:29 +0000535 hdmi_set_clk_regenerator(hdmi, hdmi->hdmi_data.video_mode.mpixelclock,
Russell Kingb195fbd2015-07-22 11:28:16 +0100536 hdmi->sample_rate);
Russell King6bcf4952015-02-02 11:01:08 +0000537 mutex_unlock(&hdmi->audio_mutex);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200538}
539
Russell Kingb5814ff2015-03-27 12:50:58 +0000540void dw_hdmi_set_sample_rate(struct dw_hdmi *hdmi, unsigned int rate)
541{
542 mutex_lock(&hdmi->audio_mutex);
543 hdmi->sample_rate = rate;
544 hdmi_set_clk_regenerator(hdmi, hdmi->hdmi_data.video_mode.mpixelclock,
Russell Kingb195fbd2015-07-22 11:28:16 +0100545 hdmi->sample_rate);
Russell Kingb5814ff2015-03-27 12:50:58 +0000546 mutex_unlock(&hdmi->audio_mutex);
547}
548EXPORT_SYMBOL_GPL(dw_hdmi_set_sample_rate);
549
Russell Kingb90120a2015-03-27 12:59:58 +0000550void dw_hdmi_audio_enable(struct dw_hdmi *hdmi)
551{
552 unsigned long flags;
553
554 spin_lock_irqsave(&hdmi->audio_lock, flags);
555 hdmi->audio_enable = true;
556 hdmi_set_cts_n(hdmi, hdmi->audio_cts, hdmi->audio_n);
557 spin_unlock_irqrestore(&hdmi->audio_lock, flags);
558}
559EXPORT_SYMBOL_GPL(dw_hdmi_audio_enable);
560
561void dw_hdmi_audio_disable(struct dw_hdmi *hdmi)
562{
563 unsigned long flags;
564
565 spin_lock_irqsave(&hdmi->audio_lock, flags);
566 hdmi->audio_enable = false;
567 hdmi_set_cts_n(hdmi, hdmi->audio_cts, 0);
568 spin_unlock_irqrestore(&hdmi->audio_lock, flags);
569}
570EXPORT_SYMBOL_GPL(dw_hdmi_audio_disable);
571
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200572/*
573 * this submodule is responsible for the video data synchronization.
574 * for example, for RGB 4:4:4 input, the data map is defined as
575 * pin{47~40} <==> R[7:0]
576 * pin{31~24} <==> G[7:0]
577 * pin{15~8} <==> B[7:0]
578 */
Andy Yanb21f4b62014-12-05 14:26:31 +0800579static void hdmi_video_sample(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200580{
581 int color_format = 0;
582 u8 val;
583
584 if (hdmi->hdmi_data.enc_in_format == RGB) {
585 if (hdmi->hdmi_data.enc_color_depth == 8)
586 color_format = 0x01;
587 else if (hdmi->hdmi_data.enc_color_depth == 10)
588 color_format = 0x03;
589 else if (hdmi->hdmi_data.enc_color_depth == 12)
590 color_format = 0x05;
591 else if (hdmi->hdmi_data.enc_color_depth == 16)
592 color_format = 0x07;
593 else
594 return;
595 } else if (hdmi->hdmi_data.enc_in_format == YCBCR444) {
596 if (hdmi->hdmi_data.enc_color_depth == 8)
597 color_format = 0x09;
598 else if (hdmi->hdmi_data.enc_color_depth == 10)
599 color_format = 0x0B;
600 else if (hdmi->hdmi_data.enc_color_depth == 12)
601 color_format = 0x0D;
602 else if (hdmi->hdmi_data.enc_color_depth == 16)
603 color_format = 0x0F;
604 else
605 return;
606 } else if (hdmi->hdmi_data.enc_in_format == YCBCR422_8BITS) {
607 if (hdmi->hdmi_data.enc_color_depth == 8)
608 color_format = 0x16;
609 else if (hdmi->hdmi_data.enc_color_depth == 10)
610 color_format = 0x14;
611 else if (hdmi->hdmi_data.enc_color_depth == 12)
612 color_format = 0x12;
613 else
614 return;
615 }
616
617 val = HDMI_TX_INVID0_INTERNAL_DE_GENERATOR_DISABLE |
618 ((color_format << HDMI_TX_INVID0_VIDEO_MAPPING_OFFSET) &
619 HDMI_TX_INVID0_VIDEO_MAPPING_MASK);
620 hdmi_writeb(hdmi, val, HDMI_TX_INVID0);
621
622 /* Enable TX stuffing: When DE is inactive, fix the output data to 0 */
623 val = HDMI_TX_INSTUFFING_BDBDATA_STUFFING_ENABLE |
624 HDMI_TX_INSTUFFING_RCRDATA_STUFFING_ENABLE |
625 HDMI_TX_INSTUFFING_GYDATA_STUFFING_ENABLE;
626 hdmi_writeb(hdmi, val, HDMI_TX_INSTUFFING);
627 hdmi_writeb(hdmi, 0x0, HDMI_TX_GYDATA0);
628 hdmi_writeb(hdmi, 0x0, HDMI_TX_GYDATA1);
629 hdmi_writeb(hdmi, 0x0, HDMI_TX_RCRDATA0);
630 hdmi_writeb(hdmi, 0x0, HDMI_TX_RCRDATA1);
631 hdmi_writeb(hdmi, 0x0, HDMI_TX_BCBDATA0);
632 hdmi_writeb(hdmi, 0x0, HDMI_TX_BCBDATA1);
633}
634
Andy Yanb21f4b62014-12-05 14:26:31 +0800635static int is_color_space_conversion(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200636{
Fabio Estevamba92b222014-02-06 10:12:03 -0200637 return hdmi->hdmi_data.enc_in_format != hdmi->hdmi_data.enc_out_format;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200638}
639
Andy Yanb21f4b62014-12-05 14:26:31 +0800640static int is_color_space_decimation(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200641{
Fabio Estevamba92b222014-02-06 10:12:03 -0200642 if (hdmi->hdmi_data.enc_out_format != YCBCR422_8BITS)
643 return 0;
644 if (hdmi->hdmi_data.enc_in_format == RGB ||
645 hdmi->hdmi_data.enc_in_format == YCBCR444)
646 return 1;
647 return 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200648}
649
Andy Yanb21f4b62014-12-05 14:26:31 +0800650static int is_color_space_interpolation(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200651{
Fabio Estevamba92b222014-02-06 10:12:03 -0200652 if (hdmi->hdmi_data.enc_in_format != YCBCR422_8BITS)
653 return 0;
654 if (hdmi->hdmi_data.enc_out_format == RGB ||
655 hdmi->hdmi_data.enc_out_format == YCBCR444)
656 return 1;
657 return 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200658}
659
Andy Yanb21f4b62014-12-05 14:26:31 +0800660static void dw_hdmi_update_csc_coeffs(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200661{
662 const u16 (*csc_coeff)[3][4] = &csc_coeff_default;
Russell Kingc082f9d2013-11-04 12:10:40 +0000663 unsigned i;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200664 u32 csc_scale = 1;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200665
666 if (is_color_space_conversion(hdmi)) {
667 if (hdmi->hdmi_data.enc_out_format == RGB) {
Gulsah Kose256a38b2014-03-09 20:11:07 +0200668 if (hdmi->hdmi_data.colorimetry ==
669 HDMI_COLORIMETRY_ITU_601)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200670 csc_coeff = &csc_coeff_rgb_out_eitu601;
671 else
672 csc_coeff = &csc_coeff_rgb_out_eitu709;
673 } else if (hdmi->hdmi_data.enc_in_format == RGB) {
Gulsah Kose256a38b2014-03-09 20:11:07 +0200674 if (hdmi->hdmi_data.colorimetry ==
675 HDMI_COLORIMETRY_ITU_601)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200676 csc_coeff = &csc_coeff_rgb_in_eitu601;
677 else
678 csc_coeff = &csc_coeff_rgb_in_eitu709;
679 csc_scale = 0;
680 }
681 }
682
Russell Kingc082f9d2013-11-04 12:10:40 +0000683 /* The CSC registers are sequential, alternating MSB then LSB */
684 for (i = 0; i < ARRAY_SIZE(csc_coeff_default[0]); i++) {
685 u16 coeff_a = (*csc_coeff)[0][i];
686 u16 coeff_b = (*csc_coeff)[1][i];
687 u16 coeff_c = (*csc_coeff)[2][i];
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200688
Andy Yanb5878332014-12-05 14:23:52 +0800689 hdmi_writeb(hdmi, coeff_a & 0xff, HDMI_CSC_COEF_A1_LSB + i * 2);
Russell Kingc082f9d2013-11-04 12:10:40 +0000690 hdmi_writeb(hdmi, coeff_a >> 8, HDMI_CSC_COEF_A1_MSB + i * 2);
691 hdmi_writeb(hdmi, coeff_b & 0xff, HDMI_CSC_COEF_B1_LSB + i * 2);
692 hdmi_writeb(hdmi, coeff_b >> 8, HDMI_CSC_COEF_B1_MSB + i * 2);
Andy Yanb5878332014-12-05 14:23:52 +0800693 hdmi_writeb(hdmi, coeff_c & 0xff, HDMI_CSC_COEF_C1_LSB + i * 2);
Russell Kingc082f9d2013-11-04 12:10:40 +0000694 hdmi_writeb(hdmi, coeff_c >> 8, HDMI_CSC_COEF_C1_MSB + i * 2);
695 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200696
Russell King812bc612013-11-04 12:42:02 +0000697 hdmi_modb(hdmi, csc_scale, HDMI_CSC_SCALE_CSCSCALE_MASK,
698 HDMI_CSC_SCALE);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200699}
700
Andy Yanb21f4b62014-12-05 14:26:31 +0800701static void hdmi_video_csc(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200702{
703 int color_depth = 0;
704 int interpolation = HDMI_CSC_CFG_INTMODE_DISABLE;
705 int decimation = 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200706
707 /* YCC422 interpolation to 444 mode */
708 if (is_color_space_interpolation(hdmi))
709 interpolation = HDMI_CSC_CFG_INTMODE_CHROMA_INT_FORMULA1;
710 else if (is_color_space_decimation(hdmi))
711 decimation = HDMI_CSC_CFG_DECMODE_CHROMA_INT_FORMULA3;
712
713 if (hdmi->hdmi_data.enc_color_depth == 8)
714 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_24BPP;
715 else if (hdmi->hdmi_data.enc_color_depth == 10)
716 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_30BPP;
717 else if (hdmi->hdmi_data.enc_color_depth == 12)
718 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_36BPP;
719 else if (hdmi->hdmi_data.enc_color_depth == 16)
720 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_48BPP;
721 else
722 return;
723
724 /* Configure the CSC registers */
725 hdmi_writeb(hdmi, interpolation | decimation, HDMI_CSC_CFG);
Russell King812bc612013-11-04 12:42:02 +0000726 hdmi_modb(hdmi, color_depth, HDMI_CSC_SCALE_CSC_COLORDE_PTH_MASK,
727 HDMI_CSC_SCALE);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200728
Andy Yanb21f4b62014-12-05 14:26:31 +0800729 dw_hdmi_update_csc_coeffs(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200730}
731
732/*
733 * HDMI video packetizer is used to packetize the data.
734 * for example, if input is YCC422 mode or repeater is used,
735 * data should be repacked this module can be bypassed.
736 */
Andy Yanb21f4b62014-12-05 14:26:31 +0800737static void hdmi_video_packetize(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200738{
739 unsigned int color_depth = 0;
740 unsigned int remap_size = HDMI_VP_REMAP_YCC422_16bit;
741 unsigned int output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_PP;
742 struct hdmi_data_info *hdmi_data = &hdmi->hdmi_data;
Russell Kingbebdf662013-11-04 12:55:30 +0000743 u8 val, vp_conf;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200744
Andy Yanb5878332014-12-05 14:23:52 +0800745 if (hdmi_data->enc_out_format == RGB ||
746 hdmi_data->enc_out_format == YCBCR444) {
747 if (!hdmi_data->enc_color_depth) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200748 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;
Andy Yanb5878332014-12-05 14:23:52 +0800749 } else if (hdmi_data->enc_color_depth == 8) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200750 color_depth = 4;
751 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;
Andy Yanb5878332014-12-05 14:23:52 +0800752 } else if (hdmi_data->enc_color_depth == 10) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200753 color_depth = 5;
Andy Yanb5878332014-12-05 14:23:52 +0800754 } else if (hdmi_data->enc_color_depth == 12) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200755 color_depth = 6;
Andy Yanb5878332014-12-05 14:23:52 +0800756 } else if (hdmi_data->enc_color_depth == 16) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200757 color_depth = 7;
Andy Yanb5878332014-12-05 14:23:52 +0800758 } else {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200759 return;
Andy Yanb5878332014-12-05 14:23:52 +0800760 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200761 } else if (hdmi_data->enc_out_format == YCBCR422_8BITS) {
762 if (!hdmi_data->enc_color_depth ||
763 hdmi_data->enc_color_depth == 8)
764 remap_size = HDMI_VP_REMAP_YCC422_16bit;
765 else if (hdmi_data->enc_color_depth == 10)
766 remap_size = HDMI_VP_REMAP_YCC422_20bit;
767 else if (hdmi_data->enc_color_depth == 12)
768 remap_size = HDMI_VP_REMAP_YCC422_24bit;
769 else
770 return;
771 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_YCC422;
Andy Yanb5878332014-12-05 14:23:52 +0800772 } else {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200773 return;
Andy Yanb5878332014-12-05 14:23:52 +0800774 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200775
776 /* set the packetizer registers */
777 val = ((color_depth << HDMI_VP_PR_CD_COLOR_DEPTH_OFFSET) &
778 HDMI_VP_PR_CD_COLOR_DEPTH_MASK) |
779 ((hdmi_data->pix_repet_factor <<
780 HDMI_VP_PR_CD_DESIRED_PR_FACTOR_OFFSET) &
781 HDMI_VP_PR_CD_DESIRED_PR_FACTOR_MASK);
782 hdmi_writeb(hdmi, val, HDMI_VP_PR_CD);
783
Russell King812bc612013-11-04 12:42:02 +0000784 hdmi_modb(hdmi, HDMI_VP_STUFF_PR_STUFFING_STUFFING_MODE,
785 HDMI_VP_STUFF_PR_STUFFING_MASK, HDMI_VP_STUFF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200786
787 /* Data from pixel repeater block */
788 if (hdmi_data->pix_repet_factor > 1) {
Russell Kingbebdf662013-11-04 12:55:30 +0000789 vp_conf = HDMI_VP_CONF_PR_EN_ENABLE |
790 HDMI_VP_CONF_BYPASS_SELECT_PIX_REPEATER;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200791 } else { /* data from packetizer block */
Russell Kingbebdf662013-11-04 12:55:30 +0000792 vp_conf = HDMI_VP_CONF_PR_EN_DISABLE |
793 HDMI_VP_CONF_BYPASS_SELECT_VID_PACKETIZER;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200794 }
795
Russell Kingbebdf662013-11-04 12:55:30 +0000796 hdmi_modb(hdmi, vp_conf,
797 HDMI_VP_CONF_PR_EN_MASK |
798 HDMI_VP_CONF_BYPASS_SELECT_MASK, HDMI_VP_CONF);
799
Russell King812bc612013-11-04 12:42:02 +0000800 hdmi_modb(hdmi, 1 << HDMI_VP_STUFF_IDEFAULT_PHASE_OFFSET,
801 HDMI_VP_STUFF_IDEFAULT_PHASE_MASK, HDMI_VP_STUFF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200802
803 hdmi_writeb(hdmi, remap_size, HDMI_VP_REMAP);
804
805 if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_PP) {
Russell Kingbebdf662013-11-04 12:55:30 +0000806 vp_conf = HDMI_VP_CONF_BYPASS_EN_DISABLE |
807 HDMI_VP_CONF_PP_EN_ENABLE |
808 HDMI_VP_CONF_YCC422_EN_DISABLE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200809 } else if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_YCC422) {
Russell Kingbebdf662013-11-04 12:55:30 +0000810 vp_conf = HDMI_VP_CONF_BYPASS_EN_DISABLE |
811 HDMI_VP_CONF_PP_EN_DISABLE |
812 HDMI_VP_CONF_YCC422_EN_ENABLE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200813 } else if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS) {
Russell Kingbebdf662013-11-04 12:55:30 +0000814 vp_conf = HDMI_VP_CONF_BYPASS_EN_ENABLE |
815 HDMI_VP_CONF_PP_EN_DISABLE |
816 HDMI_VP_CONF_YCC422_EN_DISABLE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200817 } else {
818 return;
819 }
820
Russell Kingbebdf662013-11-04 12:55:30 +0000821 hdmi_modb(hdmi, vp_conf,
822 HDMI_VP_CONF_BYPASS_EN_MASK | HDMI_VP_CONF_PP_EN_ENMASK |
823 HDMI_VP_CONF_YCC422_EN_MASK, HDMI_VP_CONF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200824
Russell King812bc612013-11-04 12:42:02 +0000825 hdmi_modb(hdmi, HDMI_VP_STUFF_PP_STUFFING_STUFFING_MODE |
826 HDMI_VP_STUFF_YCC422_STUFFING_STUFFING_MODE,
827 HDMI_VP_STUFF_PP_STUFFING_MASK |
828 HDMI_VP_STUFF_YCC422_STUFFING_MASK, HDMI_VP_STUFF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200829
Russell King812bc612013-11-04 12:42:02 +0000830 hdmi_modb(hdmi, output_select, HDMI_VP_CONF_OUTPUT_SELECTOR_MASK,
831 HDMI_VP_CONF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200832}
833
Andy Yanb21f4b62014-12-05 14:26:31 +0800834static inline void hdmi_phy_test_clear(struct dw_hdmi *hdmi,
Andy Yanb5878332014-12-05 14:23:52 +0800835 unsigned char bit)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200836{
Russell King812bc612013-11-04 12:42:02 +0000837 hdmi_modb(hdmi, bit << HDMI_PHY_TST0_TSTCLR_OFFSET,
838 HDMI_PHY_TST0_TSTCLR_MASK, HDMI_PHY_TST0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200839}
840
Andy Yanb21f4b62014-12-05 14:26:31 +0800841static bool hdmi_phy_wait_i2c_done(struct dw_hdmi *hdmi, int msec)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200842{
Andy Yana4d3b8b2014-12-05 14:31:09 +0800843 u32 val;
844
845 while ((val = hdmi_readb(hdmi, HDMI_IH_I2CMPHY_STAT0) & 0x3) == 0) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200846 if (msec-- == 0)
847 return false;
Emil Renner Berthing0e6bcf32014-03-30 00:21:21 +0100848 udelay(1000);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200849 }
Andy Yana4d3b8b2014-12-05 14:31:09 +0800850 hdmi_writeb(hdmi, val, HDMI_IH_I2CMPHY_STAT0);
851
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200852 return true;
853}
854
Laurent Pinchartcc7e9622017-01-17 10:28:51 +0200855static void hdmi_phy_i2c_write(struct dw_hdmi *hdmi, unsigned short data,
Andy Yanb5878332014-12-05 14:23:52 +0800856 unsigned char addr)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200857{
858 hdmi_writeb(hdmi, 0xFF, HDMI_IH_I2CMPHY_STAT0);
859 hdmi_writeb(hdmi, addr, HDMI_PHY_I2CM_ADDRESS_ADDR);
860 hdmi_writeb(hdmi, (unsigned char)(data >> 8),
Andy Yanb5878332014-12-05 14:23:52 +0800861 HDMI_PHY_I2CM_DATAO_1_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200862 hdmi_writeb(hdmi, (unsigned char)(data >> 0),
Andy Yanb5878332014-12-05 14:23:52 +0800863 HDMI_PHY_I2CM_DATAO_0_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200864 hdmi_writeb(hdmi, HDMI_PHY_I2CM_OPERATION_ADDR_WRITE,
Andy Yanb5878332014-12-05 14:23:52 +0800865 HDMI_PHY_I2CM_OPERATION_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200866 hdmi_phy_wait_i2c_done(hdmi, 1000);
867}
868
Russell King2fada102015-07-28 12:21:34 +0100869static void dw_hdmi_phy_enable_powerdown(struct dw_hdmi *hdmi, bool enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200870{
Russell King2fada102015-07-28 12:21:34 +0100871 hdmi_mask_writeb(hdmi, !enable, HDMI_PHY_CONF0,
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200872 HDMI_PHY_CONF0_PDZ_OFFSET,
873 HDMI_PHY_CONF0_PDZ_MASK);
874}
875
Andy Yanb21f4b62014-12-05 14:26:31 +0800876static void dw_hdmi_phy_enable_tmds(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200877{
878 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
879 HDMI_PHY_CONF0_ENTMDS_OFFSET,
880 HDMI_PHY_CONF0_ENTMDS_MASK);
881}
882
Laurent Pinchartf4104e82017-01-17 10:29:02 +0200883static void dw_hdmi_phy_enable_svsret(struct dw_hdmi *hdmi, u8 enable)
Andy Yand346c142014-12-05 14:31:53 +0800884{
885 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
Laurent Pinchartf4104e82017-01-17 10:29:02 +0200886 HDMI_PHY_CONF0_SVSRET_OFFSET,
887 HDMI_PHY_CONF0_SVSRET_MASK);
Andy Yand346c142014-12-05 14:31:53 +0800888}
889
Andy Yanb21f4b62014-12-05 14:26:31 +0800890static void dw_hdmi_phy_gen2_pddq(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200891{
892 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
893 HDMI_PHY_CONF0_GEN2_PDDQ_OFFSET,
894 HDMI_PHY_CONF0_GEN2_PDDQ_MASK);
895}
896
Andy Yanb21f4b62014-12-05 14:26:31 +0800897static void dw_hdmi_phy_gen2_txpwron(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200898{
899 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
900 HDMI_PHY_CONF0_GEN2_TXPWRON_OFFSET,
901 HDMI_PHY_CONF0_GEN2_TXPWRON_MASK);
902}
903
Andy Yanb21f4b62014-12-05 14:26:31 +0800904static void dw_hdmi_phy_sel_data_en_pol(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200905{
906 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
907 HDMI_PHY_CONF0_SELDATAENPOL_OFFSET,
908 HDMI_PHY_CONF0_SELDATAENPOL_MASK);
909}
910
Andy Yanb21f4b62014-12-05 14:26:31 +0800911static void dw_hdmi_phy_sel_interface_control(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200912{
913 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
914 HDMI_PHY_CONF0_SELDIPIF_OFFSET,
915 HDMI_PHY_CONF0_SELDIPIF_MASK);
916}
917
Laurent Pinchartb0e583e2017-03-06 01:35:39 +0200918static void dw_hdmi_phy_power_off(struct dw_hdmi *hdmi)
919{
920 const struct dw_hdmi_phy_data *phy = hdmi->phy;
921 unsigned int i;
922 u16 val;
923
924 if (phy->gen == 1) {
925 dw_hdmi_phy_enable_tmds(hdmi, 0);
926 dw_hdmi_phy_enable_powerdown(hdmi, true);
927 return;
928 }
929
930 dw_hdmi_phy_gen2_txpwron(hdmi, 0);
931
932 /*
933 * Wait for TX_PHY_LOCK to be deasserted to indicate that the PHY went
934 * to low power mode.
935 */
936 for (i = 0; i < 5; ++i) {
937 val = hdmi_readb(hdmi, HDMI_PHY_STAT0);
938 if (!(val & HDMI_PHY_TX_PHY_LOCK))
939 break;
940
941 usleep_range(1000, 2000);
942 }
943
944 if (val & HDMI_PHY_TX_PHY_LOCK)
945 dev_warn(hdmi->dev, "PHY failed to power down\n");
946 else
947 dev_dbg(hdmi->dev, "PHY powered down in %u iterations\n", i);
948
949 dw_hdmi_phy_gen2_pddq(hdmi, 1);
950}
951
Laurent Pinchart8b9e1c02017-03-03 19:19:59 +0200952static int hdmi_phy_configure(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200953{
954 u8 val, msec;
Russell King39cc1532015-03-31 18:34:11 +0100955 const struct dw_hdmi_plat_data *pdata = hdmi->plat_data;
956 const struct dw_hdmi_mpll_config *mpll_config = pdata->mpll_cfg;
957 const struct dw_hdmi_curr_ctrl *curr_ctrl = pdata->cur_ctr;
958 const struct dw_hdmi_phy_config *phy_config = pdata->phy_config;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200959
Russell King39cc1532015-03-31 18:34:11 +0100960 /* PLL/MPLL Cfg - always match on final entry */
961 for (; mpll_config->mpixelclock != ~0UL; mpll_config++)
962 if (hdmi->hdmi_data.video_mode.mpixelclock <=
963 mpll_config->mpixelclock)
964 break;
965
966 for (; curr_ctrl->mpixelclock != ~0UL; curr_ctrl++)
967 if (hdmi->hdmi_data.video_mode.mpixelclock <=
968 curr_ctrl->mpixelclock)
969 break;
970
971 for (; phy_config->mpixelclock != ~0UL; phy_config++)
972 if (hdmi->hdmi_data.video_mode.mpixelclock <=
973 phy_config->mpixelclock)
974 break;
975
976 if (mpll_config->mpixelclock == ~0UL ||
977 curr_ctrl->mpixelclock == ~0UL ||
978 phy_config->mpixelclock == ~0UL) {
979 dev_err(hdmi->dev, "Pixel clock %d - unsupported by HDMI\n",
980 hdmi->hdmi_data.video_mode.mpixelclock);
981 return -EINVAL;
982 }
983
Laurent Pinchartb0e583e2017-03-06 01:35:39 +0200984 dw_hdmi_phy_power_off(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200985
Laurent Pinchart2668db32017-01-17 10:29:09 +0200986 /* Leave low power consumption mode by asserting SVSRET. */
987 if (hdmi->phy->has_svsret)
988 dw_hdmi_phy_enable_svsret(hdmi, 1);
989
Laurent Pinchart54d72732017-01-17 10:29:08 +0200990 /* PHY reset. The reset signal is active high on Gen2 PHYs. */
991 hdmi_writeb(hdmi, HDMI_MC_PHYRSTZ_PHYRSTZ, HDMI_MC_PHYRSTZ);
992 hdmi_writeb(hdmi, 0, HDMI_MC_PHYRSTZ);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200993
994 hdmi_writeb(hdmi, HDMI_MC_HEACPHY_RST_ASSERT, HDMI_MC_HEACPHY_RST);
995
996 hdmi_phy_test_clear(hdmi, 1);
997 hdmi_writeb(hdmi, HDMI_PHY_I2CM_SLAVE_ADDR_PHY_GEN2,
Andy Yanb5878332014-12-05 14:23:52 +0800998 HDMI_PHY_I2CM_SLAVE_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200999 hdmi_phy_test_clear(hdmi, 0);
1000
Laurent Pinchartf0e7f2f2017-01-17 10:29:07 +02001001 hdmi_phy_i2c_write(hdmi, mpll_config->res[0].cpce,
1002 HDMI_3D_TX_PHY_CPCE_CTRL);
1003 hdmi_phy_i2c_write(hdmi, mpll_config->res[0].gmp,
1004 HDMI_3D_TX_PHY_GMPCTRL);
1005 hdmi_phy_i2c_write(hdmi, curr_ctrl->curr[0],
1006 HDMI_3D_TX_PHY_CURRCTRL);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001007
Laurent Pinchartf0e7f2f2017-01-17 10:29:07 +02001008 hdmi_phy_i2c_write(hdmi, 0, HDMI_3D_TX_PHY_PLLPHBYCTRL);
1009 hdmi_phy_i2c_write(hdmi, HDMI_3D_TX_PHY_MSM_CTRL_CKO_SEL_FB_CLK,
1010 HDMI_3D_TX_PHY_MSM_CTRL);
Russell King3e46f152013-11-04 11:24:00 +00001011
Laurent Pinchartf0e7f2f2017-01-17 10:29:07 +02001012 hdmi_phy_i2c_write(hdmi, phy_config->term, HDMI_3D_TX_PHY_TXTERM);
1013 hdmi_phy_i2c_write(hdmi, phy_config->sym_ctr,
1014 HDMI_3D_TX_PHY_CKSYMTXCTRL);
1015 hdmi_phy_i2c_write(hdmi, phy_config->vlev_ctr,
1016 HDMI_3D_TX_PHY_VLEVCTRL);
Andy Yanaaa757a2014-12-05 14:25:50 +08001017
Laurent Pinchartf0e7f2f2017-01-17 10:29:07 +02001018 /* Override and disable clock termination. */
1019 hdmi_phy_i2c_write(hdmi, HDMI_3D_TX_PHY_CKCALCTRL_OVERRIDE,
1020 HDMI_3D_TX_PHY_CKCALCTRL);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001021
Russell King2fada102015-07-28 12:21:34 +01001022 dw_hdmi_phy_enable_powerdown(hdmi, false);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001023
1024 /* toggle TMDS enable */
Andy Yanb21f4b62014-12-05 14:26:31 +08001025 dw_hdmi_phy_enable_tmds(hdmi, 0);
1026 dw_hdmi_phy_enable_tmds(hdmi, 1);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001027
1028 /* gen2 tx power on */
Andy Yanb21f4b62014-12-05 14:26:31 +08001029 dw_hdmi_phy_gen2_txpwron(hdmi, 1);
1030 dw_hdmi_phy_gen2_pddq(hdmi, 0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001031
Laurent Pinchart2668db32017-01-17 10:29:09 +02001032 /* Wait for PHY PLL lock */
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001033 msec = 5;
1034 do {
1035 val = hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_TX_PHY_LOCK;
1036 if (!val)
1037 break;
1038
1039 if (msec == 0) {
1040 dev_err(hdmi->dev, "PHY PLL not locked\n");
1041 return -ETIMEDOUT;
1042 }
1043
1044 udelay(1000);
1045 msec--;
1046 } while (1);
1047
1048 return 0;
1049}
1050
Andy Yanb21f4b62014-12-05 14:26:31 +08001051static int dw_hdmi_phy_init(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001052{
1053 int i, ret;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001054
1055 /* HDMI Phy spec says to do the phy initialization sequence twice */
1056 for (i = 0; i < 2; i++) {
Andy Yanb21f4b62014-12-05 14:26:31 +08001057 dw_hdmi_phy_sel_data_en_pol(hdmi, 1);
1058 dw_hdmi_phy_sel_interface_control(hdmi, 0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001059
Laurent Pinchart8b9e1c02017-03-03 19:19:59 +02001060 ret = hdmi_phy_configure(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001061 if (ret)
1062 return ret;
1063 }
1064
1065 hdmi->phy_enabled = true;
1066 return 0;
1067}
1068
Andy Yanb21f4b62014-12-05 14:26:31 +08001069static void hdmi_tx_hdcp_config(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001070{
Russell King812bc612013-11-04 12:42:02 +00001071 u8 de;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001072
1073 if (hdmi->hdmi_data.video_mode.mdataenablepolarity)
1074 de = HDMI_A_VIDPOLCFG_DATAENPOL_ACTIVE_HIGH;
1075 else
1076 de = HDMI_A_VIDPOLCFG_DATAENPOL_ACTIVE_LOW;
1077
1078 /* disable rx detect */
Russell King812bc612013-11-04 12:42:02 +00001079 hdmi_modb(hdmi, HDMI_A_HDCPCFG0_RXDETECT_DISABLE,
1080 HDMI_A_HDCPCFG0_RXDETECT_MASK, HDMI_A_HDCPCFG0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001081
Russell King812bc612013-11-04 12:42:02 +00001082 hdmi_modb(hdmi, de, HDMI_A_VIDPOLCFG_DATAENPOL_MASK, HDMI_A_VIDPOLCFG);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001083
Russell King812bc612013-11-04 12:42:02 +00001084 hdmi_modb(hdmi, HDMI_A_HDCPCFG1_ENCRYPTIONDISABLE_DISABLE,
1085 HDMI_A_HDCPCFG1_ENCRYPTIONDISABLE_MASK, HDMI_A_HDCPCFG1);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001086}
1087
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001088static void hdmi_config_AVI(struct dw_hdmi *hdmi, struct drm_display_mode *mode)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001089{
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001090 struct hdmi_avi_infoframe frame;
1091 u8 val;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001092
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001093 /* Initialise info frame from DRM mode */
1094 drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001095
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001096 if (hdmi->hdmi_data.enc_out_format == YCBCR444)
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001097 frame.colorspace = HDMI_COLORSPACE_YUV444;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001098 else if (hdmi->hdmi_data.enc_out_format == YCBCR422_8BITS)
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001099 frame.colorspace = HDMI_COLORSPACE_YUV422;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001100 else
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001101 frame.colorspace = HDMI_COLORSPACE_RGB;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001102
1103 /* Set up colorimetry */
1104 if (hdmi->hdmi_data.enc_out_format == XVYCC444) {
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001105 frame.colorimetry = HDMI_COLORIMETRY_EXTENDED;
Sachin Kamat5a819ed2014-01-28 10:33:16 +05301106 if (hdmi->hdmi_data.colorimetry == HDMI_COLORIMETRY_ITU_601)
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001107 frame.extended_colorimetry =
1108 HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
Sachin Kamat5a819ed2014-01-28 10:33:16 +05301109 else /*hdmi->hdmi_data.colorimetry == HDMI_COLORIMETRY_ITU_709*/
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001110 frame.extended_colorimetry =
1111 HDMI_EXTENDED_COLORIMETRY_XV_YCC_709;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001112 } else if (hdmi->hdmi_data.enc_out_format != RGB) {
Russell Kingd083c312015-03-27 23:14:16 +00001113 frame.colorimetry = hdmi->hdmi_data.colorimetry;
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001114 frame.extended_colorimetry = HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001115 } else { /* Carries no data */
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001116 frame.colorimetry = HDMI_COLORIMETRY_NONE;
1117 frame.extended_colorimetry = HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001118 }
1119
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001120 frame.scan_mode = HDMI_SCAN_MODE_NONE;
1121
1122 /*
1123 * The Designware IP uses a different byte format from standard
1124 * AVI info frames, though generally the bits are in the correct
1125 * bytes.
1126 */
1127
1128 /*
Jose Abreub0118e72016-08-29 10:30:51 +01001129 * AVI data byte 1 differences: Colorspace in bits 0,1 rather than 5,6,
1130 * scan info in bits 4,5 rather than 0,1 and active aspect present in
1131 * bit 6 rather than 4.
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001132 */
Jose Abreub0118e72016-08-29 10:30:51 +01001133 val = (frame.scan_mode & 3) << 4 | (frame.colorspace & 3);
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001134 if (frame.active_aspect & 15)
1135 val |= HDMI_FC_AVICONF0_ACTIVE_FMT_INFO_PRESENT;
1136 if (frame.top_bar || frame.bottom_bar)
1137 val |= HDMI_FC_AVICONF0_BAR_DATA_HORIZ_BAR;
1138 if (frame.left_bar || frame.right_bar)
1139 val |= HDMI_FC_AVICONF0_BAR_DATA_VERT_BAR;
1140 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF0);
1141
1142 /* AVI data byte 2 differences: none */
1143 val = ((frame.colorimetry & 0x3) << 6) |
1144 ((frame.picture_aspect & 0x3) << 4) |
1145 (frame.active_aspect & 0xf);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001146 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF1);
1147
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001148 /* AVI data byte 3 differences: none */
1149 val = ((frame.extended_colorimetry & 0x7) << 4) |
1150 ((frame.quantization_range & 0x3) << 2) |
1151 (frame.nups & 0x3);
1152 if (frame.itc)
1153 val |= HDMI_FC_AVICONF2_IT_CONTENT_VALID;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001154 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF2);
1155
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001156 /* AVI data byte 4 differences: none */
1157 val = frame.video_code & 0x7f;
1158 hdmi_writeb(hdmi, val, HDMI_FC_AVIVID);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001159
1160 /* AVI Data Byte 5- set up input and output pixel repetition */
1161 val = (((hdmi->hdmi_data.video_mode.mpixelrepetitioninput + 1) <<
1162 HDMI_FC_PRCONF_INCOMING_PR_FACTOR_OFFSET) &
1163 HDMI_FC_PRCONF_INCOMING_PR_FACTOR_MASK) |
1164 ((hdmi->hdmi_data.video_mode.mpixelrepetitionoutput <<
1165 HDMI_FC_PRCONF_OUTPUT_PR_FACTOR_OFFSET) &
1166 HDMI_FC_PRCONF_OUTPUT_PR_FACTOR_MASK);
1167 hdmi_writeb(hdmi, val, HDMI_FC_PRCONF);
1168
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001169 /*
1170 * AVI data byte 5 differences: content type in 0,1 rather than 4,5,
1171 * ycc range in bits 2,3 rather than 6,7
1172 */
1173 val = ((frame.ycc_quantization_range & 0x3) << 2) |
1174 (frame.content_type & 0x3);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001175 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF3);
1176
1177 /* AVI Data Bytes 6-13 */
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001178 hdmi_writeb(hdmi, frame.top_bar & 0xff, HDMI_FC_AVIETB0);
1179 hdmi_writeb(hdmi, (frame.top_bar >> 8) & 0xff, HDMI_FC_AVIETB1);
1180 hdmi_writeb(hdmi, frame.bottom_bar & 0xff, HDMI_FC_AVISBB0);
1181 hdmi_writeb(hdmi, (frame.bottom_bar >> 8) & 0xff, HDMI_FC_AVISBB1);
1182 hdmi_writeb(hdmi, frame.left_bar & 0xff, HDMI_FC_AVIELB0);
1183 hdmi_writeb(hdmi, (frame.left_bar >> 8) & 0xff, HDMI_FC_AVIELB1);
1184 hdmi_writeb(hdmi, frame.right_bar & 0xff, HDMI_FC_AVISRB0);
1185 hdmi_writeb(hdmi, (frame.right_bar >> 8) & 0xff, HDMI_FC_AVISRB1);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001186}
1187
Andy Yanb21f4b62014-12-05 14:26:31 +08001188static void hdmi_av_composer(struct dw_hdmi *hdmi,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001189 const struct drm_display_mode *mode)
1190{
1191 u8 inv_val;
1192 struct hdmi_vmode *vmode = &hdmi->hdmi_data.video_mode;
1193 int hblank, vblank, h_de_hs, v_de_vs, hsync_len, vsync_len;
Russell Kinge80b9f42015-07-21 11:08:25 +01001194 unsigned int vdisplay;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001195
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001196 vmode->mpixelclock = mode->clock * 1000;
1197
1198 dev_dbg(hdmi->dev, "final pixclk = %d\n", vmode->mpixelclock);
1199
1200 /* Set up HDMI_FC_INVIDCONF */
1201 inv_val = (hdmi->hdmi_data.hdcp_enable ?
1202 HDMI_FC_INVIDCONF_HDCP_KEEPOUT_ACTIVE :
1203 HDMI_FC_INVIDCONF_HDCP_KEEPOUT_INACTIVE);
1204
Russell Kingb91eee82015-03-27 23:27:17 +00001205 inv_val |= mode->flags & DRM_MODE_FLAG_PVSYNC ?
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001206 HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_HIGH :
Russell Kingb91eee82015-03-27 23:27:17 +00001207 HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_LOW;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001208
Russell Kingb91eee82015-03-27 23:27:17 +00001209 inv_val |= mode->flags & DRM_MODE_FLAG_PHSYNC ?
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001210 HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_HIGH :
Russell Kingb91eee82015-03-27 23:27:17 +00001211 HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_LOW;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001212
1213 inv_val |= (vmode->mdataenablepolarity ?
1214 HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_HIGH :
1215 HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_LOW);
1216
1217 if (hdmi->vic == 39)
1218 inv_val |= HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_HIGH;
1219 else
Russell Kingb91eee82015-03-27 23:27:17 +00001220 inv_val |= mode->flags & DRM_MODE_FLAG_INTERLACE ?
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001221 HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_HIGH :
Russell Kingb91eee82015-03-27 23:27:17 +00001222 HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_LOW;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001223
Russell Kingb91eee82015-03-27 23:27:17 +00001224 inv_val |= mode->flags & DRM_MODE_FLAG_INTERLACE ?
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001225 HDMI_FC_INVIDCONF_IN_I_P_INTERLACED :
Russell Kingb91eee82015-03-27 23:27:17 +00001226 HDMI_FC_INVIDCONF_IN_I_P_PROGRESSIVE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001227
Russell King05b13422015-07-21 15:35:52 +01001228 inv_val |= hdmi->sink_is_hdmi ?
1229 HDMI_FC_INVIDCONF_DVI_MODEZ_HDMI_MODE :
1230 HDMI_FC_INVIDCONF_DVI_MODEZ_DVI_MODE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001231
1232 hdmi_writeb(hdmi, inv_val, HDMI_FC_INVIDCONF);
1233
Russell Kinge80b9f42015-07-21 11:08:25 +01001234 vdisplay = mode->vdisplay;
1235 vblank = mode->vtotal - mode->vdisplay;
1236 v_de_vs = mode->vsync_start - mode->vdisplay;
1237 vsync_len = mode->vsync_end - mode->vsync_start;
1238
1239 /*
1240 * When we're setting an interlaced mode, we need
1241 * to adjust the vertical timing to suit.
1242 */
1243 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
1244 vdisplay /= 2;
1245 vblank /= 2;
1246 v_de_vs /= 2;
1247 vsync_len /= 2;
1248 }
1249
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001250 /* Set up horizontal active pixel width */
1251 hdmi_writeb(hdmi, mode->hdisplay >> 8, HDMI_FC_INHACTV1);
1252 hdmi_writeb(hdmi, mode->hdisplay, HDMI_FC_INHACTV0);
1253
1254 /* Set up vertical active lines */
Russell Kinge80b9f42015-07-21 11:08:25 +01001255 hdmi_writeb(hdmi, vdisplay >> 8, HDMI_FC_INVACTV1);
1256 hdmi_writeb(hdmi, vdisplay, HDMI_FC_INVACTV0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001257
1258 /* Set up horizontal blanking pixel region width */
1259 hblank = mode->htotal - mode->hdisplay;
1260 hdmi_writeb(hdmi, hblank >> 8, HDMI_FC_INHBLANK1);
1261 hdmi_writeb(hdmi, hblank, HDMI_FC_INHBLANK0);
1262
1263 /* Set up vertical blanking pixel region width */
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001264 hdmi_writeb(hdmi, vblank, HDMI_FC_INVBLANK);
1265
1266 /* Set up HSYNC active edge delay width (in pixel clks) */
1267 h_de_hs = mode->hsync_start - mode->hdisplay;
1268 hdmi_writeb(hdmi, h_de_hs >> 8, HDMI_FC_HSYNCINDELAY1);
1269 hdmi_writeb(hdmi, h_de_hs, HDMI_FC_HSYNCINDELAY0);
1270
1271 /* Set up VSYNC active edge delay (in lines) */
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001272 hdmi_writeb(hdmi, v_de_vs, HDMI_FC_VSYNCINDELAY);
1273
1274 /* Set up HSYNC active pulse width (in pixel clks) */
1275 hsync_len = mode->hsync_end - mode->hsync_start;
1276 hdmi_writeb(hdmi, hsync_len >> 8, HDMI_FC_HSYNCINWIDTH1);
1277 hdmi_writeb(hdmi, hsync_len, HDMI_FC_HSYNCINWIDTH0);
1278
1279 /* Set up VSYNC active edge delay (in lines) */
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001280 hdmi_writeb(hdmi, vsync_len, HDMI_FC_VSYNCINWIDTH);
1281}
1282
Andy Yanb21f4b62014-12-05 14:26:31 +08001283static void dw_hdmi_phy_disable(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001284{
1285 if (!hdmi->phy_enabled)
1286 return;
1287
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02001288 dw_hdmi_phy_power_off(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001289
1290 hdmi->phy_enabled = false;
1291}
1292
1293/* HDMI Initialization Step B.4 */
Andy Yanb21f4b62014-12-05 14:26:31 +08001294static void dw_hdmi_enable_video_path(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001295{
1296 u8 clkdis;
1297
1298 /* control period minimum duration */
1299 hdmi_writeb(hdmi, 12, HDMI_FC_CTRLDUR);
1300 hdmi_writeb(hdmi, 32, HDMI_FC_EXCTRLDUR);
1301 hdmi_writeb(hdmi, 1, HDMI_FC_EXCTRLSPAC);
1302
1303 /* Set to fill TMDS data channels */
1304 hdmi_writeb(hdmi, 0x0B, HDMI_FC_CH0PREAM);
1305 hdmi_writeb(hdmi, 0x16, HDMI_FC_CH1PREAM);
1306 hdmi_writeb(hdmi, 0x21, HDMI_FC_CH2PREAM);
1307
1308 /* Enable pixel clock and tmds data path */
1309 clkdis = 0x7F;
1310 clkdis &= ~HDMI_MC_CLKDIS_PIXELCLK_DISABLE;
1311 hdmi_writeb(hdmi, clkdis, HDMI_MC_CLKDIS);
1312
1313 clkdis &= ~HDMI_MC_CLKDIS_TMDSCLK_DISABLE;
1314 hdmi_writeb(hdmi, clkdis, HDMI_MC_CLKDIS);
1315
1316 /* Enable csc path */
1317 if (is_color_space_conversion(hdmi)) {
1318 clkdis &= ~HDMI_MC_CLKDIS_CSCCLK_DISABLE;
1319 hdmi_writeb(hdmi, clkdis, HDMI_MC_CLKDIS);
1320 }
Laurent Pinchart8b9e1c02017-03-03 19:19:59 +02001321
Neil Armstrong14247d72017-03-03 19:20:00 +02001322 /* Enable color space conversion if needed */
1323 if (is_color_space_conversion(hdmi))
Laurent Pinchart8b9e1c02017-03-03 19:19:59 +02001324 hdmi_writeb(hdmi, HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_IN_PATH,
1325 HDMI_MC_FLOWCTRL);
1326 else
1327 hdmi_writeb(hdmi, HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_BYPASS,
1328 HDMI_MC_FLOWCTRL);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001329}
1330
Andy Yanb21f4b62014-12-05 14:26:31 +08001331static void hdmi_enable_audio_clk(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001332{
Russell King812bc612013-11-04 12:42:02 +00001333 hdmi_modb(hdmi, 0, HDMI_MC_CLKDIS_AUDCLK_DISABLE, HDMI_MC_CLKDIS);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001334}
1335
1336/* Workaround to clear the overflow condition */
Andy Yanb21f4b62014-12-05 14:26:31 +08001337static void dw_hdmi_clear_overflow(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001338{
Laurent Pinchartbe41fc52017-01-17 10:29:05 +02001339 unsigned int count;
1340 unsigned int i;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001341 u8 val;
1342
Laurent Pinchartbe41fc52017-01-17 10:29:05 +02001343 /*
1344 * Under some circumstances the Frame Composer arithmetic unit can miss
1345 * an FC register write due to being busy processing the previous one.
1346 * The issue can be worked around by issuing a TMDS software reset and
1347 * then write one of the FC registers several times.
1348 *
1349 * The number of iterations matters and depends on the HDMI TX revision
1350 * (and possibly on the platform). So far only i.MX6Q (v1.30a) and
1351 * i.MX6DL (v1.31a) have been identified as needing the workaround, with
1352 * 4 and 1 iterations respectively.
1353 */
1354
1355 switch (hdmi->version) {
1356 case 0x130a:
1357 count = 4;
1358 break;
1359 case 0x131a:
1360 count = 1;
1361 break;
1362 default:
1363 return;
1364 }
1365
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001366 /* TMDS software reset */
1367 hdmi_writeb(hdmi, (u8)~HDMI_MC_SWRSTZ_TMDSSWRST_REQ, HDMI_MC_SWRSTZ);
1368
1369 val = hdmi_readb(hdmi, HDMI_FC_INVIDCONF);
Laurent Pinchartbe41fc52017-01-17 10:29:05 +02001370 for (i = 0; i < count; i++)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001371 hdmi_writeb(hdmi, val, HDMI_FC_INVIDCONF);
1372}
1373
Andy Yanb21f4b62014-12-05 14:26:31 +08001374static void hdmi_enable_overflow_interrupts(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001375{
1376 hdmi_writeb(hdmi, 0, HDMI_FC_MASK2);
1377 hdmi_writeb(hdmi, 0, HDMI_IH_MUTE_FC_STAT2);
1378}
1379
Andy Yanb21f4b62014-12-05 14:26:31 +08001380static void hdmi_disable_overflow_interrupts(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001381{
1382 hdmi_writeb(hdmi, HDMI_IH_MUTE_FC_STAT2_OVERFLOW_MASK,
1383 HDMI_IH_MUTE_FC_STAT2);
1384}
1385
Andy Yanb21f4b62014-12-05 14:26:31 +08001386static int dw_hdmi_setup(struct dw_hdmi *hdmi, struct drm_display_mode *mode)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001387{
1388 int ret;
1389
1390 hdmi_disable_overflow_interrupts(hdmi);
1391
1392 hdmi->vic = drm_match_cea_mode(mode);
1393
1394 if (!hdmi->vic) {
1395 dev_dbg(hdmi->dev, "Non-CEA mode used in HDMI\n");
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001396 } else {
1397 dev_dbg(hdmi->dev, "CEA mode used vic=%d\n", hdmi->vic);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001398 }
1399
1400 if ((hdmi->vic == 6) || (hdmi->vic == 7) ||
Andy Yanb5878332014-12-05 14:23:52 +08001401 (hdmi->vic == 21) || (hdmi->vic == 22) ||
1402 (hdmi->vic == 2) || (hdmi->vic == 3) ||
1403 (hdmi->vic == 17) || (hdmi->vic == 18))
Sachin Kamat5a819ed2014-01-28 10:33:16 +05301404 hdmi->hdmi_data.colorimetry = HDMI_COLORIMETRY_ITU_601;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001405 else
Sachin Kamat5a819ed2014-01-28 10:33:16 +05301406 hdmi->hdmi_data.colorimetry = HDMI_COLORIMETRY_ITU_709;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001407
Russell Kingd10ca822015-07-21 11:25:00 +01001408 hdmi->hdmi_data.video_mode.mpixelrepetitionoutput = 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001409 hdmi->hdmi_data.video_mode.mpixelrepetitioninput = 0;
1410
1411 /* TODO: Get input format from IPU (via FB driver interface) */
1412 hdmi->hdmi_data.enc_in_format = RGB;
1413
1414 hdmi->hdmi_data.enc_out_format = RGB;
1415
1416 hdmi->hdmi_data.enc_color_depth = 8;
1417 hdmi->hdmi_data.pix_repet_factor = 0;
1418 hdmi->hdmi_data.hdcp_enable = 0;
1419 hdmi->hdmi_data.video_mode.mdataenablepolarity = true;
1420
1421 /* HDMI Initialization Step B.1 */
1422 hdmi_av_composer(hdmi, mode);
1423
1424 /* HDMI Initializateion Step B.2 */
Andy Yanb21f4b62014-12-05 14:26:31 +08001425 ret = dw_hdmi_phy_init(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001426 if (ret)
1427 return ret;
1428
1429 /* HDMI Initialization Step B.3 */
Andy Yanb21f4b62014-12-05 14:26:31 +08001430 dw_hdmi_enable_video_path(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001431
Russell Kingf709ec02015-07-21 16:09:39 +01001432 if (hdmi->sink_has_audio) {
1433 dev_dbg(hdmi->dev, "sink has audio support\n");
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001434
1435 /* HDMI Initialization Step E - Configure audio */
1436 hdmi_clk_regenerator_update_pixel_clock(hdmi);
1437 hdmi_enable_audio_clk(hdmi);
Russell Kingf709ec02015-07-21 16:09:39 +01001438 }
1439
1440 /* not for DVI mode */
1441 if (hdmi->sink_is_hdmi) {
1442 dev_dbg(hdmi->dev, "%s HDMI mode\n", __func__);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001443
1444 /* HDMI Initialization Step F - Configure AVI InfoFrame */
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001445 hdmi_config_AVI(hdmi, mode);
Russell King05b13422015-07-21 15:35:52 +01001446 } else {
1447 dev_dbg(hdmi->dev, "%s DVI mode\n", __func__);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001448 }
1449
1450 hdmi_video_packetize(hdmi);
1451 hdmi_video_csc(hdmi);
1452 hdmi_video_sample(hdmi);
1453 hdmi_tx_hdcp_config(hdmi);
1454
Andy Yanb21f4b62014-12-05 14:26:31 +08001455 dw_hdmi_clear_overflow(hdmi);
Russell King05b13422015-07-21 15:35:52 +01001456 if (hdmi->cable_plugin && hdmi->sink_is_hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001457 hdmi_enable_overflow_interrupts(hdmi);
1458
1459 return 0;
1460}
1461
1462/* Wait until we are registered to enable interrupts */
Andy Yanb21f4b62014-12-05 14:26:31 +08001463static int dw_hdmi_fb_registered(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001464{
1465 hdmi_writeb(hdmi, HDMI_PHY_I2CM_INT_ADDR_DONE_POL,
1466 HDMI_PHY_I2CM_INT_ADDR);
1467
1468 hdmi_writeb(hdmi, HDMI_PHY_I2CM_CTLINT_ADDR_NAC_POL |
1469 HDMI_PHY_I2CM_CTLINT_ADDR_ARBITRATION_POL,
1470 HDMI_PHY_I2CM_CTLINT_ADDR);
1471
1472 /* enable cable hot plug irq */
Russell Kingaeac23b2015-06-05 13:46:22 +01001473 hdmi_writeb(hdmi, hdmi->phy_mask, HDMI_PHY_MASK0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001474
1475 /* Clear Hotplug interrupts */
Russell Kingaeac23b2015-06-05 13:46:22 +01001476 hdmi_writeb(hdmi, HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE,
1477 HDMI_IH_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001478
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001479 return 0;
1480}
1481
Andy Yanb21f4b62014-12-05 14:26:31 +08001482static void initialize_hdmi_ih_mutes(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001483{
1484 u8 ih_mute;
1485
1486 /*
1487 * Boot up defaults are:
1488 * HDMI_IH_MUTE = 0x03 (disabled)
1489 * HDMI_IH_MUTE_* = 0x00 (enabled)
1490 *
1491 * Disable top level interrupt bits in HDMI block
1492 */
1493 ih_mute = hdmi_readb(hdmi, HDMI_IH_MUTE) |
1494 HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |
1495 HDMI_IH_MUTE_MUTE_ALL_INTERRUPT;
1496
1497 hdmi_writeb(hdmi, ih_mute, HDMI_IH_MUTE);
1498
1499 /* by default mask all interrupts */
1500 hdmi_writeb(hdmi, 0xff, HDMI_VP_MASK);
1501 hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK0);
1502 hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK1);
1503 hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK2);
1504 hdmi_writeb(hdmi, 0xff, HDMI_PHY_MASK0);
1505 hdmi_writeb(hdmi, 0xff, HDMI_PHY_I2CM_INT_ADDR);
1506 hdmi_writeb(hdmi, 0xff, HDMI_PHY_I2CM_CTLINT_ADDR);
1507 hdmi_writeb(hdmi, 0xff, HDMI_AUD_INT);
1508 hdmi_writeb(hdmi, 0xff, HDMI_AUD_SPDIFINT);
1509 hdmi_writeb(hdmi, 0xff, HDMI_AUD_HBR_MASK);
1510 hdmi_writeb(hdmi, 0xff, HDMI_GP_MASK);
1511 hdmi_writeb(hdmi, 0xff, HDMI_A_APIINTMSK);
1512 hdmi_writeb(hdmi, 0xff, HDMI_CEC_MASK);
1513 hdmi_writeb(hdmi, 0xff, HDMI_I2CM_INT);
1514 hdmi_writeb(hdmi, 0xff, HDMI_I2CM_CTLINT);
1515
1516 /* Disable interrupts in the IH_MUTE_* registers */
1517 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT0);
1518 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT1);
1519 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT2);
1520 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_AS_STAT0);
1521 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_PHY_STAT0);
1522 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_I2CM_STAT0);
1523 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_CEC_STAT0);
1524 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_VP_STAT0);
1525 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_I2CMPHY_STAT0);
1526 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_AHBDMAAUD_STAT0);
1527
1528 /* Enable top level interrupt bits in HDMI block */
1529 ih_mute &= ~(HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |
1530 HDMI_IH_MUTE_MUTE_ALL_INTERRUPT);
1531 hdmi_writeb(hdmi, ih_mute, HDMI_IH_MUTE);
1532}
1533
Andy Yanb21f4b62014-12-05 14:26:31 +08001534static void dw_hdmi_poweron(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001535{
Russell King381f05a2015-06-05 15:25:08 +01001536 hdmi->bridge_is_on = true;
Andy Yanb21f4b62014-12-05 14:26:31 +08001537 dw_hdmi_setup(hdmi, &hdmi->previous_mode);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001538}
1539
Andy Yanb21f4b62014-12-05 14:26:31 +08001540static void dw_hdmi_poweroff(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001541{
Andy Yanb21f4b62014-12-05 14:26:31 +08001542 dw_hdmi_phy_disable(hdmi);
Russell King381f05a2015-06-05 15:25:08 +01001543 hdmi->bridge_is_on = false;
1544}
1545
1546static void dw_hdmi_update_power(struct dw_hdmi *hdmi)
1547{
1548 int force = hdmi->force;
1549
1550 if (hdmi->disabled) {
1551 force = DRM_FORCE_OFF;
1552 } else if (force == DRM_FORCE_UNSPECIFIED) {
Russell Kingaeac23b2015-06-05 13:46:22 +01001553 if (hdmi->rxsense)
Russell King381f05a2015-06-05 15:25:08 +01001554 force = DRM_FORCE_ON;
1555 else
1556 force = DRM_FORCE_OFF;
1557 }
1558
1559 if (force == DRM_FORCE_OFF) {
1560 if (hdmi->bridge_is_on)
1561 dw_hdmi_poweroff(hdmi);
1562 } else {
1563 if (!hdmi->bridge_is_on)
1564 dw_hdmi_poweron(hdmi);
1565 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001566}
1567
Russell Kingaeac23b2015-06-05 13:46:22 +01001568/*
1569 * Adjust the detection of RXSENSE according to whether we have a forced
1570 * connection mode enabled, or whether we have been disabled. There is
1571 * no point processing RXSENSE interrupts if we have a forced connection
1572 * state, or DRM has us disabled.
1573 *
1574 * We also disable rxsense interrupts when we think we're disconnected
1575 * to avoid floating TDMS signals giving false rxsense interrupts.
1576 *
1577 * Note: we still need to listen for HPD interrupts even when DRM has us
1578 * disabled so that we can detect a connect event.
1579 */
1580static void dw_hdmi_update_phy_mask(struct dw_hdmi *hdmi)
1581{
1582 u8 old_mask = hdmi->phy_mask;
1583
1584 if (hdmi->force || hdmi->disabled || !hdmi->rxsense)
1585 hdmi->phy_mask |= HDMI_PHY_RX_SENSE;
1586 else
1587 hdmi->phy_mask &= ~HDMI_PHY_RX_SENSE;
1588
1589 if (old_mask != hdmi->phy_mask)
1590 hdmi_writeb(hdmi, hdmi->phy_mask, HDMI_PHY_MASK0);
1591}
1592
Andy Yanb21f4b62014-12-05 14:26:31 +08001593static enum drm_connector_status
1594dw_hdmi_connector_detect(struct drm_connector *connector, bool force)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001595{
Andy Yanb21f4b62014-12-05 14:26:31 +08001596 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
Russell Kingd94905e2013-11-03 22:23:24 +00001597 connector);
Russell King98dbead2014-04-18 10:46:45 +01001598
Russell King381f05a2015-06-05 15:25:08 +01001599 mutex_lock(&hdmi->mutex);
1600 hdmi->force = DRM_FORCE_UNSPECIFIED;
1601 dw_hdmi_update_power(hdmi);
Russell Kingaeac23b2015-06-05 13:46:22 +01001602 dw_hdmi_update_phy_mask(hdmi);
Russell King381f05a2015-06-05 15:25:08 +01001603 mutex_unlock(&hdmi->mutex);
1604
Russell King98dbead2014-04-18 10:46:45 +01001605 return hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_HPD ?
1606 connector_status_connected : connector_status_disconnected;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001607}
1608
Andy Yanb21f4b62014-12-05 14:26:31 +08001609static int dw_hdmi_connector_get_modes(struct drm_connector *connector)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001610{
Andy Yanb21f4b62014-12-05 14:26:31 +08001611 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001612 connector);
1613 struct edid *edid;
Doug Anderson6c7e66e2015-06-04 11:04:36 -07001614 int ret = 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001615
1616 if (!hdmi->ddc)
1617 return 0;
1618
1619 edid = drm_get_edid(connector, hdmi->ddc);
1620 if (edid) {
1621 dev_dbg(hdmi->dev, "got edid: width[%d] x height[%d]\n",
1622 edid->width_cm, edid->height_cm);
1623
Russell King05b13422015-07-21 15:35:52 +01001624 hdmi->sink_is_hdmi = drm_detect_hdmi_monitor(edid);
Russell Kingf709ec02015-07-21 16:09:39 +01001625 hdmi->sink_has_audio = drm_detect_monitor_audio(edid);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001626 drm_mode_connector_update_edid_property(connector, edid);
1627 ret = drm_add_edid_modes(connector, edid);
Russell Kingf5ce4052013-11-07 16:06:01 +00001628 /* Store the ELD */
1629 drm_edid_to_eld(connector, edid);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001630 kfree(edid);
1631 } else {
1632 dev_dbg(hdmi->dev, "failed to get edid\n");
1633 }
1634
Doug Anderson6c7e66e2015-06-04 11:04:36 -07001635 return ret;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001636}
1637
Andy Yan632d0352014-12-05 14:30:21 +08001638static enum drm_mode_status
1639dw_hdmi_connector_mode_valid(struct drm_connector *connector,
1640 struct drm_display_mode *mode)
1641{
1642 struct dw_hdmi *hdmi = container_of(connector,
1643 struct dw_hdmi, connector);
1644 enum drm_mode_status mode_status = MODE_OK;
1645
Russell King8add4192015-07-22 11:14:00 +01001646 /* We don't support double-clocked modes */
1647 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
1648 return MODE_BAD;
1649
Andy Yan632d0352014-12-05 14:30:21 +08001650 if (hdmi->plat_data->mode_valid)
1651 mode_status = hdmi->plat_data->mode_valid(connector, mode);
1652
1653 return mode_status;
1654}
1655
Russell King381f05a2015-06-05 15:25:08 +01001656static void dw_hdmi_connector_force(struct drm_connector *connector)
1657{
1658 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
1659 connector);
1660
1661 mutex_lock(&hdmi->mutex);
1662 hdmi->force = connector->force;
1663 dw_hdmi_update_power(hdmi);
Russell Kingaeac23b2015-06-05 13:46:22 +01001664 dw_hdmi_update_phy_mask(hdmi);
Russell King381f05a2015-06-05 15:25:08 +01001665 mutex_unlock(&hdmi->mutex);
1666}
1667
Ville Syrjälädae91e42015-12-15 12:21:02 +01001668static const struct drm_connector_funcs dw_hdmi_connector_funcs = {
Mark Yao2c5b2cc2015-11-30 18:33:40 +08001669 .dpms = drm_atomic_helper_connector_dpms,
1670 .fill_modes = drm_helper_probe_single_connector_modes,
1671 .detect = dw_hdmi_connector_detect,
Marek Vasutfdd83262016-10-05 16:31:33 +02001672 .destroy = drm_connector_cleanup,
Mark Yao2c5b2cc2015-11-30 18:33:40 +08001673 .force = dw_hdmi_connector_force,
1674 .reset = drm_atomic_helper_connector_reset,
1675 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1676 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1677};
1678
Ville Syrjälädae91e42015-12-15 12:21:02 +01001679static const struct drm_connector_helper_funcs dw_hdmi_connector_helper_funcs = {
Andy Yanb21f4b62014-12-05 14:26:31 +08001680 .get_modes = dw_hdmi_connector_get_modes,
Andy Yan632d0352014-12-05 14:30:21 +08001681 .mode_valid = dw_hdmi_connector_mode_valid,
Boris Brezillonc2a441f2016-06-07 13:48:15 +02001682 .best_encoder = drm_atomic_helper_best_encoder,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001683};
1684
Laurent Pinchartd2ae94a2017-01-17 10:28:59 +02001685static int dw_hdmi_bridge_attach(struct drm_bridge *bridge)
1686{
1687 struct dw_hdmi *hdmi = bridge->driver_private;
1688 struct drm_encoder *encoder = bridge->encoder;
1689 struct drm_connector *connector = &hdmi->connector;
1690
1691 connector->interlace_allowed = 1;
1692 connector->polled = DRM_CONNECTOR_POLL_HPD;
1693
1694 drm_connector_helper_add(connector, &dw_hdmi_connector_helper_funcs);
1695
1696 drm_connector_init(bridge->dev, connector, &dw_hdmi_connector_funcs,
1697 DRM_MODE_CONNECTOR_HDMIA);
1698
1699 drm_mode_connector_attach_encoder(connector, encoder);
1700
1701 return 0;
1702}
1703
Laurent Pinchartfd30b382017-01-17 10:28:58 +02001704static void dw_hdmi_bridge_mode_set(struct drm_bridge *bridge,
1705 struct drm_display_mode *orig_mode,
1706 struct drm_display_mode *mode)
1707{
1708 struct dw_hdmi *hdmi = bridge->driver_private;
1709
1710 mutex_lock(&hdmi->mutex);
1711
1712 /* Store the display mode for plugin/DKMS poweron events */
1713 memcpy(&hdmi->previous_mode, mode, sizeof(hdmi->previous_mode));
1714
1715 mutex_unlock(&hdmi->mutex);
1716}
1717
1718static void dw_hdmi_bridge_disable(struct drm_bridge *bridge)
1719{
1720 struct dw_hdmi *hdmi = bridge->driver_private;
1721
1722 mutex_lock(&hdmi->mutex);
1723 hdmi->disabled = true;
1724 dw_hdmi_update_power(hdmi);
1725 dw_hdmi_update_phy_mask(hdmi);
1726 mutex_unlock(&hdmi->mutex);
1727}
1728
1729static void dw_hdmi_bridge_enable(struct drm_bridge *bridge)
1730{
1731 struct dw_hdmi *hdmi = bridge->driver_private;
1732
1733 mutex_lock(&hdmi->mutex);
1734 hdmi->disabled = false;
1735 dw_hdmi_update_power(hdmi);
1736 dw_hdmi_update_phy_mask(hdmi);
1737 mutex_unlock(&hdmi->mutex);
1738}
1739
Ville Syrjälädae91e42015-12-15 12:21:02 +01001740static const struct drm_bridge_funcs dw_hdmi_bridge_funcs = {
Laurent Pinchartd2ae94a2017-01-17 10:28:59 +02001741 .attach = dw_hdmi_bridge_attach,
Andy Yanb21f4b62014-12-05 14:26:31 +08001742 .enable = dw_hdmi_bridge_enable,
1743 .disable = dw_hdmi_bridge_disable,
Andy Yanb21f4b62014-12-05 14:26:31 +08001744 .mode_set = dw_hdmi_bridge_mode_set,
Andy Yan3d1b35a2014-12-05 14:25:05 +08001745};
1746
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03001747static irqreturn_t dw_hdmi_i2c_irq(struct dw_hdmi *hdmi)
1748{
1749 struct dw_hdmi_i2c *i2c = hdmi->i2c;
1750 unsigned int stat;
1751
1752 stat = hdmi_readb(hdmi, HDMI_IH_I2CM_STAT0);
1753 if (!stat)
1754 return IRQ_NONE;
1755
1756 hdmi_writeb(hdmi, stat, HDMI_IH_I2CM_STAT0);
1757
1758 i2c->stat = stat;
1759
1760 complete(&i2c->cmp);
1761
1762 return IRQ_HANDLED;
1763}
1764
Andy Yanb21f4b62014-12-05 14:26:31 +08001765static irqreturn_t dw_hdmi_hardirq(int irq, void *dev_id)
Russell Kingd94905e2013-11-03 22:23:24 +00001766{
Andy Yanb21f4b62014-12-05 14:26:31 +08001767 struct dw_hdmi *hdmi = dev_id;
Russell Kingd94905e2013-11-03 22:23:24 +00001768 u8 intr_stat;
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03001769 irqreturn_t ret = IRQ_NONE;
1770
1771 if (hdmi->i2c)
1772 ret = dw_hdmi_i2c_irq(hdmi);
Russell Kingd94905e2013-11-03 22:23:24 +00001773
1774 intr_stat = hdmi_readb(hdmi, HDMI_IH_PHY_STAT0);
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03001775 if (intr_stat) {
Russell Kingd94905e2013-11-03 22:23:24 +00001776 hdmi_writeb(hdmi, ~0, HDMI_IH_MUTE_PHY_STAT0);
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03001777 return IRQ_WAKE_THREAD;
1778 }
Russell Kingd94905e2013-11-03 22:23:24 +00001779
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03001780 return ret;
Russell Kingd94905e2013-11-03 22:23:24 +00001781}
1782
Andy Yanb21f4b62014-12-05 14:26:31 +08001783static irqreturn_t dw_hdmi_irq(int irq, void *dev_id)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001784{
Andy Yanb21f4b62014-12-05 14:26:31 +08001785 struct dw_hdmi *hdmi = dev_id;
Russell Kingaeac23b2015-06-05 13:46:22 +01001786 u8 intr_stat, phy_int_pol, phy_pol_mask, phy_stat;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001787
1788 intr_stat = hdmi_readb(hdmi, HDMI_IH_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001789 phy_int_pol = hdmi_readb(hdmi, HDMI_PHY_POL0);
Russell Kingaeac23b2015-06-05 13:46:22 +01001790 phy_stat = hdmi_readb(hdmi, HDMI_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001791
Russell Kingaeac23b2015-06-05 13:46:22 +01001792 phy_pol_mask = 0;
1793 if (intr_stat & HDMI_IH_PHY_STAT0_HPD)
1794 phy_pol_mask |= HDMI_PHY_HPD;
1795 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE0)
1796 phy_pol_mask |= HDMI_PHY_RX_SENSE0;
1797 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE1)
1798 phy_pol_mask |= HDMI_PHY_RX_SENSE1;
1799 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE2)
1800 phy_pol_mask |= HDMI_PHY_RX_SENSE2;
1801 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE3)
1802 phy_pol_mask |= HDMI_PHY_RX_SENSE3;
1803
1804 if (phy_pol_mask)
1805 hdmi_modb(hdmi, ~phy_int_pol, phy_pol_mask, HDMI_PHY_POL0);
1806
1807 /*
1808 * RX sense tells us whether the TDMS transmitters are detecting
1809 * load - in other words, there's something listening on the
1810 * other end of the link. Use this to decide whether we should
1811 * power on the phy as HPD may be toggled by the sink to merely
1812 * ask the source to re-read the EDID.
1813 */
1814 if (intr_stat &
1815 (HDMI_IH_PHY_STAT0_RX_SENSE | HDMI_IH_PHY_STAT0_HPD)) {
Russell Kingb872a8e2015-06-05 12:22:46 +01001816 mutex_lock(&hdmi->mutex);
Russell Kingaeac23b2015-06-05 13:46:22 +01001817 if (!hdmi->disabled && !hdmi->force) {
1818 /*
1819 * If the RX sense status indicates we're disconnected,
1820 * clear the software rxsense status.
1821 */
1822 if (!(phy_stat & HDMI_PHY_RX_SENSE))
1823 hdmi->rxsense = false;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001824
Russell Kingaeac23b2015-06-05 13:46:22 +01001825 /*
1826 * Only set the software rxsense status when both
1827 * rxsense and hpd indicates we're connected.
1828 * This avoids what seems to be bad behaviour in
1829 * at least iMX6S versions of the phy.
1830 */
1831 if (phy_stat & HDMI_PHY_HPD)
1832 hdmi->rxsense = true;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001833
Russell Kingaeac23b2015-06-05 13:46:22 +01001834 dw_hdmi_update_power(hdmi);
1835 dw_hdmi_update_phy_mask(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001836 }
Russell Kingb872a8e2015-06-05 12:22:46 +01001837 mutex_unlock(&hdmi->mutex);
Russell Kingaeac23b2015-06-05 13:46:22 +01001838 }
1839
1840 if (intr_stat & HDMI_IH_PHY_STAT0_HPD) {
1841 dev_dbg(hdmi->dev, "EVENT=%s\n",
1842 phy_int_pol & HDMI_PHY_HPD ? "plugin" : "plugout");
Laurent Pinchartba5d7e62017-01-17 10:28:56 +02001843 if (hdmi->bridge.dev)
1844 drm_helper_hpd_irq_event(hdmi->bridge.dev);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001845 }
1846
1847 hdmi_writeb(hdmi, intr_stat, HDMI_IH_PHY_STAT0);
Russell Kingaeac23b2015-06-05 13:46:22 +01001848 hdmi_writeb(hdmi, ~(HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE),
1849 HDMI_IH_MUTE_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001850
1851 return IRQ_HANDLED;
1852}
1853
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02001854static const struct dw_hdmi_phy_data dw_hdmi_phys[] = {
1855 {
1856 .type = DW_HDMI_PHY_DWC_HDMI_TX_PHY,
1857 .name = "DWC HDMI TX PHY",
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02001858 .gen = 1,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02001859 }, {
1860 .type = DW_HDMI_PHY_DWC_MHL_PHY_HEAC,
1861 .name = "DWC MHL PHY + HEAC PHY",
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02001862 .gen = 2,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02001863 .has_svsret = true,
1864 }, {
1865 .type = DW_HDMI_PHY_DWC_MHL_PHY,
1866 .name = "DWC MHL PHY",
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02001867 .gen = 2,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02001868 .has_svsret = true,
1869 }, {
1870 .type = DW_HDMI_PHY_DWC_HDMI_3D_TX_PHY_HEAC,
1871 .name = "DWC HDMI 3D TX PHY + HEAC PHY",
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02001872 .gen = 2,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02001873 }, {
1874 .type = DW_HDMI_PHY_DWC_HDMI_3D_TX_PHY,
1875 .name = "DWC HDMI 3D TX PHY",
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02001876 .gen = 2,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02001877 }, {
1878 .type = DW_HDMI_PHY_DWC_HDMI20_TX_PHY,
1879 .name = "DWC HDMI 2.0 TX PHY",
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02001880 .gen = 2,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02001881 .has_svsret = true,
1882 }
1883};
1884
1885static int dw_hdmi_detect_phy(struct dw_hdmi *hdmi)
1886{
1887 unsigned int i;
1888 u8 phy_type;
1889
1890 phy_type = hdmi_readb(hdmi, HDMI_CONFIG2_ID);
1891
1892 for (i = 0; i < ARRAY_SIZE(dw_hdmi_phys); ++i) {
1893 if (dw_hdmi_phys[i].type == phy_type) {
1894 hdmi->phy = &dw_hdmi_phys[i];
1895 return 0;
1896 }
1897 }
1898
1899 if (phy_type == DW_HDMI_PHY_VENDOR_PHY)
1900 dev_err(hdmi->dev, "Unsupported vendor HDMI PHY\n");
1901 else
1902 dev_err(hdmi->dev, "Unsupported HDMI PHY type (%02x)\n",
1903 phy_type);
1904
1905 return -ENODEV;
1906}
1907
Laurent Pinchart69497eb2017-01-17 10:29:00 +02001908static struct dw_hdmi *
1909__dw_hdmi_probe(struct platform_device *pdev,
1910 const struct dw_hdmi_plat_data *plat_data)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001911{
Laurent Pinchartc6081192017-01-17 10:28:57 +02001912 struct device *dev = &pdev->dev;
Russell King17b50012013-11-03 11:23:34 +00001913 struct device_node *np = dev->of_node;
Russell King7ed6c662013-11-07 16:01:45 +00001914 struct platform_device_info pdevinfo;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001915 struct device_node *ddc_node;
Andy Yanb21f4b62014-12-05 14:26:31 +08001916 struct dw_hdmi *hdmi;
Laurent Pinchartc6081192017-01-17 10:28:57 +02001917 struct resource *iores;
1918 int irq;
Andy Yan3d1b35a2014-12-05 14:25:05 +08001919 int ret;
Andy Yan0cd9d142014-12-05 14:28:24 +08001920 u32 val = 1;
Laurent Pinchart0527e122017-01-17 10:29:03 +02001921 u8 prod_id0;
1922 u8 prod_id1;
Kuninori Morimoto2761ba62016-11-08 01:00:57 +00001923 u8 config0;
Laurent Pinchart0c674942017-01-17 10:29:04 +02001924 u8 config3;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001925
Russell King17b50012013-11-03 11:23:34 +00001926 hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001927 if (!hdmi)
Laurent Pinchart69497eb2017-01-17 10:29:00 +02001928 return ERR_PTR(-ENOMEM);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001929
Andy Yan3d1b35a2014-12-05 14:25:05 +08001930 hdmi->plat_data = plat_data;
Russell King17b50012013-11-03 11:23:34 +00001931 hdmi->dev = dev;
Andy Yan3d1b35a2014-12-05 14:25:05 +08001932 hdmi->dev_type = plat_data->dev_type;
Russell King40678382013-11-07 15:35:06 +00001933 hdmi->sample_rate = 48000;
Russell Kingb872a8e2015-06-05 12:22:46 +01001934 hdmi->disabled = true;
Russell Kingaeac23b2015-06-05 13:46:22 +01001935 hdmi->rxsense = true;
1936 hdmi->phy_mask = (u8)~(HDMI_PHY_HPD | HDMI_PHY_RX_SENSE);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001937
Russell Kingb872a8e2015-06-05 12:22:46 +01001938 mutex_init(&hdmi->mutex);
Russell King6bcf4952015-02-02 11:01:08 +00001939 mutex_init(&hdmi->audio_mutex);
Russell Kingb90120a2015-03-27 12:59:58 +00001940 spin_lock_init(&hdmi->audio_lock);
Russell King6bcf4952015-02-02 11:01:08 +00001941
Andy Yan0cd9d142014-12-05 14:28:24 +08001942 of_property_read_u32(np, "reg-io-width", &val);
1943
1944 switch (val) {
1945 case 4:
1946 hdmi->write = dw_hdmi_writel;
1947 hdmi->read = dw_hdmi_readl;
1948 break;
1949 case 1:
1950 hdmi->write = dw_hdmi_writeb;
1951 hdmi->read = dw_hdmi_readb;
1952 break;
1953 default:
1954 dev_err(dev, "reg-io-width must be 1 or 4\n");
Laurent Pinchart69497eb2017-01-17 10:29:00 +02001955 return ERR_PTR(-EINVAL);
Andy Yan0cd9d142014-12-05 14:28:24 +08001956 }
1957
Philipp Zabelb5d45902014-03-05 10:20:56 +01001958 ddc_node = of_parse_phandle(np, "ddc-i2c-bus", 0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001959 if (ddc_node) {
Vladimir Zapolskiy9f04a1f2016-08-16 23:26:43 +03001960 hdmi->ddc = of_get_i2c_adapter_by_node(ddc_node);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001961 of_node_put(ddc_node);
Andy Yanc2c38482014-12-05 14:24:28 +08001962 if (!hdmi->ddc) {
1963 dev_dbg(hdmi->dev, "failed to read ddc node\n");
Laurent Pinchart69497eb2017-01-17 10:29:00 +02001964 return ERR_PTR(-EPROBE_DEFER);
Andy Yanc2c38482014-12-05 14:24:28 +08001965 }
1966
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001967 } else {
1968 dev_dbg(hdmi->dev, "no ddc property found\n");
1969 }
1970
Laurent Pinchartc6081192017-01-17 10:28:57 +02001971 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Russell King17b50012013-11-03 11:23:34 +00001972 hdmi->regs = devm_ioremap_resource(dev, iores);
Vladimir Zapolskiy9f04a1f2016-08-16 23:26:43 +03001973 if (IS_ERR(hdmi->regs)) {
1974 ret = PTR_ERR(hdmi->regs);
1975 goto err_res;
1976 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001977
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001978 hdmi->isfr_clk = devm_clk_get(hdmi->dev, "isfr");
1979 if (IS_ERR(hdmi->isfr_clk)) {
1980 ret = PTR_ERR(hdmi->isfr_clk);
Andy Yanb5878332014-12-05 14:23:52 +08001981 dev_err(hdmi->dev, "Unable to get HDMI isfr clk: %d\n", ret);
Vladimir Zapolskiy9f04a1f2016-08-16 23:26:43 +03001982 goto err_res;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001983 }
1984
1985 ret = clk_prepare_enable(hdmi->isfr_clk);
1986 if (ret) {
Andy Yanb5878332014-12-05 14:23:52 +08001987 dev_err(hdmi->dev, "Cannot enable HDMI isfr clock: %d\n", ret);
Vladimir Zapolskiy9f04a1f2016-08-16 23:26:43 +03001988 goto err_res;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001989 }
1990
1991 hdmi->iahb_clk = devm_clk_get(hdmi->dev, "iahb");
1992 if (IS_ERR(hdmi->iahb_clk)) {
1993 ret = PTR_ERR(hdmi->iahb_clk);
Andy Yanb5878332014-12-05 14:23:52 +08001994 dev_err(hdmi->dev, "Unable to get HDMI iahb clk: %d\n", ret);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001995 goto err_isfr;
1996 }
1997
1998 ret = clk_prepare_enable(hdmi->iahb_clk);
1999 if (ret) {
Andy Yanb5878332014-12-05 14:23:52 +08002000 dev_err(hdmi->dev, "Cannot enable HDMI iahb clock: %d\n", ret);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002001 goto err_isfr;
2002 }
2003
2004 /* Product and revision IDs */
Laurent Pinchartbe41fc52017-01-17 10:29:05 +02002005 hdmi->version = (hdmi_readb(hdmi, HDMI_DESIGN_ID) << 8)
2006 | (hdmi_readb(hdmi, HDMI_REVISION_ID) << 0);
Laurent Pinchart0527e122017-01-17 10:29:03 +02002007 prod_id0 = hdmi_readb(hdmi, HDMI_PRODUCT_ID0);
2008 prod_id1 = hdmi_readb(hdmi, HDMI_PRODUCT_ID1);
2009
2010 if (prod_id0 != HDMI_PRODUCT_ID0_HDMI_TX ||
2011 (prod_id1 & ~HDMI_PRODUCT_ID1_HDCP) != HDMI_PRODUCT_ID1_HDMI_TX) {
2012 dev_err(dev, "Unsupported HDMI controller (%04x:%02x:%02x)\n",
Laurent Pinchartbe41fc52017-01-17 10:29:05 +02002013 hdmi->version, prod_id0, prod_id1);
Laurent Pinchart0527e122017-01-17 10:29:03 +02002014 ret = -ENODEV;
2015 goto err_iahb;
2016 }
2017
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002018 ret = dw_hdmi_detect_phy(hdmi);
2019 if (ret < 0)
2020 goto err_iahb;
2021
2022 dev_info(dev, "Detected HDMI TX controller v%x.%03x %s HDCP (%s)\n",
Laurent Pinchartbe41fc52017-01-17 10:29:05 +02002023 hdmi->version >> 12, hdmi->version & 0xfff,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002024 prod_id1 & HDMI_PRODUCT_ID1_HDCP ? "with" : "without",
2025 hdmi->phy->name);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002026
2027 initialize_hdmi_ih_mutes(hdmi);
2028
Laurent Pinchartc6081192017-01-17 10:28:57 +02002029 irq = platform_get_irq(pdev, 0);
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002030 if (irq < 0) {
2031 ret = irq;
Laurent Pinchartc6081192017-01-17 10:28:57 +02002032 goto err_iahb;
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002033 }
Laurent Pinchartc6081192017-01-17 10:28:57 +02002034
Philipp Zabel639a2022015-01-07 13:43:50 +01002035 ret = devm_request_threaded_irq(dev, irq, dw_hdmi_hardirq,
2036 dw_hdmi_irq, IRQF_SHARED,
2037 dev_name(dev), hdmi);
2038 if (ret)
Fabio Estevamb33ef612015-01-27 10:54:12 -02002039 goto err_iahb;
Philipp Zabel639a2022015-01-07 13:43:50 +01002040
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002041 /*
2042 * To prevent overflows in HDMI_IH_FC_STAT2, set the clk regenerator
2043 * N and cts values before enabling phy
2044 */
2045 hdmi_init_clk_regenerator(hdmi);
2046
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002047 /* If DDC bus is not specified, try to register HDMI I2C bus */
2048 if (!hdmi->ddc) {
2049 hdmi->ddc = dw_hdmi_i2c_adapter(hdmi);
2050 if (IS_ERR(hdmi->ddc))
2051 hdmi->ddc = NULL;
2052 }
2053
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002054 /*
2055 * Configure registers related to HDMI interrupt
2056 * generation before registering IRQ.
2057 */
Russell Kingaeac23b2015-06-05 13:46:22 +01002058 hdmi_writeb(hdmi, HDMI_PHY_HPD | HDMI_PHY_RX_SENSE, HDMI_PHY_POL0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002059
2060 /* Clear Hotplug interrupts */
Russell Kingaeac23b2015-06-05 13:46:22 +01002061 hdmi_writeb(hdmi, HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE,
2062 HDMI_IH_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002063
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002064 hdmi->bridge.driver_private = hdmi;
2065 hdmi->bridge.funcs = &dw_hdmi_bridge_funcs;
Arnd Bergmannd5ad7842017-01-23 13:20:38 +01002066#ifdef CONFIG_OF
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002067 hdmi->bridge.of_node = pdev->dev.of_node;
Arnd Bergmannd5ad7842017-01-23 13:20:38 +01002068#endif
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002069
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002070 ret = dw_hdmi_fb_registered(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002071 if (ret)
2072 goto err_iahb;
2073
Russell Kingd94905e2013-11-03 22:23:24 +00002074 /* Unmute interrupts */
Russell Kingaeac23b2015-06-05 13:46:22 +01002075 hdmi_writeb(hdmi, ~(HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE),
2076 HDMI_IH_MUTE_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002077
Russell King7ed6c662013-11-07 16:01:45 +00002078 memset(&pdevinfo, 0, sizeof(pdevinfo));
2079 pdevinfo.parent = dev;
2080 pdevinfo.id = PLATFORM_DEVID_AUTO;
2081
Kuninori Morimoto2761ba62016-11-08 01:00:57 +00002082 config0 = hdmi_readb(hdmi, HDMI_CONFIG0_ID);
Laurent Pinchart0c674942017-01-17 10:29:04 +02002083 config3 = hdmi_readb(hdmi, HDMI_CONFIG3_ID);
Kuninori Morimoto2761ba62016-11-08 01:00:57 +00002084
Laurent Pinchart0c674942017-01-17 10:29:04 +02002085 if (config3 & HDMI_CONFIG3_AHBAUDDMA) {
Kuninori Morimoto2761ba62016-11-08 01:00:57 +00002086 struct dw_hdmi_audio_data audio;
2087
Russell King7ed6c662013-11-07 16:01:45 +00002088 audio.phys = iores->start;
2089 audio.base = hdmi->regs;
2090 audio.irq = irq;
2091 audio.hdmi = hdmi;
Russell Kingf5ce4052013-11-07 16:06:01 +00002092 audio.eld = hdmi->connector.eld;
Russell King7ed6c662013-11-07 16:01:45 +00002093
2094 pdevinfo.name = "dw-hdmi-ahb-audio";
2095 pdevinfo.data = &audio;
2096 pdevinfo.size_data = sizeof(audio);
2097 pdevinfo.dma_mask = DMA_BIT_MASK(32);
2098 hdmi->audio = platform_device_register_full(&pdevinfo);
Kuninori Morimoto2761ba62016-11-08 01:00:57 +00002099 } else if (config0 & HDMI_CONFIG0_I2S) {
2100 struct dw_hdmi_i2s_audio_data audio;
2101
2102 audio.hdmi = hdmi;
2103 audio.write = hdmi_writeb;
2104 audio.read = hdmi_readb;
2105
2106 pdevinfo.name = "dw-hdmi-i2s-audio";
2107 pdevinfo.data = &audio;
2108 pdevinfo.size_data = sizeof(audio);
2109 pdevinfo.dma_mask = DMA_BIT_MASK(32);
2110 hdmi->audio = platform_device_register_full(&pdevinfo);
Russell King7ed6c662013-11-07 16:01:45 +00002111 }
2112
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002113 /* Reset HDMI DDC I2C master controller and mute I2CM interrupts */
2114 if (hdmi->i2c)
2115 dw_hdmi_i2c_init(hdmi);
2116
Laurent Pinchartc6081192017-01-17 10:28:57 +02002117 platform_set_drvdata(pdev, hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002118
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002119 return hdmi;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002120
2121err_iahb:
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002122 if (hdmi->i2c) {
2123 i2c_del_adapter(&hdmi->i2c->adap);
2124 hdmi->ddc = NULL;
2125 }
2126
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002127 clk_disable_unprepare(hdmi->iahb_clk);
2128err_isfr:
2129 clk_disable_unprepare(hdmi->isfr_clk);
Vladimir Zapolskiy9f04a1f2016-08-16 23:26:43 +03002130err_res:
2131 i2c_put_adapter(hdmi->ddc);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002132
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002133 return ERR_PTR(ret);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002134}
2135
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002136static void __dw_hdmi_remove(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002137{
Russell King7ed6c662013-11-07 16:01:45 +00002138 if (hdmi->audio && !IS_ERR(hdmi->audio))
2139 platform_device_unregister(hdmi->audio);
2140
Russell Kingd94905e2013-11-03 22:23:24 +00002141 /* Disable all interrupts */
2142 hdmi_writeb(hdmi, ~0, HDMI_IH_MUTE_PHY_STAT0);
2143
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002144 clk_disable_unprepare(hdmi->iahb_clk);
2145 clk_disable_unprepare(hdmi->isfr_clk);
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002146
2147 if (hdmi->i2c)
2148 i2c_del_adapter(&hdmi->i2c->adap);
2149 else
2150 i2c_put_adapter(hdmi->ddc);
Russell King17b50012013-11-03 11:23:34 +00002151}
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002152
2153/* -----------------------------------------------------------------------------
2154 * Probe/remove API, used from platforms based on the DRM bridge API.
2155 */
2156int dw_hdmi_probe(struct platform_device *pdev,
2157 const struct dw_hdmi_plat_data *plat_data)
2158{
2159 struct dw_hdmi *hdmi;
2160 int ret;
2161
2162 hdmi = __dw_hdmi_probe(pdev, plat_data);
2163 if (IS_ERR(hdmi))
2164 return PTR_ERR(hdmi);
2165
2166 ret = drm_bridge_add(&hdmi->bridge);
2167 if (ret < 0) {
2168 __dw_hdmi_remove(hdmi);
2169 return ret;
2170 }
2171
2172 return 0;
2173}
2174EXPORT_SYMBOL_GPL(dw_hdmi_probe);
2175
2176void dw_hdmi_remove(struct platform_device *pdev)
2177{
2178 struct dw_hdmi *hdmi = platform_get_drvdata(pdev);
2179
2180 drm_bridge_remove(&hdmi->bridge);
2181
2182 __dw_hdmi_remove(hdmi);
2183}
2184EXPORT_SYMBOL_GPL(dw_hdmi_remove);
2185
2186/* -----------------------------------------------------------------------------
2187 * Bind/unbind API, used from platforms based on the component framework.
2188 */
2189int dw_hdmi_bind(struct platform_device *pdev, struct drm_encoder *encoder,
2190 const struct dw_hdmi_plat_data *plat_data)
2191{
2192 struct dw_hdmi *hdmi;
2193 int ret;
2194
2195 hdmi = __dw_hdmi_probe(pdev, plat_data);
2196 if (IS_ERR(hdmi))
2197 return PTR_ERR(hdmi);
2198
2199 ret = drm_bridge_attach(encoder, &hdmi->bridge, NULL);
2200 if (ret) {
2201 dw_hdmi_remove(pdev);
2202 DRM_ERROR("Failed to initialize bridge with drm\n");
2203 return ret;
2204 }
2205
2206 return 0;
2207}
2208EXPORT_SYMBOL_GPL(dw_hdmi_bind);
2209
2210void dw_hdmi_unbind(struct device *dev)
2211{
2212 struct dw_hdmi *hdmi = dev_get_drvdata(dev);
2213
2214 __dw_hdmi_remove(hdmi);
2215}
Andy Yanb21f4b62014-12-05 14:26:31 +08002216EXPORT_SYMBOL_GPL(dw_hdmi_unbind);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002217
2218MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
Andy Yan3d1b35a2014-12-05 14:25:05 +08002219MODULE_AUTHOR("Andy Yan <andy.yan@rock-chips.com>");
2220MODULE_AUTHOR("Yakir Yang <ykk@rock-chips.com>");
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002221MODULE_AUTHOR("Vladimir Zapolskiy <vladimir_zapolskiy@mentor.com>");
Andy Yanb21f4b62014-12-05 14:26:31 +08002222MODULE_DESCRIPTION("DW HDMI transmitter driver");
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002223MODULE_LICENSE("GPL");
Andy Yanb21f4b62014-12-05 14:26:31 +08002224MODULE_ALIAS("platform:dw-hdmi");