blob: 026a0dce766182577651ef49650ab68cafd86c00 [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>
Neil Armstrong80e2f972017-03-03 19:20:06 +020022#include <linux/regmap.h>
Russell Kingb90120a2015-03-27 12:59:58 +000023#include <linux/spinlock.h>
Fabio Estevam9aaf8802013-11-29 08:46:32 -020024
Andy Yan3d1b35a2014-12-05 14:25:05 +080025#include <drm/drm_of.h>
Fabio Estevam9aaf8802013-11-29 08:46:32 -020026#include <drm/drmP.h>
Mark Yao2c5b2cc2015-11-30 18:33:40 +080027#include <drm/drm_atomic_helper.h>
Fabio Estevam9aaf8802013-11-29 08:46:32 -020028#include <drm/drm_crtc_helper.h>
29#include <drm/drm_edid.h>
30#include <drm/drm_encoder_slave.h>
Andy Yanb21f4b62014-12-05 14:26:31 +080031#include <drm/bridge/dw_hdmi.h>
Fabio Estevam9aaf8802013-11-29 08:46:32 -020032
Thierry Reding248a86f2015-11-24 17:52:58 +010033#include "dw-hdmi.h"
34#include "dw-hdmi-audio.h"
Fabio Estevam9aaf8802013-11-29 08:46:32 -020035
36#define HDMI_EDID_LEN 512
37
38#define RGB 0
39#define YCBCR444 1
40#define YCBCR422_16BITS 2
41#define YCBCR422_8BITS 3
42#define XVYCC444 4
43
44enum hdmi_datamap {
45 RGB444_8B = 0x01,
46 RGB444_10B = 0x03,
47 RGB444_12B = 0x05,
48 RGB444_16B = 0x07,
49 YCbCr444_8B = 0x09,
50 YCbCr444_10B = 0x0B,
51 YCbCr444_12B = 0x0D,
52 YCbCr444_16B = 0x0F,
53 YCbCr422_8B = 0x16,
54 YCbCr422_10B = 0x14,
55 YCbCr422_12B = 0x12,
56};
57
Fabio Estevam9aaf8802013-11-29 08:46:32 -020058static const u16 csc_coeff_default[3][4] = {
59 { 0x2000, 0x0000, 0x0000, 0x0000 },
60 { 0x0000, 0x2000, 0x0000, 0x0000 },
61 { 0x0000, 0x0000, 0x2000, 0x0000 }
62};
63
64static const u16 csc_coeff_rgb_out_eitu601[3][4] = {
65 { 0x2000, 0x6926, 0x74fd, 0x010e },
66 { 0x2000, 0x2cdd, 0x0000, 0x7e9a },
67 { 0x2000, 0x0000, 0x38b4, 0x7e3b }
68};
69
70static const u16 csc_coeff_rgb_out_eitu709[3][4] = {
71 { 0x2000, 0x7106, 0x7a02, 0x00a7 },
72 { 0x2000, 0x3264, 0x0000, 0x7e6d },
73 { 0x2000, 0x0000, 0x3b61, 0x7e25 }
74};
75
76static const u16 csc_coeff_rgb_in_eitu601[3][4] = {
77 { 0x2591, 0x1322, 0x074b, 0x0000 },
78 { 0x6535, 0x2000, 0x7acc, 0x0200 },
79 { 0x6acd, 0x7534, 0x2000, 0x0200 }
80};
81
82static const u16 csc_coeff_rgb_in_eitu709[3][4] = {
83 { 0x2dc5, 0x0d9b, 0x049e, 0x0000 },
84 { 0x62f0, 0x2000, 0x7d11, 0x0200 },
85 { 0x6756, 0x78ab, 0x2000, 0x0200 }
86};
87
88struct hdmi_vmode {
Fabio Estevam9aaf8802013-11-29 08:46:32 -020089 bool mdataenablepolarity;
90
91 unsigned int mpixelclock;
92 unsigned int mpixelrepetitioninput;
93 unsigned int mpixelrepetitionoutput;
94};
95
96struct hdmi_data_info {
97 unsigned int enc_in_format;
98 unsigned int enc_out_format;
99 unsigned int enc_color_depth;
100 unsigned int colorimetry;
101 unsigned int pix_repet_factor;
102 unsigned int hdcp_enable;
103 struct hdmi_vmode video_mode;
104};
105
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +0300106struct dw_hdmi_i2c {
107 struct i2c_adapter adap;
108
109 struct mutex lock; /* used to serialize data transfers */
110 struct completion cmp;
111 u8 stat;
112
113 u8 slave_reg;
114 bool is_regaddr;
115};
116
Laurent Pinchartfaba6c32017-01-17 10:29:06 +0200117struct dw_hdmi_phy_data {
118 enum dw_hdmi_phy_type type;
119 const char *name;
Laurent Pinchartb0e583e2017-03-06 01:35:39 +0200120 unsigned int gen;
Laurent Pinchartfaba6c32017-01-17 10:29:06 +0200121 bool has_svsret;
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +0200122 int (*configure)(struct dw_hdmi *hdmi,
123 const struct dw_hdmi_plat_data *pdata,
124 unsigned long mpixelclock);
Laurent Pinchartfaba6c32017-01-17 10:29:06 +0200125};
126
Andy Yanb21f4b62014-12-05 14:26:31 +0800127struct dw_hdmi {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200128 struct drm_connector connector;
Laurent Pinchart70c963e2017-01-17 10:28:54 +0200129 struct drm_bridge bridge;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200130
Laurent Pinchartbe41fc52017-01-17 10:29:05 +0200131 unsigned int version;
132
133 struct platform_device *audio;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200134 struct device *dev;
135 struct clk *isfr_clk;
136 struct clk *iahb_clk;
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +0300137 struct dw_hdmi_i2c *i2c;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200138
139 struct hdmi_data_info hdmi_data;
Andy Yanb21f4b62014-12-05 14:26:31 +0800140 const struct dw_hdmi_plat_data *plat_data;
141
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200142 int vic;
143
144 u8 edid[HDMI_EDID_LEN];
145 bool cable_plugin;
146
Laurent Pinchartf1585f62017-03-06 01:36:15 +0200147 struct {
148 const struct dw_hdmi_phy_ops *ops;
149 const char *name;
150 void *data;
151 bool enabled;
152 } phy;
Laurent Pinchartfaba6c32017-01-17 10:29:06 +0200153
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200154 struct drm_display_mode previous_mode;
155
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200156 struct i2c_adapter *ddc;
157 void __iomem *regs;
Russell King05b13422015-07-21 15:35:52 +0100158 bool sink_is_hdmi;
Russell Kingf709ec02015-07-21 16:09:39 +0100159 bool sink_has_audio;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200160
Russell Kingb872a8e2015-06-05 12:22:46 +0100161 struct mutex mutex; /* for state below and previous_mode */
Russell King381f05a2015-06-05 15:25:08 +0100162 enum drm_connector_force force; /* mutex-protected force state */
Russell Kingb872a8e2015-06-05 12:22:46 +0100163 bool disabled; /* DRM has disabled our bridge */
Russell King381f05a2015-06-05 15:25:08 +0100164 bool bridge_is_on; /* indicates the bridge is on */
Russell Kingaeac23b2015-06-05 13:46:22 +0100165 bool rxsense; /* rxsense state */
166 u8 phy_mask; /* desired phy int mask settings */
Russell Kingb872a8e2015-06-05 12:22:46 +0100167
Russell Kingb90120a2015-03-27 12:59:58 +0000168 spinlock_t audio_lock;
Russell King6bcf4952015-02-02 11:01:08 +0000169 struct mutex audio_mutex;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200170 unsigned int sample_rate;
Russell Kingb90120a2015-03-27 12:59:58 +0000171 unsigned int audio_cts;
172 unsigned int audio_n;
173 bool audio_enable;
Andy Yan0cd9d142014-12-05 14:28:24 +0800174
Neil Armstrong80e2f972017-03-03 19:20:06 +0200175 unsigned int reg_shift;
176 struct regmap *regm;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200177};
178
Russell Kingaeac23b2015-06-05 13:46:22 +0100179#define HDMI_IH_PHY_STAT0_RX_SENSE \
180 (HDMI_IH_PHY_STAT0_RX_SENSE0 | HDMI_IH_PHY_STAT0_RX_SENSE1 | \
181 HDMI_IH_PHY_STAT0_RX_SENSE2 | HDMI_IH_PHY_STAT0_RX_SENSE3)
182
183#define HDMI_PHY_RX_SENSE \
184 (HDMI_PHY_RX_SENSE0 | HDMI_PHY_RX_SENSE1 | \
185 HDMI_PHY_RX_SENSE2 | HDMI_PHY_RX_SENSE3)
186
Andy Yan0cd9d142014-12-05 14:28:24 +0800187static inline void hdmi_writeb(struct dw_hdmi *hdmi, u8 val, int offset)
188{
Neil Armstrong80e2f972017-03-03 19:20:06 +0200189 regmap_write(hdmi->regm, offset << hdmi->reg_shift, val);
Andy Yan0cd9d142014-12-05 14:28:24 +0800190}
191
192static inline u8 hdmi_readb(struct dw_hdmi *hdmi, int offset)
193{
Neil Armstrong80e2f972017-03-03 19:20:06 +0200194 unsigned int val = 0;
195
196 regmap_read(hdmi->regm, offset << hdmi->reg_shift, &val);
197
198 return val;
Andy Yan0cd9d142014-12-05 14:28:24 +0800199}
200
Andy Yanb21f4b62014-12-05 14:26:31 +0800201static void hdmi_modb(struct dw_hdmi *hdmi, u8 data, u8 mask, unsigned reg)
Russell King812bc612013-11-04 12:42:02 +0000202{
Neil Armstrong80e2f972017-03-03 19:20:06 +0200203 regmap_update_bits(hdmi->regm, reg << hdmi->reg_shift, mask, data);
Russell King812bc612013-11-04 12:42:02 +0000204}
205
Andy Yanb21f4b62014-12-05 14:26:31 +0800206static void hdmi_mask_writeb(struct dw_hdmi *hdmi, u8 data, unsigned int reg,
Andy Yanb5878332014-12-05 14:23:52 +0800207 u8 shift, u8 mask)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200208{
Russell King812bc612013-11-04 12:42:02 +0000209 hdmi_modb(hdmi, data << shift, mask, reg);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200210}
211
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +0300212static void dw_hdmi_i2c_init(struct dw_hdmi *hdmi)
213{
214 /* Software reset */
215 hdmi_writeb(hdmi, 0x00, HDMI_I2CM_SOFTRSTZ);
216
217 /* Set Standard Mode speed (determined to be 100KHz on iMX6) */
218 hdmi_writeb(hdmi, 0x00, HDMI_I2CM_DIV);
219
220 /* Set done, not acknowledged and arbitration interrupt polarities */
221 hdmi_writeb(hdmi, HDMI_I2CM_INT_DONE_POL, HDMI_I2CM_INT);
222 hdmi_writeb(hdmi, HDMI_I2CM_CTLINT_NAC_POL | HDMI_I2CM_CTLINT_ARB_POL,
223 HDMI_I2CM_CTLINT);
224
225 /* Clear DONE and ERROR interrupts */
226 hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,
227 HDMI_IH_I2CM_STAT0);
228
229 /* Mute DONE and ERROR interrupts */
230 hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,
231 HDMI_IH_MUTE_I2CM_STAT0);
232}
233
234static int dw_hdmi_i2c_read(struct dw_hdmi *hdmi,
235 unsigned char *buf, unsigned int length)
236{
237 struct dw_hdmi_i2c *i2c = hdmi->i2c;
238 int stat;
239
240 if (!i2c->is_regaddr) {
241 dev_dbg(hdmi->dev, "set read register address to 0\n");
242 i2c->slave_reg = 0x00;
243 i2c->is_regaddr = true;
244 }
245
246 while (length--) {
247 reinit_completion(&i2c->cmp);
248
249 hdmi_writeb(hdmi, i2c->slave_reg++, HDMI_I2CM_ADDRESS);
250 hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_READ,
251 HDMI_I2CM_OPERATION);
252
253 stat = wait_for_completion_timeout(&i2c->cmp, HZ / 10);
254 if (!stat)
255 return -EAGAIN;
256
257 /* Check for error condition on the bus */
258 if (i2c->stat & HDMI_IH_I2CM_STAT0_ERROR)
259 return -EIO;
260
261 *buf++ = hdmi_readb(hdmi, HDMI_I2CM_DATAI);
262 }
263
264 return 0;
265}
266
267static int dw_hdmi_i2c_write(struct dw_hdmi *hdmi,
268 unsigned char *buf, unsigned int length)
269{
270 struct dw_hdmi_i2c *i2c = hdmi->i2c;
271 int stat;
272
273 if (!i2c->is_regaddr) {
274 /* Use the first write byte as register address */
275 i2c->slave_reg = buf[0];
276 length--;
277 buf++;
278 i2c->is_regaddr = true;
279 }
280
281 while (length--) {
282 reinit_completion(&i2c->cmp);
283
284 hdmi_writeb(hdmi, *buf++, HDMI_I2CM_DATAO);
285 hdmi_writeb(hdmi, i2c->slave_reg++, HDMI_I2CM_ADDRESS);
286 hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_WRITE,
287 HDMI_I2CM_OPERATION);
288
289 stat = wait_for_completion_timeout(&i2c->cmp, HZ / 10);
290 if (!stat)
291 return -EAGAIN;
292
293 /* Check for error condition on the bus */
294 if (i2c->stat & HDMI_IH_I2CM_STAT0_ERROR)
295 return -EIO;
296 }
297
298 return 0;
299}
300
301static int dw_hdmi_i2c_xfer(struct i2c_adapter *adap,
302 struct i2c_msg *msgs, int num)
303{
304 struct dw_hdmi *hdmi = i2c_get_adapdata(adap);
305 struct dw_hdmi_i2c *i2c = hdmi->i2c;
306 u8 addr = msgs[0].addr;
307 int i, ret = 0;
308
309 dev_dbg(hdmi->dev, "xfer: num: %d, addr: %#x\n", num, addr);
310
311 for (i = 0; i < num; i++) {
312 if (msgs[i].addr != addr) {
313 dev_warn(hdmi->dev,
314 "unsupported transfer, changed slave address\n");
315 return -EOPNOTSUPP;
316 }
317
318 if (msgs[i].len == 0) {
319 dev_dbg(hdmi->dev,
320 "unsupported transfer %d/%d, no data\n",
321 i + 1, num);
322 return -EOPNOTSUPP;
323 }
324 }
325
326 mutex_lock(&i2c->lock);
327
328 /* Unmute DONE and ERROR interrupts */
329 hdmi_writeb(hdmi, 0x00, HDMI_IH_MUTE_I2CM_STAT0);
330
331 /* Set slave device address taken from the first I2C message */
332 hdmi_writeb(hdmi, addr, HDMI_I2CM_SLAVE);
333
334 /* Set slave device register address on transfer */
335 i2c->is_regaddr = false;
336
337 for (i = 0; i < num; i++) {
338 dev_dbg(hdmi->dev, "xfer: num: %d/%d, len: %d, flags: %#x\n",
339 i + 1, num, msgs[i].len, msgs[i].flags);
340
341 if (msgs[i].flags & I2C_M_RD)
342 ret = dw_hdmi_i2c_read(hdmi, msgs[i].buf, msgs[i].len);
343 else
344 ret = dw_hdmi_i2c_write(hdmi, msgs[i].buf, msgs[i].len);
345
346 if (ret < 0)
347 break;
348 }
349
350 if (!ret)
351 ret = num;
352
353 /* Mute DONE and ERROR interrupts */
354 hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,
355 HDMI_IH_MUTE_I2CM_STAT0);
356
357 mutex_unlock(&i2c->lock);
358
359 return ret;
360}
361
362static u32 dw_hdmi_i2c_func(struct i2c_adapter *adapter)
363{
364 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
365}
366
367static const struct i2c_algorithm dw_hdmi_algorithm = {
368 .master_xfer = dw_hdmi_i2c_xfer,
369 .functionality = dw_hdmi_i2c_func,
370};
371
372static struct i2c_adapter *dw_hdmi_i2c_adapter(struct dw_hdmi *hdmi)
373{
374 struct i2c_adapter *adap;
375 struct dw_hdmi_i2c *i2c;
376 int ret;
377
378 i2c = devm_kzalloc(hdmi->dev, sizeof(*i2c), GFP_KERNEL);
379 if (!i2c)
380 return ERR_PTR(-ENOMEM);
381
382 mutex_init(&i2c->lock);
383 init_completion(&i2c->cmp);
384
385 adap = &i2c->adap;
386 adap->class = I2C_CLASS_DDC;
387 adap->owner = THIS_MODULE;
388 adap->dev.parent = hdmi->dev;
389 adap->algo = &dw_hdmi_algorithm;
390 strlcpy(adap->name, "DesignWare HDMI", sizeof(adap->name));
391 i2c_set_adapdata(adap, hdmi);
392
393 ret = i2c_add_adapter(adap);
394 if (ret) {
395 dev_warn(hdmi->dev, "cannot add %s I2C adapter\n", adap->name);
396 devm_kfree(hdmi->dev, i2c);
397 return ERR_PTR(ret);
398 }
399
400 hdmi->i2c = i2c;
401
402 dev_info(hdmi->dev, "registered %s I2C bus driver\n", adap->name);
403
404 return adap;
405}
406
Russell King351e1352015-01-31 14:50:23 +0000407static void hdmi_set_cts_n(struct dw_hdmi *hdmi, unsigned int cts,
408 unsigned int n)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200409{
Russell King622494a2015-02-02 10:55:38 +0000410 /* Must be set/cleared first */
411 hdmi_modb(hdmi, 0, HDMI_AUD_CTS3_CTS_MANUAL, HDMI_AUD_CTS3);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200412
413 /* nshift factor = 0 */
Russell King812bc612013-11-04 12:42:02 +0000414 hdmi_modb(hdmi, 0, HDMI_AUD_CTS3_N_SHIFT_MASK, HDMI_AUD_CTS3);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200415
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200416 hdmi_writeb(hdmi, ((cts >> 16) & HDMI_AUD_CTS3_AUDCTS19_16_MASK) |
417 HDMI_AUD_CTS3_CTS_MANUAL, HDMI_AUD_CTS3);
Russell King622494a2015-02-02 10:55:38 +0000418 hdmi_writeb(hdmi, (cts >> 8) & 0xff, HDMI_AUD_CTS2);
419 hdmi_writeb(hdmi, cts & 0xff, HDMI_AUD_CTS1);
420
421 hdmi_writeb(hdmi, (n >> 16) & 0x0f, HDMI_AUD_N3);
422 hdmi_writeb(hdmi, (n >> 8) & 0xff, HDMI_AUD_N2);
423 hdmi_writeb(hdmi, n & 0xff, HDMI_AUD_N1);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200424}
425
Russell Kingb195fbd2015-07-22 11:28:16 +0100426static unsigned int hdmi_compute_n(unsigned int freq, unsigned long pixel_clk)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200427{
428 unsigned int n = (128 * freq) / 1000;
Russell Kingd0c96d12015-07-22 10:35:41 +0100429 unsigned int mult = 1;
430
431 while (freq > 48000) {
432 mult *= 2;
433 freq /= 2;
434 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200435
436 switch (freq) {
437 case 32000:
Russell King426701d2015-07-22 10:39:27 +0100438 if (pixel_clk == 25175000)
Russell Kingb195fbd2015-07-22 11:28:16 +0100439 n = 4576;
Russell King426701d2015-07-22 10:39:27 +0100440 else if (pixel_clk == 27027000)
Russell Kingb195fbd2015-07-22 11:28:16 +0100441 n = 4096;
Russell King426701d2015-07-22 10:39:27 +0100442 else if (pixel_clk == 74176000 || pixel_clk == 148352000)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200443 n = 11648;
444 else
445 n = 4096;
Russell Kingd0c96d12015-07-22 10:35:41 +0100446 n *= mult;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200447 break;
448
449 case 44100:
Russell King426701d2015-07-22 10:39:27 +0100450 if (pixel_clk == 25175000)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200451 n = 7007;
Russell King426701d2015-07-22 10:39:27 +0100452 else if (pixel_clk == 74176000)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200453 n = 17836;
Russell King426701d2015-07-22 10:39:27 +0100454 else if (pixel_clk == 148352000)
Russell Kingb195fbd2015-07-22 11:28:16 +0100455 n = 8918;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200456 else
457 n = 6272;
Russell Kingd0c96d12015-07-22 10:35:41 +0100458 n *= mult;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200459 break;
460
461 case 48000:
Russell King426701d2015-07-22 10:39:27 +0100462 if (pixel_clk == 25175000)
Russell Kingb195fbd2015-07-22 11:28:16 +0100463 n = 6864;
Russell King426701d2015-07-22 10:39:27 +0100464 else if (pixel_clk == 27027000)
Russell Kingb195fbd2015-07-22 11:28:16 +0100465 n = 6144;
Russell King426701d2015-07-22 10:39:27 +0100466 else if (pixel_clk == 74176000)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200467 n = 11648;
Russell King426701d2015-07-22 10:39:27 +0100468 else if (pixel_clk == 148352000)
Russell Kingb195fbd2015-07-22 11:28:16 +0100469 n = 5824;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200470 else
471 n = 6144;
Russell Kingd0c96d12015-07-22 10:35:41 +0100472 n *= mult;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200473 break;
474
475 default:
476 break;
477 }
478
479 return n;
480}
481
Andy Yanb21f4b62014-12-05 14:26:31 +0800482static void hdmi_set_clk_regenerator(struct dw_hdmi *hdmi,
Russell Kingb195fbd2015-07-22 11:28:16 +0100483 unsigned long pixel_clk, unsigned int sample_rate)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200484{
Russell Kingdfbdaf52015-07-22 16:54:37 +0100485 unsigned long ftdms = pixel_clk;
Russell Kingf879b382015-03-27 12:53:29 +0000486 unsigned int n, cts;
Russell Kingdfbdaf52015-07-22 16:54:37 +0100487 u64 tmp;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200488
Russell Kingb195fbd2015-07-22 11:28:16 +0100489 n = hdmi_compute_n(sample_rate, pixel_clk);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200490
Russell Kingdfbdaf52015-07-22 16:54:37 +0100491 /*
492 * Compute the CTS value from the N value. Note that CTS and N
493 * can be up to 20 bits in total, so we need 64-bit math. Also
494 * note that our TDMS clock is not fully accurate; it is accurate
495 * to kHz. This can introduce an unnecessary remainder in the
496 * calculation below, so we don't try to warn about that.
497 */
498 tmp = (u64)ftdms * n;
499 do_div(tmp, 128 * sample_rate);
500 cts = tmp;
501
502 dev_dbg(hdmi->dev, "%s: fs=%uHz ftdms=%lu.%03luMHz N=%d cts=%d\n",
503 __func__, sample_rate, ftdms / 1000000, (ftdms / 1000) % 1000,
504 n, cts);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200505
Russell Kingb90120a2015-03-27 12:59:58 +0000506 spin_lock_irq(&hdmi->audio_lock);
507 hdmi->audio_n = n;
508 hdmi->audio_cts = cts;
509 hdmi_set_cts_n(hdmi, cts, hdmi->audio_enable ? n : 0);
510 spin_unlock_irq(&hdmi->audio_lock);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200511}
512
Andy Yanb21f4b62014-12-05 14:26:31 +0800513static void hdmi_init_clk_regenerator(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200514{
Russell King6bcf4952015-02-02 11:01:08 +0000515 mutex_lock(&hdmi->audio_mutex);
Russell Kingb195fbd2015-07-22 11:28:16 +0100516 hdmi_set_clk_regenerator(hdmi, 74250000, hdmi->sample_rate);
Russell King6bcf4952015-02-02 11:01:08 +0000517 mutex_unlock(&hdmi->audio_mutex);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200518}
519
Andy Yanb21f4b62014-12-05 14:26:31 +0800520static void hdmi_clk_regenerator_update_pixel_clock(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200521{
Russell King6bcf4952015-02-02 11:01:08 +0000522 mutex_lock(&hdmi->audio_mutex);
Russell Kingf879b382015-03-27 12:53:29 +0000523 hdmi_set_clk_regenerator(hdmi, hdmi->hdmi_data.video_mode.mpixelclock,
Russell Kingb195fbd2015-07-22 11:28:16 +0100524 hdmi->sample_rate);
Russell King6bcf4952015-02-02 11:01:08 +0000525 mutex_unlock(&hdmi->audio_mutex);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200526}
527
Russell Kingb5814ff2015-03-27 12:50:58 +0000528void dw_hdmi_set_sample_rate(struct dw_hdmi *hdmi, unsigned int rate)
529{
530 mutex_lock(&hdmi->audio_mutex);
531 hdmi->sample_rate = rate;
532 hdmi_set_clk_regenerator(hdmi, hdmi->hdmi_data.video_mode.mpixelclock,
Russell Kingb195fbd2015-07-22 11:28:16 +0100533 hdmi->sample_rate);
Russell Kingb5814ff2015-03-27 12:50:58 +0000534 mutex_unlock(&hdmi->audio_mutex);
535}
536EXPORT_SYMBOL_GPL(dw_hdmi_set_sample_rate);
537
Russell Kingb90120a2015-03-27 12:59:58 +0000538void dw_hdmi_audio_enable(struct dw_hdmi *hdmi)
539{
540 unsigned long flags;
541
542 spin_lock_irqsave(&hdmi->audio_lock, flags);
543 hdmi->audio_enable = true;
544 hdmi_set_cts_n(hdmi, hdmi->audio_cts, hdmi->audio_n);
545 spin_unlock_irqrestore(&hdmi->audio_lock, flags);
546}
547EXPORT_SYMBOL_GPL(dw_hdmi_audio_enable);
548
549void dw_hdmi_audio_disable(struct dw_hdmi *hdmi)
550{
551 unsigned long flags;
552
553 spin_lock_irqsave(&hdmi->audio_lock, flags);
554 hdmi->audio_enable = false;
555 hdmi_set_cts_n(hdmi, hdmi->audio_cts, 0);
556 spin_unlock_irqrestore(&hdmi->audio_lock, flags);
557}
558EXPORT_SYMBOL_GPL(dw_hdmi_audio_disable);
559
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200560/*
561 * this submodule is responsible for the video data synchronization.
562 * for example, for RGB 4:4:4 input, the data map is defined as
563 * pin{47~40} <==> R[7:0]
564 * pin{31~24} <==> G[7:0]
565 * pin{15~8} <==> B[7:0]
566 */
Andy Yanb21f4b62014-12-05 14:26:31 +0800567static void hdmi_video_sample(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200568{
569 int color_format = 0;
570 u8 val;
571
572 if (hdmi->hdmi_data.enc_in_format == RGB) {
573 if (hdmi->hdmi_data.enc_color_depth == 8)
574 color_format = 0x01;
575 else if (hdmi->hdmi_data.enc_color_depth == 10)
576 color_format = 0x03;
577 else if (hdmi->hdmi_data.enc_color_depth == 12)
578 color_format = 0x05;
579 else if (hdmi->hdmi_data.enc_color_depth == 16)
580 color_format = 0x07;
581 else
582 return;
583 } else if (hdmi->hdmi_data.enc_in_format == YCBCR444) {
584 if (hdmi->hdmi_data.enc_color_depth == 8)
585 color_format = 0x09;
586 else if (hdmi->hdmi_data.enc_color_depth == 10)
587 color_format = 0x0B;
588 else if (hdmi->hdmi_data.enc_color_depth == 12)
589 color_format = 0x0D;
590 else if (hdmi->hdmi_data.enc_color_depth == 16)
591 color_format = 0x0F;
592 else
593 return;
594 } else if (hdmi->hdmi_data.enc_in_format == YCBCR422_8BITS) {
595 if (hdmi->hdmi_data.enc_color_depth == 8)
596 color_format = 0x16;
597 else if (hdmi->hdmi_data.enc_color_depth == 10)
598 color_format = 0x14;
599 else if (hdmi->hdmi_data.enc_color_depth == 12)
600 color_format = 0x12;
601 else
602 return;
603 }
604
605 val = HDMI_TX_INVID0_INTERNAL_DE_GENERATOR_DISABLE |
606 ((color_format << HDMI_TX_INVID0_VIDEO_MAPPING_OFFSET) &
607 HDMI_TX_INVID0_VIDEO_MAPPING_MASK);
608 hdmi_writeb(hdmi, val, HDMI_TX_INVID0);
609
610 /* Enable TX stuffing: When DE is inactive, fix the output data to 0 */
611 val = HDMI_TX_INSTUFFING_BDBDATA_STUFFING_ENABLE |
612 HDMI_TX_INSTUFFING_RCRDATA_STUFFING_ENABLE |
613 HDMI_TX_INSTUFFING_GYDATA_STUFFING_ENABLE;
614 hdmi_writeb(hdmi, val, HDMI_TX_INSTUFFING);
615 hdmi_writeb(hdmi, 0x0, HDMI_TX_GYDATA0);
616 hdmi_writeb(hdmi, 0x0, HDMI_TX_GYDATA1);
617 hdmi_writeb(hdmi, 0x0, HDMI_TX_RCRDATA0);
618 hdmi_writeb(hdmi, 0x0, HDMI_TX_RCRDATA1);
619 hdmi_writeb(hdmi, 0x0, HDMI_TX_BCBDATA0);
620 hdmi_writeb(hdmi, 0x0, HDMI_TX_BCBDATA1);
621}
622
Andy Yanb21f4b62014-12-05 14:26:31 +0800623static int is_color_space_conversion(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200624{
Fabio Estevamba92b222014-02-06 10:12:03 -0200625 return hdmi->hdmi_data.enc_in_format != hdmi->hdmi_data.enc_out_format;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200626}
627
Andy Yanb21f4b62014-12-05 14:26:31 +0800628static int is_color_space_decimation(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200629{
Fabio Estevamba92b222014-02-06 10:12:03 -0200630 if (hdmi->hdmi_data.enc_out_format != YCBCR422_8BITS)
631 return 0;
632 if (hdmi->hdmi_data.enc_in_format == RGB ||
633 hdmi->hdmi_data.enc_in_format == YCBCR444)
634 return 1;
635 return 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200636}
637
Andy Yanb21f4b62014-12-05 14:26:31 +0800638static int is_color_space_interpolation(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200639{
Fabio Estevamba92b222014-02-06 10:12:03 -0200640 if (hdmi->hdmi_data.enc_in_format != YCBCR422_8BITS)
641 return 0;
642 if (hdmi->hdmi_data.enc_out_format == RGB ||
643 hdmi->hdmi_data.enc_out_format == YCBCR444)
644 return 1;
645 return 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200646}
647
Andy Yanb21f4b62014-12-05 14:26:31 +0800648static void dw_hdmi_update_csc_coeffs(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200649{
650 const u16 (*csc_coeff)[3][4] = &csc_coeff_default;
Russell Kingc082f9d2013-11-04 12:10:40 +0000651 unsigned i;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200652 u32 csc_scale = 1;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200653
654 if (is_color_space_conversion(hdmi)) {
655 if (hdmi->hdmi_data.enc_out_format == RGB) {
Gulsah Kose256a38b2014-03-09 20:11:07 +0200656 if (hdmi->hdmi_data.colorimetry ==
657 HDMI_COLORIMETRY_ITU_601)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200658 csc_coeff = &csc_coeff_rgb_out_eitu601;
659 else
660 csc_coeff = &csc_coeff_rgb_out_eitu709;
661 } else if (hdmi->hdmi_data.enc_in_format == RGB) {
Gulsah Kose256a38b2014-03-09 20:11:07 +0200662 if (hdmi->hdmi_data.colorimetry ==
663 HDMI_COLORIMETRY_ITU_601)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200664 csc_coeff = &csc_coeff_rgb_in_eitu601;
665 else
666 csc_coeff = &csc_coeff_rgb_in_eitu709;
667 csc_scale = 0;
668 }
669 }
670
Russell Kingc082f9d2013-11-04 12:10:40 +0000671 /* The CSC registers are sequential, alternating MSB then LSB */
672 for (i = 0; i < ARRAY_SIZE(csc_coeff_default[0]); i++) {
673 u16 coeff_a = (*csc_coeff)[0][i];
674 u16 coeff_b = (*csc_coeff)[1][i];
675 u16 coeff_c = (*csc_coeff)[2][i];
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200676
Andy Yanb5878332014-12-05 14:23:52 +0800677 hdmi_writeb(hdmi, coeff_a & 0xff, HDMI_CSC_COEF_A1_LSB + i * 2);
Russell Kingc082f9d2013-11-04 12:10:40 +0000678 hdmi_writeb(hdmi, coeff_a >> 8, HDMI_CSC_COEF_A1_MSB + i * 2);
679 hdmi_writeb(hdmi, coeff_b & 0xff, HDMI_CSC_COEF_B1_LSB + i * 2);
680 hdmi_writeb(hdmi, coeff_b >> 8, HDMI_CSC_COEF_B1_MSB + i * 2);
Andy Yanb5878332014-12-05 14:23:52 +0800681 hdmi_writeb(hdmi, coeff_c & 0xff, HDMI_CSC_COEF_C1_LSB + i * 2);
Russell Kingc082f9d2013-11-04 12:10:40 +0000682 hdmi_writeb(hdmi, coeff_c >> 8, HDMI_CSC_COEF_C1_MSB + i * 2);
683 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200684
Russell King812bc612013-11-04 12:42:02 +0000685 hdmi_modb(hdmi, csc_scale, HDMI_CSC_SCALE_CSCSCALE_MASK,
686 HDMI_CSC_SCALE);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200687}
688
Andy Yanb21f4b62014-12-05 14:26:31 +0800689static void hdmi_video_csc(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200690{
691 int color_depth = 0;
692 int interpolation = HDMI_CSC_CFG_INTMODE_DISABLE;
693 int decimation = 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200694
695 /* YCC422 interpolation to 444 mode */
696 if (is_color_space_interpolation(hdmi))
697 interpolation = HDMI_CSC_CFG_INTMODE_CHROMA_INT_FORMULA1;
698 else if (is_color_space_decimation(hdmi))
699 decimation = HDMI_CSC_CFG_DECMODE_CHROMA_INT_FORMULA3;
700
701 if (hdmi->hdmi_data.enc_color_depth == 8)
702 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_24BPP;
703 else if (hdmi->hdmi_data.enc_color_depth == 10)
704 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_30BPP;
705 else if (hdmi->hdmi_data.enc_color_depth == 12)
706 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_36BPP;
707 else if (hdmi->hdmi_data.enc_color_depth == 16)
708 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_48BPP;
709 else
710 return;
711
712 /* Configure the CSC registers */
713 hdmi_writeb(hdmi, interpolation | decimation, HDMI_CSC_CFG);
Russell King812bc612013-11-04 12:42:02 +0000714 hdmi_modb(hdmi, color_depth, HDMI_CSC_SCALE_CSC_COLORDE_PTH_MASK,
715 HDMI_CSC_SCALE);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200716
Andy Yanb21f4b62014-12-05 14:26:31 +0800717 dw_hdmi_update_csc_coeffs(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200718}
719
720/*
721 * HDMI video packetizer is used to packetize the data.
722 * for example, if input is YCC422 mode or repeater is used,
723 * data should be repacked this module can be bypassed.
724 */
Andy Yanb21f4b62014-12-05 14:26:31 +0800725static void hdmi_video_packetize(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200726{
727 unsigned int color_depth = 0;
728 unsigned int remap_size = HDMI_VP_REMAP_YCC422_16bit;
729 unsigned int output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_PP;
730 struct hdmi_data_info *hdmi_data = &hdmi->hdmi_data;
Russell Kingbebdf662013-11-04 12:55:30 +0000731 u8 val, vp_conf;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200732
Andy Yanb5878332014-12-05 14:23:52 +0800733 if (hdmi_data->enc_out_format == RGB ||
734 hdmi_data->enc_out_format == YCBCR444) {
735 if (!hdmi_data->enc_color_depth) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200736 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;
Andy Yanb5878332014-12-05 14:23:52 +0800737 } else if (hdmi_data->enc_color_depth == 8) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200738 color_depth = 4;
739 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;
Andy Yanb5878332014-12-05 14:23:52 +0800740 } else if (hdmi_data->enc_color_depth == 10) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200741 color_depth = 5;
Andy Yanb5878332014-12-05 14:23:52 +0800742 } else if (hdmi_data->enc_color_depth == 12) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200743 color_depth = 6;
Andy Yanb5878332014-12-05 14:23:52 +0800744 } else if (hdmi_data->enc_color_depth == 16) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200745 color_depth = 7;
Andy Yanb5878332014-12-05 14:23:52 +0800746 } else {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200747 return;
Andy Yanb5878332014-12-05 14:23:52 +0800748 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200749 } else if (hdmi_data->enc_out_format == YCBCR422_8BITS) {
750 if (!hdmi_data->enc_color_depth ||
751 hdmi_data->enc_color_depth == 8)
752 remap_size = HDMI_VP_REMAP_YCC422_16bit;
753 else if (hdmi_data->enc_color_depth == 10)
754 remap_size = HDMI_VP_REMAP_YCC422_20bit;
755 else if (hdmi_data->enc_color_depth == 12)
756 remap_size = HDMI_VP_REMAP_YCC422_24bit;
757 else
758 return;
759 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_YCC422;
Andy Yanb5878332014-12-05 14:23:52 +0800760 } else {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200761 return;
Andy Yanb5878332014-12-05 14:23:52 +0800762 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200763
764 /* set the packetizer registers */
765 val = ((color_depth << HDMI_VP_PR_CD_COLOR_DEPTH_OFFSET) &
766 HDMI_VP_PR_CD_COLOR_DEPTH_MASK) |
767 ((hdmi_data->pix_repet_factor <<
768 HDMI_VP_PR_CD_DESIRED_PR_FACTOR_OFFSET) &
769 HDMI_VP_PR_CD_DESIRED_PR_FACTOR_MASK);
770 hdmi_writeb(hdmi, val, HDMI_VP_PR_CD);
771
Russell King812bc612013-11-04 12:42:02 +0000772 hdmi_modb(hdmi, HDMI_VP_STUFF_PR_STUFFING_STUFFING_MODE,
773 HDMI_VP_STUFF_PR_STUFFING_MASK, HDMI_VP_STUFF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200774
775 /* Data from pixel repeater block */
776 if (hdmi_data->pix_repet_factor > 1) {
Russell Kingbebdf662013-11-04 12:55:30 +0000777 vp_conf = HDMI_VP_CONF_PR_EN_ENABLE |
778 HDMI_VP_CONF_BYPASS_SELECT_PIX_REPEATER;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200779 } else { /* data from packetizer block */
Russell Kingbebdf662013-11-04 12:55:30 +0000780 vp_conf = HDMI_VP_CONF_PR_EN_DISABLE |
781 HDMI_VP_CONF_BYPASS_SELECT_VID_PACKETIZER;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200782 }
783
Russell Kingbebdf662013-11-04 12:55:30 +0000784 hdmi_modb(hdmi, vp_conf,
785 HDMI_VP_CONF_PR_EN_MASK |
786 HDMI_VP_CONF_BYPASS_SELECT_MASK, HDMI_VP_CONF);
787
Russell King812bc612013-11-04 12:42:02 +0000788 hdmi_modb(hdmi, 1 << HDMI_VP_STUFF_IDEFAULT_PHASE_OFFSET,
789 HDMI_VP_STUFF_IDEFAULT_PHASE_MASK, HDMI_VP_STUFF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200790
791 hdmi_writeb(hdmi, remap_size, HDMI_VP_REMAP);
792
793 if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_PP) {
Russell Kingbebdf662013-11-04 12:55:30 +0000794 vp_conf = HDMI_VP_CONF_BYPASS_EN_DISABLE |
795 HDMI_VP_CONF_PP_EN_ENABLE |
796 HDMI_VP_CONF_YCC422_EN_DISABLE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200797 } else if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_YCC422) {
Russell Kingbebdf662013-11-04 12:55:30 +0000798 vp_conf = HDMI_VP_CONF_BYPASS_EN_DISABLE |
799 HDMI_VP_CONF_PP_EN_DISABLE |
800 HDMI_VP_CONF_YCC422_EN_ENABLE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200801 } else if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS) {
Russell Kingbebdf662013-11-04 12:55:30 +0000802 vp_conf = HDMI_VP_CONF_BYPASS_EN_ENABLE |
803 HDMI_VP_CONF_PP_EN_DISABLE |
804 HDMI_VP_CONF_YCC422_EN_DISABLE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200805 } else {
806 return;
807 }
808
Russell Kingbebdf662013-11-04 12:55:30 +0000809 hdmi_modb(hdmi, vp_conf,
810 HDMI_VP_CONF_BYPASS_EN_MASK | HDMI_VP_CONF_PP_EN_ENMASK |
811 HDMI_VP_CONF_YCC422_EN_MASK, HDMI_VP_CONF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200812
Russell King812bc612013-11-04 12:42:02 +0000813 hdmi_modb(hdmi, HDMI_VP_STUFF_PP_STUFFING_STUFFING_MODE |
814 HDMI_VP_STUFF_YCC422_STUFFING_STUFFING_MODE,
815 HDMI_VP_STUFF_PP_STUFFING_MASK |
816 HDMI_VP_STUFF_YCC422_STUFFING_MASK, HDMI_VP_STUFF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200817
Russell King812bc612013-11-04 12:42:02 +0000818 hdmi_modb(hdmi, output_select, HDMI_VP_CONF_OUTPUT_SELECTOR_MASK,
819 HDMI_VP_CONF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200820}
821
Laurent Pinchartf1585f62017-03-06 01:36:15 +0200822/* -----------------------------------------------------------------------------
823 * Synopsys PHY Handling
824 */
825
Andy Yanb21f4b62014-12-05 14:26:31 +0800826static inline void hdmi_phy_test_clear(struct dw_hdmi *hdmi,
Andy Yanb5878332014-12-05 14:23:52 +0800827 unsigned char bit)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200828{
Russell King812bc612013-11-04 12:42:02 +0000829 hdmi_modb(hdmi, bit << HDMI_PHY_TST0_TSTCLR_OFFSET,
830 HDMI_PHY_TST0_TSTCLR_MASK, HDMI_PHY_TST0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200831}
832
Andy Yanb21f4b62014-12-05 14:26:31 +0800833static bool hdmi_phy_wait_i2c_done(struct dw_hdmi *hdmi, int msec)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200834{
Andy Yana4d3b8b2014-12-05 14:31:09 +0800835 u32 val;
836
837 while ((val = hdmi_readb(hdmi, HDMI_IH_I2CMPHY_STAT0) & 0x3) == 0) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200838 if (msec-- == 0)
839 return false;
Emil Renner Berthing0e6bcf32014-03-30 00:21:21 +0100840 udelay(1000);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200841 }
Andy Yana4d3b8b2014-12-05 14:31:09 +0800842 hdmi_writeb(hdmi, val, HDMI_IH_I2CMPHY_STAT0);
843
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200844 return true;
845}
846
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +0200847void dw_hdmi_phy_i2c_write(struct dw_hdmi *hdmi, unsigned short data,
848 unsigned char addr)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200849{
850 hdmi_writeb(hdmi, 0xFF, HDMI_IH_I2CMPHY_STAT0);
851 hdmi_writeb(hdmi, addr, HDMI_PHY_I2CM_ADDRESS_ADDR);
852 hdmi_writeb(hdmi, (unsigned char)(data >> 8),
Andy Yanb5878332014-12-05 14:23:52 +0800853 HDMI_PHY_I2CM_DATAO_1_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200854 hdmi_writeb(hdmi, (unsigned char)(data >> 0),
Andy Yanb5878332014-12-05 14:23:52 +0800855 HDMI_PHY_I2CM_DATAO_0_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200856 hdmi_writeb(hdmi, HDMI_PHY_I2CM_OPERATION_ADDR_WRITE,
Andy Yanb5878332014-12-05 14:23:52 +0800857 HDMI_PHY_I2CM_OPERATION_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200858 hdmi_phy_wait_i2c_done(hdmi, 1000);
859}
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +0200860EXPORT_SYMBOL_GPL(dw_hdmi_phy_i2c_write);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200861
Russell King2fada102015-07-28 12:21:34 +0100862static void dw_hdmi_phy_enable_powerdown(struct dw_hdmi *hdmi, bool enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200863{
Russell King2fada102015-07-28 12:21:34 +0100864 hdmi_mask_writeb(hdmi, !enable, HDMI_PHY_CONF0,
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200865 HDMI_PHY_CONF0_PDZ_OFFSET,
866 HDMI_PHY_CONF0_PDZ_MASK);
867}
868
Andy Yanb21f4b62014-12-05 14:26:31 +0800869static void dw_hdmi_phy_enable_tmds(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200870{
871 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
872 HDMI_PHY_CONF0_ENTMDS_OFFSET,
873 HDMI_PHY_CONF0_ENTMDS_MASK);
874}
875
Laurent Pinchartf4104e82017-01-17 10:29:02 +0200876static void dw_hdmi_phy_enable_svsret(struct dw_hdmi *hdmi, u8 enable)
Andy Yand346c142014-12-05 14:31:53 +0800877{
878 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
Laurent Pinchartf4104e82017-01-17 10:29:02 +0200879 HDMI_PHY_CONF0_SVSRET_OFFSET,
880 HDMI_PHY_CONF0_SVSRET_MASK);
Andy Yand346c142014-12-05 14:31:53 +0800881}
882
Andy Yanb21f4b62014-12-05 14:26:31 +0800883static void dw_hdmi_phy_gen2_pddq(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200884{
885 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
886 HDMI_PHY_CONF0_GEN2_PDDQ_OFFSET,
887 HDMI_PHY_CONF0_GEN2_PDDQ_MASK);
888}
889
Andy Yanb21f4b62014-12-05 14:26:31 +0800890static void dw_hdmi_phy_gen2_txpwron(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_TXPWRON_OFFSET,
894 HDMI_PHY_CONF0_GEN2_TXPWRON_MASK);
895}
896
Andy Yanb21f4b62014-12-05 14:26:31 +0800897static void dw_hdmi_phy_sel_data_en_pol(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_SELDATAENPOL_OFFSET,
901 HDMI_PHY_CONF0_SELDATAENPOL_MASK);
902}
903
Andy Yanb21f4b62014-12-05 14:26:31 +0800904static void dw_hdmi_phy_sel_interface_control(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_SELDIPIF_OFFSET,
908 HDMI_PHY_CONF0_SELDIPIF_MASK);
909}
910
Laurent Pinchartb0e583e2017-03-06 01:35:39 +0200911static void dw_hdmi_phy_power_off(struct dw_hdmi *hdmi)
912{
Laurent Pinchartf1585f62017-03-06 01:36:15 +0200913 const struct dw_hdmi_phy_data *phy = hdmi->phy.data;
Laurent Pinchartb0e583e2017-03-06 01:35:39 +0200914 unsigned int i;
915 u16 val;
916
917 if (phy->gen == 1) {
918 dw_hdmi_phy_enable_tmds(hdmi, 0);
919 dw_hdmi_phy_enable_powerdown(hdmi, true);
920 return;
921 }
922
923 dw_hdmi_phy_gen2_txpwron(hdmi, 0);
924
925 /*
926 * Wait for TX_PHY_LOCK to be deasserted to indicate that the PHY went
927 * to low power mode.
928 */
929 for (i = 0; i < 5; ++i) {
930 val = hdmi_readb(hdmi, HDMI_PHY_STAT0);
931 if (!(val & HDMI_PHY_TX_PHY_LOCK))
932 break;
933
934 usleep_range(1000, 2000);
935 }
936
937 if (val & HDMI_PHY_TX_PHY_LOCK)
938 dev_warn(hdmi->dev, "PHY failed to power down\n");
939 else
940 dev_dbg(hdmi->dev, "PHY powered down in %u iterations\n", i);
941
942 dw_hdmi_phy_gen2_pddq(hdmi, 1);
943}
944
Laurent Pinchart181e0ef2017-03-06 01:35:57 +0200945static int dw_hdmi_phy_power_on(struct dw_hdmi *hdmi)
946{
Laurent Pinchartf1585f62017-03-06 01:36:15 +0200947 const struct dw_hdmi_phy_data *phy = hdmi->phy.data;
Laurent Pinchart181e0ef2017-03-06 01:35:57 +0200948 unsigned int i;
949 u8 val;
950
951 if (phy->gen == 1) {
952 dw_hdmi_phy_enable_powerdown(hdmi, false);
953
954 /* Toggle TMDS enable. */
955 dw_hdmi_phy_enable_tmds(hdmi, 0);
956 dw_hdmi_phy_enable_tmds(hdmi, 1);
957 return 0;
958 }
959
960 dw_hdmi_phy_gen2_txpwron(hdmi, 1);
961 dw_hdmi_phy_gen2_pddq(hdmi, 0);
962
963 /* Wait for PHY PLL lock */
964 for (i = 0; i < 5; ++i) {
965 val = hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_TX_PHY_LOCK;
966 if (val)
967 break;
968
969 usleep_range(1000, 2000);
970 }
971
972 if (!val) {
973 dev_err(hdmi->dev, "PHY PLL failed to lock\n");
974 return -ETIMEDOUT;
975 }
976
977 dev_dbg(hdmi->dev, "PHY PLL locked %u iterations\n", i);
978 return 0;
979}
980
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +0200981/*
982 * PHY configuration function for the DWC HDMI 3D TX PHY. Based on the available
983 * information the DWC MHL PHY has the same register layout and is thus also
984 * supported by this function.
985 */
986static int hdmi_phy_configure_dwc_hdmi_3d_tx(struct dw_hdmi *hdmi,
987 const struct dw_hdmi_plat_data *pdata,
988 unsigned long mpixelclock)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200989{
Russell King39cc1532015-03-31 18:34:11 +0100990 const struct dw_hdmi_mpll_config *mpll_config = pdata->mpll_cfg;
991 const struct dw_hdmi_curr_ctrl *curr_ctrl = pdata->cur_ctr;
992 const struct dw_hdmi_phy_config *phy_config = pdata->phy_config;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200993
Russell King39cc1532015-03-31 18:34:11 +0100994 /* PLL/MPLL Cfg - always match on final entry */
995 for (; mpll_config->mpixelclock != ~0UL; mpll_config++)
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +0200996 if (mpixelclock <= mpll_config->mpixelclock)
Russell King39cc1532015-03-31 18:34:11 +0100997 break;
998
999 for (; curr_ctrl->mpixelclock != ~0UL; curr_ctrl++)
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001000 if (mpixelclock <= curr_ctrl->mpixelclock)
Russell King39cc1532015-03-31 18:34:11 +01001001 break;
1002
1003 for (; phy_config->mpixelclock != ~0UL; phy_config++)
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001004 if (mpixelclock <= phy_config->mpixelclock)
Russell King39cc1532015-03-31 18:34:11 +01001005 break;
1006
1007 if (mpll_config->mpixelclock == ~0UL ||
1008 curr_ctrl->mpixelclock == ~0UL ||
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001009 phy_config->mpixelclock == ~0UL)
Russell King39cc1532015-03-31 18:34:11 +01001010 return -EINVAL;
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001011
1012 dw_hdmi_phy_i2c_write(hdmi, mpll_config->res[0].cpce,
1013 HDMI_3D_TX_PHY_CPCE_CTRL);
1014 dw_hdmi_phy_i2c_write(hdmi, mpll_config->res[0].gmp,
1015 HDMI_3D_TX_PHY_GMPCTRL);
1016 dw_hdmi_phy_i2c_write(hdmi, curr_ctrl->curr[0],
1017 HDMI_3D_TX_PHY_CURRCTRL);
1018
1019 dw_hdmi_phy_i2c_write(hdmi, 0, HDMI_3D_TX_PHY_PLLPHBYCTRL);
1020 dw_hdmi_phy_i2c_write(hdmi, HDMI_3D_TX_PHY_MSM_CTRL_CKO_SEL_FB_CLK,
1021 HDMI_3D_TX_PHY_MSM_CTRL);
1022
1023 dw_hdmi_phy_i2c_write(hdmi, phy_config->term, HDMI_3D_TX_PHY_TXTERM);
1024 dw_hdmi_phy_i2c_write(hdmi, phy_config->sym_ctr,
1025 HDMI_3D_TX_PHY_CKSYMTXCTRL);
1026 dw_hdmi_phy_i2c_write(hdmi, phy_config->vlev_ctr,
1027 HDMI_3D_TX_PHY_VLEVCTRL);
1028
1029 /* Override and disable clock termination. */
1030 dw_hdmi_phy_i2c_write(hdmi, HDMI_3D_TX_PHY_CKCALCTRL_OVERRIDE,
1031 HDMI_3D_TX_PHY_CKCALCTRL);
1032
1033 return 0;
1034}
1035
1036static int hdmi_phy_configure(struct dw_hdmi *hdmi)
1037{
1038 const struct dw_hdmi_phy_data *phy = hdmi->phy.data;
1039 const struct dw_hdmi_plat_data *pdata = hdmi->plat_data;
1040 unsigned long mpixelclock = hdmi->hdmi_data.video_mode.mpixelclock;
1041 int ret;
Russell King39cc1532015-03-31 18:34:11 +01001042
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02001043 dw_hdmi_phy_power_off(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001044
Laurent Pinchart2668db32017-01-17 10:29:09 +02001045 /* Leave low power consumption mode by asserting SVSRET. */
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001046 if (phy->has_svsret)
Laurent Pinchart2668db32017-01-17 10:29:09 +02001047 dw_hdmi_phy_enable_svsret(hdmi, 1);
1048
Laurent Pinchart54d72732017-01-17 10:29:08 +02001049 /* PHY reset. The reset signal is active high on Gen2 PHYs. */
1050 hdmi_writeb(hdmi, HDMI_MC_PHYRSTZ_PHYRSTZ, HDMI_MC_PHYRSTZ);
1051 hdmi_writeb(hdmi, 0, HDMI_MC_PHYRSTZ);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001052
1053 hdmi_writeb(hdmi, HDMI_MC_HEACPHY_RST_ASSERT, HDMI_MC_HEACPHY_RST);
1054
1055 hdmi_phy_test_clear(hdmi, 1);
1056 hdmi_writeb(hdmi, HDMI_PHY_I2CM_SLAVE_ADDR_PHY_GEN2,
Andy Yanb5878332014-12-05 14:23:52 +08001057 HDMI_PHY_I2CM_SLAVE_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001058 hdmi_phy_test_clear(hdmi, 0);
1059
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001060 /* Write to the PHY as configured by the platform */
1061 if (pdata->configure_phy)
1062 ret = pdata->configure_phy(hdmi, pdata, mpixelclock);
1063 else
1064 ret = phy->configure(hdmi, pdata, mpixelclock);
1065 if (ret) {
1066 dev_err(hdmi->dev, "PHY configuration failed (clock %lu)\n",
1067 mpixelclock);
1068 return ret;
1069 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001070
Laurent Pinchart181e0ef2017-03-06 01:35:57 +02001071 return dw_hdmi_phy_power_on(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001072}
1073
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001074static int dw_hdmi_phy_init(struct dw_hdmi *hdmi, void *data,
1075 struct drm_display_mode *mode)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001076{
1077 int i, ret;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001078
1079 /* HDMI Phy spec says to do the phy initialization sequence twice */
1080 for (i = 0; i < 2; i++) {
Andy Yanb21f4b62014-12-05 14:26:31 +08001081 dw_hdmi_phy_sel_data_en_pol(hdmi, 1);
1082 dw_hdmi_phy_sel_interface_control(hdmi, 0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001083
Laurent Pinchart8b9e1c02017-03-03 19:19:59 +02001084 ret = hdmi_phy_configure(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001085 if (ret)
1086 return ret;
1087 }
1088
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001089 return 0;
1090}
1091
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001092static void dw_hdmi_phy_disable(struct dw_hdmi *hdmi, void *data)
1093{
1094 dw_hdmi_phy_power_off(hdmi);
1095}
1096
1097static enum drm_connector_status dw_hdmi_phy_read_hpd(struct dw_hdmi *hdmi,
1098 void *data)
1099{
1100 return hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_HPD ?
1101 connector_status_connected : connector_status_disconnected;
1102}
1103
1104static const struct dw_hdmi_phy_ops dw_hdmi_synopsys_phy_ops = {
1105 .init = dw_hdmi_phy_init,
1106 .disable = dw_hdmi_phy_disable,
1107 .read_hpd = dw_hdmi_phy_read_hpd,
1108};
1109
1110/* -----------------------------------------------------------------------------
1111 * HDMI TX Setup
1112 */
1113
Andy Yanb21f4b62014-12-05 14:26:31 +08001114static void hdmi_tx_hdcp_config(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001115{
Russell King812bc612013-11-04 12:42:02 +00001116 u8 de;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001117
1118 if (hdmi->hdmi_data.video_mode.mdataenablepolarity)
1119 de = HDMI_A_VIDPOLCFG_DATAENPOL_ACTIVE_HIGH;
1120 else
1121 de = HDMI_A_VIDPOLCFG_DATAENPOL_ACTIVE_LOW;
1122
1123 /* disable rx detect */
Russell King812bc612013-11-04 12:42:02 +00001124 hdmi_modb(hdmi, HDMI_A_HDCPCFG0_RXDETECT_DISABLE,
1125 HDMI_A_HDCPCFG0_RXDETECT_MASK, HDMI_A_HDCPCFG0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001126
Russell King812bc612013-11-04 12:42:02 +00001127 hdmi_modb(hdmi, de, HDMI_A_VIDPOLCFG_DATAENPOL_MASK, HDMI_A_VIDPOLCFG);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001128
Russell King812bc612013-11-04 12:42:02 +00001129 hdmi_modb(hdmi, HDMI_A_HDCPCFG1_ENCRYPTIONDISABLE_DISABLE,
1130 HDMI_A_HDCPCFG1_ENCRYPTIONDISABLE_MASK, HDMI_A_HDCPCFG1);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001131}
1132
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001133static void hdmi_config_AVI(struct dw_hdmi *hdmi, struct drm_display_mode *mode)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001134{
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001135 struct hdmi_avi_infoframe frame;
1136 u8 val;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001137
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001138 /* Initialise info frame from DRM mode */
1139 drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001140
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001141 if (hdmi->hdmi_data.enc_out_format == YCBCR444)
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001142 frame.colorspace = HDMI_COLORSPACE_YUV444;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001143 else if (hdmi->hdmi_data.enc_out_format == YCBCR422_8BITS)
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001144 frame.colorspace = HDMI_COLORSPACE_YUV422;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001145 else
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001146 frame.colorspace = HDMI_COLORSPACE_RGB;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001147
1148 /* Set up colorimetry */
1149 if (hdmi->hdmi_data.enc_out_format == XVYCC444) {
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001150 frame.colorimetry = HDMI_COLORIMETRY_EXTENDED;
Sachin Kamat5a819ed2014-01-28 10:33:16 +05301151 if (hdmi->hdmi_data.colorimetry == HDMI_COLORIMETRY_ITU_601)
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001152 frame.extended_colorimetry =
1153 HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
Sachin Kamat5a819ed2014-01-28 10:33:16 +05301154 else /*hdmi->hdmi_data.colorimetry == HDMI_COLORIMETRY_ITU_709*/
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001155 frame.extended_colorimetry =
1156 HDMI_EXTENDED_COLORIMETRY_XV_YCC_709;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001157 } else if (hdmi->hdmi_data.enc_out_format != RGB) {
Russell Kingd083c312015-03-27 23:14:16 +00001158 frame.colorimetry = hdmi->hdmi_data.colorimetry;
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001159 frame.extended_colorimetry = HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001160 } else { /* Carries no data */
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001161 frame.colorimetry = HDMI_COLORIMETRY_NONE;
1162 frame.extended_colorimetry = HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001163 }
1164
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001165 frame.scan_mode = HDMI_SCAN_MODE_NONE;
1166
1167 /*
1168 * The Designware IP uses a different byte format from standard
1169 * AVI info frames, though generally the bits are in the correct
1170 * bytes.
1171 */
1172
1173 /*
Jose Abreub0118e72016-08-29 10:30:51 +01001174 * AVI data byte 1 differences: Colorspace in bits 0,1 rather than 5,6,
1175 * scan info in bits 4,5 rather than 0,1 and active aspect present in
1176 * bit 6 rather than 4.
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001177 */
Jose Abreub0118e72016-08-29 10:30:51 +01001178 val = (frame.scan_mode & 3) << 4 | (frame.colorspace & 3);
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001179 if (frame.active_aspect & 15)
1180 val |= HDMI_FC_AVICONF0_ACTIVE_FMT_INFO_PRESENT;
1181 if (frame.top_bar || frame.bottom_bar)
1182 val |= HDMI_FC_AVICONF0_BAR_DATA_HORIZ_BAR;
1183 if (frame.left_bar || frame.right_bar)
1184 val |= HDMI_FC_AVICONF0_BAR_DATA_VERT_BAR;
1185 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF0);
1186
1187 /* AVI data byte 2 differences: none */
1188 val = ((frame.colorimetry & 0x3) << 6) |
1189 ((frame.picture_aspect & 0x3) << 4) |
1190 (frame.active_aspect & 0xf);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001191 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF1);
1192
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001193 /* AVI data byte 3 differences: none */
1194 val = ((frame.extended_colorimetry & 0x7) << 4) |
1195 ((frame.quantization_range & 0x3) << 2) |
1196 (frame.nups & 0x3);
1197 if (frame.itc)
1198 val |= HDMI_FC_AVICONF2_IT_CONTENT_VALID;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001199 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF2);
1200
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001201 /* AVI data byte 4 differences: none */
1202 val = frame.video_code & 0x7f;
1203 hdmi_writeb(hdmi, val, HDMI_FC_AVIVID);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001204
1205 /* AVI Data Byte 5- set up input and output pixel repetition */
1206 val = (((hdmi->hdmi_data.video_mode.mpixelrepetitioninput + 1) <<
1207 HDMI_FC_PRCONF_INCOMING_PR_FACTOR_OFFSET) &
1208 HDMI_FC_PRCONF_INCOMING_PR_FACTOR_MASK) |
1209 ((hdmi->hdmi_data.video_mode.mpixelrepetitionoutput <<
1210 HDMI_FC_PRCONF_OUTPUT_PR_FACTOR_OFFSET) &
1211 HDMI_FC_PRCONF_OUTPUT_PR_FACTOR_MASK);
1212 hdmi_writeb(hdmi, val, HDMI_FC_PRCONF);
1213
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001214 /*
1215 * AVI data byte 5 differences: content type in 0,1 rather than 4,5,
1216 * ycc range in bits 2,3 rather than 6,7
1217 */
1218 val = ((frame.ycc_quantization_range & 0x3) << 2) |
1219 (frame.content_type & 0x3);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001220 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF3);
1221
1222 /* AVI Data Bytes 6-13 */
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001223 hdmi_writeb(hdmi, frame.top_bar & 0xff, HDMI_FC_AVIETB0);
1224 hdmi_writeb(hdmi, (frame.top_bar >> 8) & 0xff, HDMI_FC_AVIETB1);
1225 hdmi_writeb(hdmi, frame.bottom_bar & 0xff, HDMI_FC_AVISBB0);
1226 hdmi_writeb(hdmi, (frame.bottom_bar >> 8) & 0xff, HDMI_FC_AVISBB1);
1227 hdmi_writeb(hdmi, frame.left_bar & 0xff, HDMI_FC_AVIELB0);
1228 hdmi_writeb(hdmi, (frame.left_bar >> 8) & 0xff, HDMI_FC_AVIELB1);
1229 hdmi_writeb(hdmi, frame.right_bar & 0xff, HDMI_FC_AVISRB0);
1230 hdmi_writeb(hdmi, (frame.right_bar >> 8) & 0xff, HDMI_FC_AVISRB1);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001231}
1232
Andy Yanb21f4b62014-12-05 14:26:31 +08001233static void hdmi_av_composer(struct dw_hdmi *hdmi,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001234 const struct drm_display_mode *mode)
1235{
1236 u8 inv_val;
1237 struct hdmi_vmode *vmode = &hdmi->hdmi_data.video_mode;
1238 int hblank, vblank, h_de_hs, v_de_vs, hsync_len, vsync_len;
Russell Kinge80b9f42015-07-21 11:08:25 +01001239 unsigned int vdisplay;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001240
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001241 vmode->mpixelclock = mode->clock * 1000;
1242
1243 dev_dbg(hdmi->dev, "final pixclk = %d\n", vmode->mpixelclock);
1244
1245 /* Set up HDMI_FC_INVIDCONF */
1246 inv_val = (hdmi->hdmi_data.hdcp_enable ?
1247 HDMI_FC_INVIDCONF_HDCP_KEEPOUT_ACTIVE :
1248 HDMI_FC_INVIDCONF_HDCP_KEEPOUT_INACTIVE);
1249
Russell Kingb91eee82015-03-27 23:27:17 +00001250 inv_val |= mode->flags & DRM_MODE_FLAG_PVSYNC ?
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001251 HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_HIGH :
Russell Kingb91eee82015-03-27 23:27:17 +00001252 HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_LOW;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001253
Russell Kingb91eee82015-03-27 23:27:17 +00001254 inv_val |= mode->flags & DRM_MODE_FLAG_PHSYNC ?
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001255 HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_HIGH :
Russell Kingb91eee82015-03-27 23:27:17 +00001256 HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_LOW;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001257
1258 inv_val |= (vmode->mdataenablepolarity ?
1259 HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_HIGH :
1260 HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_LOW);
1261
1262 if (hdmi->vic == 39)
1263 inv_val |= HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_HIGH;
1264 else
Russell Kingb91eee82015-03-27 23:27:17 +00001265 inv_val |= mode->flags & DRM_MODE_FLAG_INTERLACE ?
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001266 HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_HIGH :
Russell Kingb91eee82015-03-27 23:27:17 +00001267 HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_LOW;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001268
Russell Kingb91eee82015-03-27 23:27:17 +00001269 inv_val |= mode->flags & DRM_MODE_FLAG_INTERLACE ?
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001270 HDMI_FC_INVIDCONF_IN_I_P_INTERLACED :
Russell Kingb91eee82015-03-27 23:27:17 +00001271 HDMI_FC_INVIDCONF_IN_I_P_PROGRESSIVE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001272
Russell King05b13422015-07-21 15:35:52 +01001273 inv_val |= hdmi->sink_is_hdmi ?
1274 HDMI_FC_INVIDCONF_DVI_MODEZ_HDMI_MODE :
1275 HDMI_FC_INVIDCONF_DVI_MODEZ_DVI_MODE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001276
1277 hdmi_writeb(hdmi, inv_val, HDMI_FC_INVIDCONF);
1278
Russell Kinge80b9f42015-07-21 11:08:25 +01001279 vdisplay = mode->vdisplay;
1280 vblank = mode->vtotal - mode->vdisplay;
1281 v_de_vs = mode->vsync_start - mode->vdisplay;
1282 vsync_len = mode->vsync_end - mode->vsync_start;
1283
1284 /*
1285 * When we're setting an interlaced mode, we need
1286 * to adjust the vertical timing to suit.
1287 */
1288 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
1289 vdisplay /= 2;
1290 vblank /= 2;
1291 v_de_vs /= 2;
1292 vsync_len /= 2;
1293 }
1294
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001295 /* Set up horizontal active pixel width */
1296 hdmi_writeb(hdmi, mode->hdisplay >> 8, HDMI_FC_INHACTV1);
1297 hdmi_writeb(hdmi, mode->hdisplay, HDMI_FC_INHACTV0);
1298
1299 /* Set up vertical active lines */
Russell Kinge80b9f42015-07-21 11:08:25 +01001300 hdmi_writeb(hdmi, vdisplay >> 8, HDMI_FC_INVACTV1);
1301 hdmi_writeb(hdmi, vdisplay, HDMI_FC_INVACTV0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001302
1303 /* Set up horizontal blanking pixel region width */
1304 hblank = mode->htotal - mode->hdisplay;
1305 hdmi_writeb(hdmi, hblank >> 8, HDMI_FC_INHBLANK1);
1306 hdmi_writeb(hdmi, hblank, HDMI_FC_INHBLANK0);
1307
1308 /* Set up vertical blanking pixel region width */
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001309 hdmi_writeb(hdmi, vblank, HDMI_FC_INVBLANK);
1310
1311 /* Set up HSYNC active edge delay width (in pixel clks) */
1312 h_de_hs = mode->hsync_start - mode->hdisplay;
1313 hdmi_writeb(hdmi, h_de_hs >> 8, HDMI_FC_HSYNCINDELAY1);
1314 hdmi_writeb(hdmi, h_de_hs, HDMI_FC_HSYNCINDELAY0);
1315
1316 /* Set up VSYNC active edge delay (in lines) */
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001317 hdmi_writeb(hdmi, v_de_vs, HDMI_FC_VSYNCINDELAY);
1318
1319 /* Set up HSYNC active pulse width (in pixel clks) */
1320 hsync_len = mode->hsync_end - mode->hsync_start;
1321 hdmi_writeb(hdmi, hsync_len >> 8, HDMI_FC_HSYNCINWIDTH1);
1322 hdmi_writeb(hdmi, hsync_len, HDMI_FC_HSYNCINWIDTH0);
1323
1324 /* Set up VSYNC active edge delay (in lines) */
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001325 hdmi_writeb(hdmi, vsync_len, HDMI_FC_VSYNCINWIDTH);
1326}
1327
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001328/* HDMI Initialization Step B.4 */
Andy Yanb21f4b62014-12-05 14:26:31 +08001329static void dw_hdmi_enable_video_path(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001330{
1331 u8 clkdis;
1332
1333 /* control period minimum duration */
1334 hdmi_writeb(hdmi, 12, HDMI_FC_CTRLDUR);
1335 hdmi_writeb(hdmi, 32, HDMI_FC_EXCTRLDUR);
1336 hdmi_writeb(hdmi, 1, HDMI_FC_EXCTRLSPAC);
1337
1338 /* Set to fill TMDS data channels */
1339 hdmi_writeb(hdmi, 0x0B, HDMI_FC_CH0PREAM);
1340 hdmi_writeb(hdmi, 0x16, HDMI_FC_CH1PREAM);
1341 hdmi_writeb(hdmi, 0x21, HDMI_FC_CH2PREAM);
1342
1343 /* Enable pixel clock and tmds data path */
1344 clkdis = 0x7F;
1345 clkdis &= ~HDMI_MC_CLKDIS_PIXELCLK_DISABLE;
1346 hdmi_writeb(hdmi, clkdis, HDMI_MC_CLKDIS);
1347
1348 clkdis &= ~HDMI_MC_CLKDIS_TMDSCLK_DISABLE;
1349 hdmi_writeb(hdmi, clkdis, HDMI_MC_CLKDIS);
1350
1351 /* Enable csc path */
1352 if (is_color_space_conversion(hdmi)) {
1353 clkdis &= ~HDMI_MC_CLKDIS_CSCCLK_DISABLE;
1354 hdmi_writeb(hdmi, clkdis, HDMI_MC_CLKDIS);
1355 }
Laurent Pinchart8b9e1c02017-03-03 19:19:59 +02001356
Neil Armstrong14247d72017-03-03 19:20:00 +02001357 /* Enable color space conversion if needed */
1358 if (is_color_space_conversion(hdmi))
Laurent Pinchart8b9e1c02017-03-03 19:19:59 +02001359 hdmi_writeb(hdmi, HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_IN_PATH,
1360 HDMI_MC_FLOWCTRL);
1361 else
1362 hdmi_writeb(hdmi, HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_BYPASS,
1363 HDMI_MC_FLOWCTRL);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001364}
1365
Andy Yanb21f4b62014-12-05 14:26:31 +08001366static void hdmi_enable_audio_clk(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001367{
Russell King812bc612013-11-04 12:42:02 +00001368 hdmi_modb(hdmi, 0, HDMI_MC_CLKDIS_AUDCLK_DISABLE, HDMI_MC_CLKDIS);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001369}
1370
1371/* Workaround to clear the overflow condition */
Andy Yanb21f4b62014-12-05 14:26:31 +08001372static void dw_hdmi_clear_overflow(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001373{
Laurent Pinchartbe41fc52017-01-17 10:29:05 +02001374 unsigned int count;
1375 unsigned int i;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001376 u8 val;
1377
Laurent Pinchartbe41fc52017-01-17 10:29:05 +02001378 /*
1379 * Under some circumstances the Frame Composer arithmetic unit can miss
1380 * an FC register write due to being busy processing the previous one.
1381 * The issue can be worked around by issuing a TMDS software reset and
1382 * then write one of the FC registers several times.
1383 *
1384 * The number of iterations matters and depends on the HDMI TX revision
1385 * (and possibly on the platform). So far only i.MX6Q (v1.30a) and
1386 * i.MX6DL (v1.31a) have been identified as needing the workaround, with
1387 * 4 and 1 iterations respectively.
1388 */
1389
1390 switch (hdmi->version) {
1391 case 0x130a:
1392 count = 4;
1393 break;
1394 case 0x131a:
1395 count = 1;
1396 break;
1397 default:
1398 return;
1399 }
1400
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001401 /* TMDS software reset */
1402 hdmi_writeb(hdmi, (u8)~HDMI_MC_SWRSTZ_TMDSSWRST_REQ, HDMI_MC_SWRSTZ);
1403
1404 val = hdmi_readb(hdmi, HDMI_FC_INVIDCONF);
Laurent Pinchartbe41fc52017-01-17 10:29:05 +02001405 for (i = 0; i < count; i++)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001406 hdmi_writeb(hdmi, val, HDMI_FC_INVIDCONF);
1407}
1408
Andy Yanb21f4b62014-12-05 14:26:31 +08001409static void hdmi_enable_overflow_interrupts(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001410{
1411 hdmi_writeb(hdmi, 0, HDMI_FC_MASK2);
1412 hdmi_writeb(hdmi, 0, HDMI_IH_MUTE_FC_STAT2);
1413}
1414
Andy Yanb21f4b62014-12-05 14:26:31 +08001415static void hdmi_disable_overflow_interrupts(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001416{
1417 hdmi_writeb(hdmi, HDMI_IH_MUTE_FC_STAT2_OVERFLOW_MASK,
1418 HDMI_IH_MUTE_FC_STAT2);
1419}
1420
Andy Yanb21f4b62014-12-05 14:26:31 +08001421static int dw_hdmi_setup(struct dw_hdmi *hdmi, struct drm_display_mode *mode)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001422{
1423 int ret;
1424
1425 hdmi_disable_overflow_interrupts(hdmi);
1426
1427 hdmi->vic = drm_match_cea_mode(mode);
1428
1429 if (!hdmi->vic) {
1430 dev_dbg(hdmi->dev, "Non-CEA mode used in HDMI\n");
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001431 } else {
1432 dev_dbg(hdmi->dev, "CEA mode used vic=%d\n", hdmi->vic);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001433 }
1434
1435 if ((hdmi->vic == 6) || (hdmi->vic == 7) ||
Andy Yanb5878332014-12-05 14:23:52 +08001436 (hdmi->vic == 21) || (hdmi->vic == 22) ||
1437 (hdmi->vic == 2) || (hdmi->vic == 3) ||
1438 (hdmi->vic == 17) || (hdmi->vic == 18))
Sachin Kamat5a819ed2014-01-28 10:33:16 +05301439 hdmi->hdmi_data.colorimetry = HDMI_COLORIMETRY_ITU_601;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001440 else
Sachin Kamat5a819ed2014-01-28 10:33:16 +05301441 hdmi->hdmi_data.colorimetry = HDMI_COLORIMETRY_ITU_709;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001442
Russell Kingd10ca822015-07-21 11:25:00 +01001443 hdmi->hdmi_data.video_mode.mpixelrepetitionoutput = 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001444 hdmi->hdmi_data.video_mode.mpixelrepetitioninput = 0;
1445
1446 /* TODO: Get input format from IPU (via FB driver interface) */
1447 hdmi->hdmi_data.enc_in_format = RGB;
1448
1449 hdmi->hdmi_data.enc_out_format = RGB;
1450
1451 hdmi->hdmi_data.enc_color_depth = 8;
1452 hdmi->hdmi_data.pix_repet_factor = 0;
1453 hdmi->hdmi_data.hdcp_enable = 0;
1454 hdmi->hdmi_data.video_mode.mdataenablepolarity = true;
1455
1456 /* HDMI Initialization Step B.1 */
1457 hdmi_av_composer(hdmi, mode);
1458
1459 /* HDMI Initializateion Step B.2 */
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001460 ret = hdmi->phy.ops->init(hdmi, hdmi->phy.data, &hdmi->previous_mode);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001461 if (ret)
1462 return ret;
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001463 hdmi->phy.enabled = true;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001464
1465 /* HDMI Initialization Step B.3 */
Andy Yanb21f4b62014-12-05 14:26:31 +08001466 dw_hdmi_enable_video_path(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001467
Russell Kingf709ec02015-07-21 16:09:39 +01001468 if (hdmi->sink_has_audio) {
1469 dev_dbg(hdmi->dev, "sink has audio support\n");
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001470
1471 /* HDMI Initialization Step E - Configure audio */
1472 hdmi_clk_regenerator_update_pixel_clock(hdmi);
1473 hdmi_enable_audio_clk(hdmi);
Russell Kingf709ec02015-07-21 16:09:39 +01001474 }
1475
1476 /* not for DVI mode */
1477 if (hdmi->sink_is_hdmi) {
1478 dev_dbg(hdmi->dev, "%s HDMI mode\n", __func__);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001479
1480 /* HDMI Initialization Step F - Configure AVI InfoFrame */
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001481 hdmi_config_AVI(hdmi, mode);
Russell King05b13422015-07-21 15:35:52 +01001482 } else {
1483 dev_dbg(hdmi->dev, "%s DVI mode\n", __func__);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001484 }
1485
1486 hdmi_video_packetize(hdmi);
1487 hdmi_video_csc(hdmi);
1488 hdmi_video_sample(hdmi);
1489 hdmi_tx_hdcp_config(hdmi);
1490
Andy Yanb21f4b62014-12-05 14:26:31 +08001491 dw_hdmi_clear_overflow(hdmi);
Russell King05b13422015-07-21 15:35:52 +01001492 if (hdmi->cable_plugin && hdmi->sink_is_hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001493 hdmi_enable_overflow_interrupts(hdmi);
1494
1495 return 0;
1496}
1497
1498/* Wait until we are registered to enable interrupts */
Andy Yanb21f4b62014-12-05 14:26:31 +08001499static int dw_hdmi_fb_registered(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001500{
1501 hdmi_writeb(hdmi, HDMI_PHY_I2CM_INT_ADDR_DONE_POL,
1502 HDMI_PHY_I2CM_INT_ADDR);
1503
1504 hdmi_writeb(hdmi, HDMI_PHY_I2CM_CTLINT_ADDR_NAC_POL |
1505 HDMI_PHY_I2CM_CTLINT_ADDR_ARBITRATION_POL,
1506 HDMI_PHY_I2CM_CTLINT_ADDR);
1507
1508 /* enable cable hot plug irq */
Russell Kingaeac23b2015-06-05 13:46:22 +01001509 hdmi_writeb(hdmi, hdmi->phy_mask, HDMI_PHY_MASK0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001510
1511 /* Clear Hotplug interrupts */
Russell Kingaeac23b2015-06-05 13:46:22 +01001512 hdmi_writeb(hdmi, HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE,
1513 HDMI_IH_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001514
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001515 return 0;
1516}
1517
Andy Yanb21f4b62014-12-05 14:26:31 +08001518static void initialize_hdmi_ih_mutes(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001519{
1520 u8 ih_mute;
1521
1522 /*
1523 * Boot up defaults are:
1524 * HDMI_IH_MUTE = 0x03 (disabled)
1525 * HDMI_IH_MUTE_* = 0x00 (enabled)
1526 *
1527 * Disable top level interrupt bits in HDMI block
1528 */
1529 ih_mute = hdmi_readb(hdmi, HDMI_IH_MUTE) |
1530 HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |
1531 HDMI_IH_MUTE_MUTE_ALL_INTERRUPT;
1532
1533 hdmi_writeb(hdmi, ih_mute, HDMI_IH_MUTE);
1534
1535 /* by default mask all interrupts */
1536 hdmi_writeb(hdmi, 0xff, HDMI_VP_MASK);
1537 hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK0);
1538 hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK1);
1539 hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK2);
1540 hdmi_writeb(hdmi, 0xff, HDMI_PHY_MASK0);
1541 hdmi_writeb(hdmi, 0xff, HDMI_PHY_I2CM_INT_ADDR);
1542 hdmi_writeb(hdmi, 0xff, HDMI_PHY_I2CM_CTLINT_ADDR);
1543 hdmi_writeb(hdmi, 0xff, HDMI_AUD_INT);
1544 hdmi_writeb(hdmi, 0xff, HDMI_AUD_SPDIFINT);
1545 hdmi_writeb(hdmi, 0xff, HDMI_AUD_HBR_MASK);
1546 hdmi_writeb(hdmi, 0xff, HDMI_GP_MASK);
1547 hdmi_writeb(hdmi, 0xff, HDMI_A_APIINTMSK);
1548 hdmi_writeb(hdmi, 0xff, HDMI_CEC_MASK);
1549 hdmi_writeb(hdmi, 0xff, HDMI_I2CM_INT);
1550 hdmi_writeb(hdmi, 0xff, HDMI_I2CM_CTLINT);
1551
1552 /* Disable interrupts in the IH_MUTE_* registers */
1553 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT0);
1554 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT1);
1555 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT2);
1556 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_AS_STAT0);
1557 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_PHY_STAT0);
1558 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_I2CM_STAT0);
1559 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_CEC_STAT0);
1560 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_VP_STAT0);
1561 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_I2CMPHY_STAT0);
1562 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_AHBDMAAUD_STAT0);
1563
1564 /* Enable top level interrupt bits in HDMI block */
1565 ih_mute &= ~(HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |
1566 HDMI_IH_MUTE_MUTE_ALL_INTERRUPT);
1567 hdmi_writeb(hdmi, ih_mute, HDMI_IH_MUTE);
1568}
1569
Andy Yanb21f4b62014-12-05 14:26:31 +08001570static void dw_hdmi_poweron(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001571{
Russell King381f05a2015-06-05 15:25:08 +01001572 hdmi->bridge_is_on = true;
Andy Yanb21f4b62014-12-05 14:26:31 +08001573 dw_hdmi_setup(hdmi, &hdmi->previous_mode);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001574}
1575
Andy Yanb21f4b62014-12-05 14:26:31 +08001576static void dw_hdmi_poweroff(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001577{
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001578 if (hdmi->phy.enabled) {
1579 hdmi->phy.ops->disable(hdmi, hdmi->phy.data);
1580 hdmi->phy.enabled = false;
1581 }
1582
Russell King381f05a2015-06-05 15:25:08 +01001583 hdmi->bridge_is_on = false;
1584}
1585
1586static void dw_hdmi_update_power(struct dw_hdmi *hdmi)
1587{
1588 int force = hdmi->force;
1589
1590 if (hdmi->disabled) {
1591 force = DRM_FORCE_OFF;
1592 } else if (force == DRM_FORCE_UNSPECIFIED) {
Russell Kingaeac23b2015-06-05 13:46:22 +01001593 if (hdmi->rxsense)
Russell King381f05a2015-06-05 15:25:08 +01001594 force = DRM_FORCE_ON;
1595 else
1596 force = DRM_FORCE_OFF;
1597 }
1598
1599 if (force == DRM_FORCE_OFF) {
1600 if (hdmi->bridge_is_on)
1601 dw_hdmi_poweroff(hdmi);
1602 } else {
1603 if (!hdmi->bridge_is_on)
1604 dw_hdmi_poweron(hdmi);
1605 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001606}
1607
Russell Kingaeac23b2015-06-05 13:46:22 +01001608/*
1609 * Adjust the detection of RXSENSE according to whether we have a forced
1610 * connection mode enabled, or whether we have been disabled. There is
1611 * no point processing RXSENSE interrupts if we have a forced connection
1612 * state, or DRM has us disabled.
1613 *
1614 * We also disable rxsense interrupts when we think we're disconnected
1615 * to avoid floating TDMS signals giving false rxsense interrupts.
1616 *
1617 * Note: we still need to listen for HPD interrupts even when DRM has us
1618 * disabled so that we can detect a connect event.
1619 */
1620static void dw_hdmi_update_phy_mask(struct dw_hdmi *hdmi)
1621{
1622 u8 old_mask = hdmi->phy_mask;
1623
1624 if (hdmi->force || hdmi->disabled || !hdmi->rxsense)
1625 hdmi->phy_mask |= HDMI_PHY_RX_SENSE;
1626 else
1627 hdmi->phy_mask &= ~HDMI_PHY_RX_SENSE;
1628
1629 if (old_mask != hdmi->phy_mask)
1630 hdmi_writeb(hdmi, hdmi->phy_mask, HDMI_PHY_MASK0);
1631}
1632
Andy Yanb21f4b62014-12-05 14:26:31 +08001633static enum drm_connector_status
1634dw_hdmi_connector_detect(struct drm_connector *connector, bool force)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001635{
Andy Yanb21f4b62014-12-05 14:26:31 +08001636 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
Russell Kingd94905e2013-11-03 22:23:24 +00001637 connector);
Russell King98dbead2014-04-18 10:46:45 +01001638
Russell King381f05a2015-06-05 15:25:08 +01001639 mutex_lock(&hdmi->mutex);
1640 hdmi->force = DRM_FORCE_UNSPECIFIED;
1641 dw_hdmi_update_power(hdmi);
Russell Kingaeac23b2015-06-05 13:46:22 +01001642 dw_hdmi_update_phy_mask(hdmi);
Russell King381f05a2015-06-05 15:25:08 +01001643 mutex_unlock(&hdmi->mutex);
1644
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001645 return hdmi->phy.ops->read_hpd(hdmi, hdmi->phy.data);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001646}
1647
Andy Yanb21f4b62014-12-05 14:26:31 +08001648static int dw_hdmi_connector_get_modes(struct drm_connector *connector)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001649{
Andy Yanb21f4b62014-12-05 14:26:31 +08001650 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001651 connector);
1652 struct edid *edid;
Doug Anderson6c7e66e2015-06-04 11:04:36 -07001653 int ret = 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001654
1655 if (!hdmi->ddc)
1656 return 0;
1657
1658 edid = drm_get_edid(connector, hdmi->ddc);
1659 if (edid) {
1660 dev_dbg(hdmi->dev, "got edid: width[%d] x height[%d]\n",
1661 edid->width_cm, edid->height_cm);
1662
Russell King05b13422015-07-21 15:35:52 +01001663 hdmi->sink_is_hdmi = drm_detect_hdmi_monitor(edid);
Russell Kingf709ec02015-07-21 16:09:39 +01001664 hdmi->sink_has_audio = drm_detect_monitor_audio(edid);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001665 drm_mode_connector_update_edid_property(connector, edid);
1666 ret = drm_add_edid_modes(connector, edid);
Russell Kingf5ce4052013-11-07 16:06:01 +00001667 /* Store the ELD */
1668 drm_edid_to_eld(connector, edid);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001669 kfree(edid);
1670 } else {
1671 dev_dbg(hdmi->dev, "failed to get edid\n");
1672 }
1673
Doug Anderson6c7e66e2015-06-04 11:04:36 -07001674 return ret;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001675}
1676
Andy Yan632d0352014-12-05 14:30:21 +08001677static enum drm_mode_status
1678dw_hdmi_connector_mode_valid(struct drm_connector *connector,
1679 struct drm_display_mode *mode)
1680{
1681 struct dw_hdmi *hdmi = container_of(connector,
1682 struct dw_hdmi, connector);
1683 enum drm_mode_status mode_status = MODE_OK;
1684
Russell King8add4192015-07-22 11:14:00 +01001685 /* We don't support double-clocked modes */
1686 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
1687 return MODE_BAD;
1688
Andy Yan632d0352014-12-05 14:30:21 +08001689 if (hdmi->plat_data->mode_valid)
1690 mode_status = hdmi->plat_data->mode_valid(connector, mode);
1691
1692 return mode_status;
1693}
1694
Russell King381f05a2015-06-05 15:25:08 +01001695static void dw_hdmi_connector_force(struct drm_connector *connector)
1696{
1697 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
1698 connector);
1699
1700 mutex_lock(&hdmi->mutex);
1701 hdmi->force = connector->force;
1702 dw_hdmi_update_power(hdmi);
Russell Kingaeac23b2015-06-05 13:46:22 +01001703 dw_hdmi_update_phy_mask(hdmi);
Russell King381f05a2015-06-05 15:25:08 +01001704 mutex_unlock(&hdmi->mutex);
1705}
1706
Ville Syrjälädae91e42015-12-15 12:21:02 +01001707static const struct drm_connector_funcs dw_hdmi_connector_funcs = {
Mark Yao2c5b2cc2015-11-30 18:33:40 +08001708 .dpms = drm_atomic_helper_connector_dpms,
1709 .fill_modes = drm_helper_probe_single_connector_modes,
1710 .detect = dw_hdmi_connector_detect,
Marek Vasutfdd83262016-10-05 16:31:33 +02001711 .destroy = drm_connector_cleanup,
Mark Yao2c5b2cc2015-11-30 18:33:40 +08001712 .force = dw_hdmi_connector_force,
1713 .reset = drm_atomic_helper_connector_reset,
1714 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1715 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1716};
1717
Ville Syrjälädae91e42015-12-15 12:21:02 +01001718static const struct drm_connector_helper_funcs dw_hdmi_connector_helper_funcs = {
Andy Yanb21f4b62014-12-05 14:26:31 +08001719 .get_modes = dw_hdmi_connector_get_modes,
Andy Yan632d0352014-12-05 14:30:21 +08001720 .mode_valid = dw_hdmi_connector_mode_valid,
Boris Brezillonc2a441f2016-06-07 13:48:15 +02001721 .best_encoder = drm_atomic_helper_best_encoder,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001722};
1723
Laurent Pinchartd2ae94a2017-01-17 10:28:59 +02001724static int dw_hdmi_bridge_attach(struct drm_bridge *bridge)
1725{
1726 struct dw_hdmi *hdmi = bridge->driver_private;
1727 struct drm_encoder *encoder = bridge->encoder;
1728 struct drm_connector *connector = &hdmi->connector;
1729
1730 connector->interlace_allowed = 1;
1731 connector->polled = DRM_CONNECTOR_POLL_HPD;
1732
1733 drm_connector_helper_add(connector, &dw_hdmi_connector_helper_funcs);
1734
1735 drm_connector_init(bridge->dev, connector, &dw_hdmi_connector_funcs,
1736 DRM_MODE_CONNECTOR_HDMIA);
1737
1738 drm_mode_connector_attach_encoder(connector, encoder);
1739
1740 return 0;
1741}
1742
Laurent Pinchartfd30b382017-01-17 10:28:58 +02001743static void dw_hdmi_bridge_mode_set(struct drm_bridge *bridge,
1744 struct drm_display_mode *orig_mode,
1745 struct drm_display_mode *mode)
1746{
1747 struct dw_hdmi *hdmi = bridge->driver_private;
1748
1749 mutex_lock(&hdmi->mutex);
1750
1751 /* Store the display mode for plugin/DKMS poweron events */
1752 memcpy(&hdmi->previous_mode, mode, sizeof(hdmi->previous_mode));
1753
1754 mutex_unlock(&hdmi->mutex);
1755}
1756
1757static void dw_hdmi_bridge_disable(struct drm_bridge *bridge)
1758{
1759 struct dw_hdmi *hdmi = bridge->driver_private;
1760
1761 mutex_lock(&hdmi->mutex);
1762 hdmi->disabled = true;
1763 dw_hdmi_update_power(hdmi);
1764 dw_hdmi_update_phy_mask(hdmi);
1765 mutex_unlock(&hdmi->mutex);
1766}
1767
1768static void dw_hdmi_bridge_enable(struct drm_bridge *bridge)
1769{
1770 struct dw_hdmi *hdmi = bridge->driver_private;
1771
1772 mutex_lock(&hdmi->mutex);
1773 hdmi->disabled = false;
1774 dw_hdmi_update_power(hdmi);
1775 dw_hdmi_update_phy_mask(hdmi);
1776 mutex_unlock(&hdmi->mutex);
1777}
1778
Ville Syrjälädae91e42015-12-15 12:21:02 +01001779static const struct drm_bridge_funcs dw_hdmi_bridge_funcs = {
Laurent Pinchartd2ae94a2017-01-17 10:28:59 +02001780 .attach = dw_hdmi_bridge_attach,
Andy Yanb21f4b62014-12-05 14:26:31 +08001781 .enable = dw_hdmi_bridge_enable,
1782 .disable = dw_hdmi_bridge_disable,
Andy Yanb21f4b62014-12-05 14:26:31 +08001783 .mode_set = dw_hdmi_bridge_mode_set,
Andy Yan3d1b35a2014-12-05 14:25:05 +08001784};
1785
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03001786static irqreturn_t dw_hdmi_i2c_irq(struct dw_hdmi *hdmi)
1787{
1788 struct dw_hdmi_i2c *i2c = hdmi->i2c;
1789 unsigned int stat;
1790
1791 stat = hdmi_readb(hdmi, HDMI_IH_I2CM_STAT0);
1792 if (!stat)
1793 return IRQ_NONE;
1794
1795 hdmi_writeb(hdmi, stat, HDMI_IH_I2CM_STAT0);
1796
1797 i2c->stat = stat;
1798
1799 complete(&i2c->cmp);
1800
1801 return IRQ_HANDLED;
1802}
1803
Andy Yanb21f4b62014-12-05 14:26:31 +08001804static irqreturn_t dw_hdmi_hardirq(int irq, void *dev_id)
Russell Kingd94905e2013-11-03 22:23:24 +00001805{
Andy Yanb21f4b62014-12-05 14:26:31 +08001806 struct dw_hdmi *hdmi = dev_id;
Russell Kingd94905e2013-11-03 22:23:24 +00001807 u8 intr_stat;
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03001808 irqreturn_t ret = IRQ_NONE;
1809
1810 if (hdmi->i2c)
1811 ret = dw_hdmi_i2c_irq(hdmi);
Russell Kingd94905e2013-11-03 22:23:24 +00001812
1813 intr_stat = hdmi_readb(hdmi, HDMI_IH_PHY_STAT0);
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03001814 if (intr_stat) {
Russell Kingd94905e2013-11-03 22:23:24 +00001815 hdmi_writeb(hdmi, ~0, HDMI_IH_MUTE_PHY_STAT0);
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03001816 return IRQ_WAKE_THREAD;
1817 }
Russell Kingd94905e2013-11-03 22:23:24 +00001818
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03001819 return ret;
Russell Kingd94905e2013-11-03 22:23:24 +00001820}
1821
Andy Yanb21f4b62014-12-05 14:26:31 +08001822static irqreturn_t dw_hdmi_irq(int irq, void *dev_id)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001823{
Andy Yanb21f4b62014-12-05 14:26:31 +08001824 struct dw_hdmi *hdmi = dev_id;
Russell Kingaeac23b2015-06-05 13:46:22 +01001825 u8 intr_stat, phy_int_pol, phy_pol_mask, phy_stat;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001826
1827 intr_stat = hdmi_readb(hdmi, HDMI_IH_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001828 phy_int_pol = hdmi_readb(hdmi, HDMI_PHY_POL0);
Russell Kingaeac23b2015-06-05 13:46:22 +01001829 phy_stat = hdmi_readb(hdmi, HDMI_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001830
Russell Kingaeac23b2015-06-05 13:46:22 +01001831 phy_pol_mask = 0;
1832 if (intr_stat & HDMI_IH_PHY_STAT0_HPD)
1833 phy_pol_mask |= HDMI_PHY_HPD;
1834 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE0)
1835 phy_pol_mask |= HDMI_PHY_RX_SENSE0;
1836 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE1)
1837 phy_pol_mask |= HDMI_PHY_RX_SENSE1;
1838 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE2)
1839 phy_pol_mask |= HDMI_PHY_RX_SENSE2;
1840 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE3)
1841 phy_pol_mask |= HDMI_PHY_RX_SENSE3;
1842
1843 if (phy_pol_mask)
1844 hdmi_modb(hdmi, ~phy_int_pol, phy_pol_mask, HDMI_PHY_POL0);
1845
1846 /*
1847 * RX sense tells us whether the TDMS transmitters are detecting
1848 * load - in other words, there's something listening on the
1849 * other end of the link. Use this to decide whether we should
1850 * power on the phy as HPD may be toggled by the sink to merely
1851 * ask the source to re-read the EDID.
1852 */
1853 if (intr_stat &
1854 (HDMI_IH_PHY_STAT0_RX_SENSE | HDMI_IH_PHY_STAT0_HPD)) {
Russell Kingb872a8e2015-06-05 12:22:46 +01001855 mutex_lock(&hdmi->mutex);
Russell Kingaeac23b2015-06-05 13:46:22 +01001856 if (!hdmi->disabled && !hdmi->force) {
1857 /*
1858 * If the RX sense status indicates we're disconnected,
1859 * clear the software rxsense status.
1860 */
1861 if (!(phy_stat & HDMI_PHY_RX_SENSE))
1862 hdmi->rxsense = false;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001863
Russell Kingaeac23b2015-06-05 13:46:22 +01001864 /*
1865 * Only set the software rxsense status when both
1866 * rxsense and hpd indicates we're connected.
1867 * This avoids what seems to be bad behaviour in
1868 * at least iMX6S versions of the phy.
1869 */
1870 if (phy_stat & HDMI_PHY_HPD)
1871 hdmi->rxsense = true;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001872
Russell Kingaeac23b2015-06-05 13:46:22 +01001873 dw_hdmi_update_power(hdmi);
1874 dw_hdmi_update_phy_mask(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001875 }
Russell Kingb872a8e2015-06-05 12:22:46 +01001876 mutex_unlock(&hdmi->mutex);
Russell Kingaeac23b2015-06-05 13:46:22 +01001877 }
1878
1879 if (intr_stat & HDMI_IH_PHY_STAT0_HPD) {
1880 dev_dbg(hdmi->dev, "EVENT=%s\n",
1881 phy_int_pol & HDMI_PHY_HPD ? "plugin" : "plugout");
Laurent Pinchartba5d7e62017-01-17 10:28:56 +02001882 if (hdmi->bridge.dev)
1883 drm_helper_hpd_irq_event(hdmi->bridge.dev);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001884 }
1885
1886 hdmi_writeb(hdmi, intr_stat, HDMI_IH_PHY_STAT0);
Russell Kingaeac23b2015-06-05 13:46:22 +01001887 hdmi_writeb(hdmi, ~(HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE),
1888 HDMI_IH_MUTE_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001889
1890 return IRQ_HANDLED;
1891}
1892
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02001893static const struct dw_hdmi_phy_data dw_hdmi_phys[] = {
1894 {
1895 .type = DW_HDMI_PHY_DWC_HDMI_TX_PHY,
1896 .name = "DWC HDMI TX PHY",
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02001897 .gen = 1,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02001898 }, {
1899 .type = DW_HDMI_PHY_DWC_MHL_PHY_HEAC,
1900 .name = "DWC MHL PHY + HEAC PHY",
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02001901 .gen = 2,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02001902 .has_svsret = true,
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001903 .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02001904 }, {
1905 .type = DW_HDMI_PHY_DWC_MHL_PHY,
1906 .name = "DWC MHL PHY",
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02001907 .gen = 2,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02001908 .has_svsret = true,
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001909 .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02001910 }, {
1911 .type = DW_HDMI_PHY_DWC_HDMI_3D_TX_PHY_HEAC,
1912 .name = "DWC HDMI 3D TX PHY + HEAC PHY",
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02001913 .gen = 2,
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001914 .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02001915 }, {
1916 .type = DW_HDMI_PHY_DWC_HDMI_3D_TX_PHY,
1917 .name = "DWC HDMI 3D TX PHY",
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02001918 .gen = 2,
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001919 .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02001920 }, {
1921 .type = DW_HDMI_PHY_DWC_HDMI20_TX_PHY,
1922 .name = "DWC HDMI 2.0 TX PHY",
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02001923 .gen = 2,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02001924 .has_svsret = true,
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001925 }, {
1926 .type = DW_HDMI_PHY_VENDOR_PHY,
1927 .name = "Vendor PHY",
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02001928 }
1929};
1930
1931static int dw_hdmi_detect_phy(struct dw_hdmi *hdmi)
1932{
1933 unsigned int i;
1934 u8 phy_type;
1935
1936 phy_type = hdmi_readb(hdmi, HDMI_CONFIG2_ID);
1937
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001938 if (phy_type == DW_HDMI_PHY_VENDOR_PHY) {
1939 /* Vendor PHYs require support from the glue layer. */
1940 if (!hdmi->plat_data->phy_ops || !hdmi->plat_data->phy_name) {
1941 dev_err(hdmi->dev,
1942 "Vendor HDMI PHY not supported by glue layer\n");
1943 return -ENODEV;
1944 }
1945
1946 hdmi->phy.ops = hdmi->plat_data->phy_ops;
1947 hdmi->phy.data = hdmi->plat_data->phy_data;
1948 hdmi->phy.name = hdmi->plat_data->phy_name;
1949 return 0;
1950 }
1951
1952 /* Synopsys PHYs are handled internally. */
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02001953 for (i = 0; i < ARRAY_SIZE(dw_hdmi_phys); ++i) {
1954 if (dw_hdmi_phys[i].type == phy_type) {
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001955 hdmi->phy.ops = &dw_hdmi_synopsys_phy_ops;
1956 hdmi->phy.name = dw_hdmi_phys[i].name;
1957 hdmi->phy.data = (void *)&dw_hdmi_phys[i];
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001958
1959 if (!dw_hdmi_phys[i].configure &&
1960 !hdmi->plat_data->configure_phy) {
1961 dev_err(hdmi->dev, "%s requires platform support\n",
1962 hdmi->phy.name);
1963 return -ENODEV;
1964 }
1965
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02001966 return 0;
1967 }
1968 }
1969
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001970 dev_err(hdmi->dev, "Unsupported HDMI PHY type (%02x)\n", phy_type);
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02001971 return -ENODEV;
1972}
1973
Neil Armstrong80e2f972017-03-03 19:20:06 +02001974static const struct regmap_config hdmi_regmap_8bit_config = {
1975 .reg_bits = 32,
1976 .val_bits = 8,
1977 .reg_stride = 1,
1978 .max_register = HDMI_I2CM_FS_SCL_LCNT_0_ADDR,
1979};
1980
1981static const struct regmap_config hdmi_regmap_32bit_config = {
1982 .reg_bits = 32,
1983 .val_bits = 32,
1984 .reg_stride = 4,
1985 .max_register = HDMI_I2CM_FS_SCL_LCNT_0_ADDR << 2,
1986};
1987
Laurent Pinchart69497eb2017-01-17 10:29:00 +02001988static struct dw_hdmi *
1989__dw_hdmi_probe(struct platform_device *pdev,
1990 const struct dw_hdmi_plat_data *plat_data)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001991{
Laurent Pinchartc6081192017-01-17 10:28:57 +02001992 struct device *dev = &pdev->dev;
Russell King17b50012013-11-03 11:23:34 +00001993 struct device_node *np = dev->of_node;
Russell King7ed6c662013-11-07 16:01:45 +00001994 struct platform_device_info pdevinfo;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001995 struct device_node *ddc_node;
Andy Yanb21f4b62014-12-05 14:26:31 +08001996 struct dw_hdmi *hdmi;
Neil Armstrong80e2f972017-03-03 19:20:06 +02001997 struct resource *iores = NULL;
Laurent Pinchartc6081192017-01-17 10:28:57 +02001998 int irq;
Andy Yan3d1b35a2014-12-05 14:25:05 +08001999 int ret;
Andy Yan0cd9d142014-12-05 14:28:24 +08002000 u32 val = 1;
Laurent Pinchart0527e122017-01-17 10:29:03 +02002001 u8 prod_id0;
2002 u8 prod_id1;
Kuninori Morimoto2761ba62016-11-08 01:00:57 +00002003 u8 config0;
Laurent Pinchart0c674942017-01-17 10:29:04 +02002004 u8 config3;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002005
Russell King17b50012013-11-03 11:23:34 +00002006 hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002007 if (!hdmi)
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002008 return ERR_PTR(-ENOMEM);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002009
Andy Yan3d1b35a2014-12-05 14:25:05 +08002010 hdmi->plat_data = plat_data;
Russell King17b50012013-11-03 11:23:34 +00002011 hdmi->dev = dev;
Russell King40678382013-11-07 15:35:06 +00002012 hdmi->sample_rate = 48000;
Russell Kingb872a8e2015-06-05 12:22:46 +01002013 hdmi->disabled = true;
Russell Kingaeac23b2015-06-05 13:46:22 +01002014 hdmi->rxsense = true;
2015 hdmi->phy_mask = (u8)~(HDMI_PHY_HPD | HDMI_PHY_RX_SENSE);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002016
Russell Kingb872a8e2015-06-05 12:22:46 +01002017 mutex_init(&hdmi->mutex);
Russell King6bcf4952015-02-02 11:01:08 +00002018 mutex_init(&hdmi->audio_mutex);
Russell Kingb90120a2015-03-27 12:59:58 +00002019 spin_lock_init(&hdmi->audio_lock);
Russell King6bcf4952015-02-02 11:01:08 +00002020
Philipp Zabelb5d45902014-03-05 10:20:56 +01002021 ddc_node = of_parse_phandle(np, "ddc-i2c-bus", 0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002022 if (ddc_node) {
Vladimir Zapolskiy9f04a1f2016-08-16 23:26:43 +03002023 hdmi->ddc = of_get_i2c_adapter_by_node(ddc_node);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002024 of_node_put(ddc_node);
Andy Yanc2c38482014-12-05 14:24:28 +08002025 if (!hdmi->ddc) {
2026 dev_dbg(hdmi->dev, "failed to read ddc node\n");
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002027 return ERR_PTR(-EPROBE_DEFER);
Andy Yanc2c38482014-12-05 14:24:28 +08002028 }
2029
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002030 } else {
2031 dev_dbg(hdmi->dev, "no ddc property found\n");
2032 }
2033
Neil Armstrong80e2f972017-03-03 19:20:06 +02002034 if (!plat_data->regm) {
2035 const struct regmap_config *reg_config;
2036
2037 of_property_read_u32(np, "reg-io-width", &val);
2038 switch (val) {
2039 case 4:
2040 reg_config = &hdmi_regmap_32bit_config;
2041 hdmi->reg_shift = 2;
2042 break;
2043 case 1:
2044 reg_config = &hdmi_regmap_8bit_config;
2045 break;
2046 default:
2047 dev_err(dev, "reg-io-width must be 1 or 4\n");
2048 return ERR_PTR(-EINVAL);
2049 }
2050
2051 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2052 hdmi->regs = devm_ioremap_resource(dev, iores);
2053 if (IS_ERR(hdmi->regs)) {
2054 ret = PTR_ERR(hdmi->regs);
2055 goto err_res;
2056 }
2057
2058 hdmi->regm = devm_regmap_init_mmio(dev, hdmi->regs, reg_config);
2059 if (IS_ERR(hdmi->regm)) {
2060 dev_err(dev, "Failed to configure regmap\n");
2061 ret = PTR_ERR(hdmi->regm);
2062 goto err_res;
2063 }
2064 } else {
2065 hdmi->regm = plat_data->regm;
Vladimir Zapolskiy9f04a1f2016-08-16 23:26:43 +03002066 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002067
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002068 hdmi->isfr_clk = devm_clk_get(hdmi->dev, "isfr");
2069 if (IS_ERR(hdmi->isfr_clk)) {
2070 ret = PTR_ERR(hdmi->isfr_clk);
Andy Yanb5878332014-12-05 14:23:52 +08002071 dev_err(hdmi->dev, "Unable to get HDMI isfr clk: %d\n", ret);
Vladimir Zapolskiy9f04a1f2016-08-16 23:26:43 +03002072 goto err_res;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002073 }
2074
2075 ret = clk_prepare_enable(hdmi->isfr_clk);
2076 if (ret) {
Andy Yanb5878332014-12-05 14:23:52 +08002077 dev_err(hdmi->dev, "Cannot enable HDMI isfr clock: %d\n", ret);
Vladimir Zapolskiy9f04a1f2016-08-16 23:26:43 +03002078 goto err_res;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002079 }
2080
2081 hdmi->iahb_clk = devm_clk_get(hdmi->dev, "iahb");
2082 if (IS_ERR(hdmi->iahb_clk)) {
2083 ret = PTR_ERR(hdmi->iahb_clk);
Andy Yanb5878332014-12-05 14:23:52 +08002084 dev_err(hdmi->dev, "Unable to get HDMI iahb clk: %d\n", ret);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002085 goto err_isfr;
2086 }
2087
2088 ret = clk_prepare_enable(hdmi->iahb_clk);
2089 if (ret) {
Andy Yanb5878332014-12-05 14:23:52 +08002090 dev_err(hdmi->dev, "Cannot enable HDMI iahb clock: %d\n", ret);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002091 goto err_isfr;
2092 }
2093
2094 /* Product and revision IDs */
Laurent Pinchartbe41fc52017-01-17 10:29:05 +02002095 hdmi->version = (hdmi_readb(hdmi, HDMI_DESIGN_ID) << 8)
2096 | (hdmi_readb(hdmi, HDMI_REVISION_ID) << 0);
Laurent Pinchart0527e122017-01-17 10:29:03 +02002097 prod_id0 = hdmi_readb(hdmi, HDMI_PRODUCT_ID0);
2098 prod_id1 = hdmi_readb(hdmi, HDMI_PRODUCT_ID1);
2099
2100 if (prod_id0 != HDMI_PRODUCT_ID0_HDMI_TX ||
2101 (prod_id1 & ~HDMI_PRODUCT_ID1_HDCP) != HDMI_PRODUCT_ID1_HDMI_TX) {
2102 dev_err(dev, "Unsupported HDMI controller (%04x:%02x:%02x)\n",
Laurent Pinchartbe41fc52017-01-17 10:29:05 +02002103 hdmi->version, prod_id0, prod_id1);
Laurent Pinchart0527e122017-01-17 10:29:03 +02002104 ret = -ENODEV;
2105 goto err_iahb;
2106 }
2107
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002108 ret = dw_hdmi_detect_phy(hdmi);
2109 if (ret < 0)
2110 goto err_iahb;
2111
2112 dev_info(dev, "Detected HDMI TX controller v%x.%03x %s HDCP (%s)\n",
Laurent Pinchartbe41fc52017-01-17 10:29:05 +02002113 hdmi->version >> 12, hdmi->version & 0xfff,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002114 prod_id1 & HDMI_PRODUCT_ID1_HDCP ? "with" : "without",
Laurent Pinchartf1585f62017-03-06 01:36:15 +02002115 hdmi->phy.name);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002116
2117 initialize_hdmi_ih_mutes(hdmi);
2118
Laurent Pinchartc6081192017-01-17 10:28:57 +02002119 irq = platform_get_irq(pdev, 0);
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002120 if (irq < 0) {
2121 ret = irq;
Laurent Pinchartc6081192017-01-17 10:28:57 +02002122 goto err_iahb;
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002123 }
Laurent Pinchartc6081192017-01-17 10:28:57 +02002124
Philipp Zabel639a2022015-01-07 13:43:50 +01002125 ret = devm_request_threaded_irq(dev, irq, dw_hdmi_hardirq,
2126 dw_hdmi_irq, IRQF_SHARED,
2127 dev_name(dev), hdmi);
2128 if (ret)
Fabio Estevamb33ef612015-01-27 10:54:12 -02002129 goto err_iahb;
Philipp Zabel639a2022015-01-07 13:43:50 +01002130
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002131 /*
2132 * To prevent overflows in HDMI_IH_FC_STAT2, set the clk regenerator
2133 * N and cts values before enabling phy
2134 */
2135 hdmi_init_clk_regenerator(hdmi);
2136
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002137 /* If DDC bus is not specified, try to register HDMI I2C bus */
2138 if (!hdmi->ddc) {
2139 hdmi->ddc = dw_hdmi_i2c_adapter(hdmi);
2140 if (IS_ERR(hdmi->ddc))
2141 hdmi->ddc = NULL;
2142 }
2143
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002144 /*
2145 * Configure registers related to HDMI interrupt
2146 * generation before registering IRQ.
2147 */
Russell Kingaeac23b2015-06-05 13:46:22 +01002148 hdmi_writeb(hdmi, HDMI_PHY_HPD | HDMI_PHY_RX_SENSE, HDMI_PHY_POL0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002149
2150 /* Clear Hotplug interrupts */
Russell Kingaeac23b2015-06-05 13:46:22 +01002151 hdmi_writeb(hdmi, HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE,
2152 HDMI_IH_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002153
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002154 hdmi->bridge.driver_private = hdmi;
2155 hdmi->bridge.funcs = &dw_hdmi_bridge_funcs;
Arnd Bergmannd5ad7842017-01-23 13:20:38 +01002156#ifdef CONFIG_OF
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002157 hdmi->bridge.of_node = pdev->dev.of_node;
Arnd Bergmannd5ad7842017-01-23 13:20:38 +01002158#endif
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002159
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002160 ret = dw_hdmi_fb_registered(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002161 if (ret)
2162 goto err_iahb;
2163
Russell Kingd94905e2013-11-03 22:23:24 +00002164 /* Unmute interrupts */
Russell Kingaeac23b2015-06-05 13:46:22 +01002165 hdmi_writeb(hdmi, ~(HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE),
2166 HDMI_IH_MUTE_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002167
Russell King7ed6c662013-11-07 16:01:45 +00002168 memset(&pdevinfo, 0, sizeof(pdevinfo));
2169 pdevinfo.parent = dev;
2170 pdevinfo.id = PLATFORM_DEVID_AUTO;
2171
Kuninori Morimoto2761ba62016-11-08 01:00:57 +00002172 config0 = hdmi_readb(hdmi, HDMI_CONFIG0_ID);
Laurent Pinchart0c674942017-01-17 10:29:04 +02002173 config3 = hdmi_readb(hdmi, HDMI_CONFIG3_ID);
Kuninori Morimoto2761ba62016-11-08 01:00:57 +00002174
Neil Armstrong80e2f972017-03-03 19:20:06 +02002175 if (iores && config3 & HDMI_CONFIG3_AHBAUDDMA) {
Kuninori Morimoto2761ba62016-11-08 01:00:57 +00002176 struct dw_hdmi_audio_data audio;
2177
Russell King7ed6c662013-11-07 16:01:45 +00002178 audio.phys = iores->start;
2179 audio.base = hdmi->regs;
2180 audio.irq = irq;
2181 audio.hdmi = hdmi;
Russell Kingf5ce4052013-11-07 16:06:01 +00002182 audio.eld = hdmi->connector.eld;
Russell King7ed6c662013-11-07 16:01:45 +00002183
2184 pdevinfo.name = "dw-hdmi-ahb-audio";
2185 pdevinfo.data = &audio;
2186 pdevinfo.size_data = sizeof(audio);
2187 pdevinfo.dma_mask = DMA_BIT_MASK(32);
2188 hdmi->audio = platform_device_register_full(&pdevinfo);
Kuninori Morimoto2761ba62016-11-08 01:00:57 +00002189 } else if (config0 & HDMI_CONFIG0_I2S) {
2190 struct dw_hdmi_i2s_audio_data audio;
2191
2192 audio.hdmi = hdmi;
2193 audio.write = hdmi_writeb;
2194 audio.read = hdmi_readb;
2195
2196 pdevinfo.name = "dw-hdmi-i2s-audio";
2197 pdevinfo.data = &audio;
2198 pdevinfo.size_data = sizeof(audio);
2199 pdevinfo.dma_mask = DMA_BIT_MASK(32);
2200 hdmi->audio = platform_device_register_full(&pdevinfo);
Russell King7ed6c662013-11-07 16:01:45 +00002201 }
2202
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002203 /* Reset HDMI DDC I2C master controller and mute I2CM interrupts */
2204 if (hdmi->i2c)
2205 dw_hdmi_i2c_init(hdmi);
2206
Laurent Pinchartc6081192017-01-17 10:28:57 +02002207 platform_set_drvdata(pdev, hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002208
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002209 return hdmi;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002210
2211err_iahb:
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002212 if (hdmi->i2c) {
2213 i2c_del_adapter(&hdmi->i2c->adap);
2214 hdmi->ddc = NULL;
2215 }
2216
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002217 clk_disable_unprepare(hdmi->iahb_clk);
2218err_isfr:
2219 clk_disable_unprepare(hdmi->isfr_clk);
Vladimir Zapolskiy9f04a1f2016-08-16 23:26:43 +03002220err_res:
2221 i2c_put_adapter(hdmi->ddc);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002222
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002223 return ERR_PTR(ret);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002224}
2225
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002226static void __dw_hdmi_remove(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002227{
Russell King7ed6c662013-11-07 16:01:45 +00002228 if (hdmi->audio && !IS_ERR(hdmi->audio))
2229 platform_device_unregister(hdmi->audio);
2230
Russell Kingd94905e2013-11-03 22:23:24 +00002231 /* Disable all interrupts */
2232 hdmi_writeb(hdmi, ~0, HDMI_IH_MUTE_PHY_STAT0);
2233
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002234 clk_disable_unprepare(hdmi->iahb_clk);
2235 clk_disable_unprepare(hdmi->isfr_clk);
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002236
2237 if (hdmi->i2c)
2238 i2c_del_adapter(&hdmi->i2c->adap);
2239 else
2240 i2c_put_adapter(hdmi->ddc);
Russell King17b50012013-11-03 11:23:34 +00002241}
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002242
2243/* -----------------------------------------------------------------------------
2244 * Probe/remove API, used from platforms based on the DRM bridge API.
2245 */
2246int dw_hdmi_probe(struct platform_device *pdev,
2247 const struct dw_hdmi_plat_data *plat_data)
2248{
2249 struct dw_hdmi *hdmi;
2250 int ret;
2251
2252 hdmi = __dw_hdmi_probe(pdev, plat_data);
2253 if (IS_ERR(hdmi))
2254 return PTR_ERR(hdmi);
2255
2256 ret = drm_bridge_add(&hdmi->bridge);
2257 if (ret < 0) {
2258 __dw_hdmi_remove(hdmi);
2259 return ret;
2260 }
2261
2262 return 0;
2263}
2264EXPORT_SYMBOL_GPL(dw_hdmi_probe);
2265
2266void dw_hdmi_remove(struct platform_device *pdev)
2267{
2268 struct dw_hdmi *hdmi = platform_get_drvdata(pdev);
2269
2270 drm_bridge_remove(&hdmi->bridge);
2271
2272 __dw_hdmi_remove(hdmi);
2273}
2274EXPORT_SYMBOL_GPL(dw_hdmi_remove);
2275
2276/* -----------------------------------------------------------------------------
2277 * Bind/unbind API, used from platforms based on the component framework.
2278 */
2279int dw_hdmi_bind(struct platform_device *pdev, struct drm_encoder *encoder,
2280 const struct dw_hdmi_plat_data *plat_data)
2281{
2282 struct dw_hdmi *hdmi;
2283 int ret;
2284
2285 hdmi = __dw_hdmi_probe(pdev, plat_data);
2286 if (IS_ERR(hdmi))
2287 return PTR_ERR(hdmi);
2288
2289 ret = drm_bridge_attach(encoder, &hdmi->bridge, NULL);
2290 if (ret) {
2291 dw_hdmi_remove(pdev);
2292 DRM_ERROR("Failed to initialize bridge with drm\n");
2293 return ret;
2294 }
2295
2296 return 0;
2297}
2298EXPORT_SYMBOL_GPL(dw_hdmi_bind);
2299
2300void dw_hdmi_unbind(struct device *dev)
2301{
2302 struct dw_hdmi *hdmi = dev_get_drvdata(dev);
2303
2304 __dw_hdmi_remove(hdmi);
2305}
Andy Yanb21f4b62014-12-05 14:26:31 +08002306EXPORT_SYMBOL_GPL(dw_hdmi_unbind);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002307
2308MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
Andy Yan3d1b35a2014-12-05 14:25:05 +08002309MODULE_AUTHOR("Andy Yan <andy.yan@rock-chips.com>");
2310MODULE_AUTHOR("Yakir Yang <ykk@rock-chips.com>");
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002311MODULE_AUTHOR("Vladimir Zapolskiy <vladimir_zapolskiy@mentor.com>");
Andy Yanb21f4b62014-12-05 14:26:31 +08002312MODULE_DESCRIPTION("DW HDMI transmitter driver");
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002313MODULE_LICENSE("GPL");
Andy Yanb21f4b62014-12-05 14:26:31 +08002314MODULE_ALIAS("platform:dw-hdmi");