blob: 67b4af0413807c0d06a38b0eeceb047152d32b35 [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
Neil Armstrongdef23aa2017-04-04 14:31:57 +020033#include <uapi/linux/media-bus-format.h>
34#include <uapi/linux/videodev2.h>
35
Thierry Reding248a86f2015-11-24 17:52:58 +010036#include "dw-hdmi.h"
37#include "dw-hdmi-audio.h"
Fabio Estevam9aaf8802013-11-29 08:46:32 -020038
Russell Kinge84b8d72017-07-31 15:29:41 +010039#include <media/cec-notifier.h>
40
Nickey Yang94bb4dc2017-03-20 10:57:31 +080041#define DDC_SEGMENT_ADDR 0x30
Russell Kinge84b8d72017-07-31 15:29:41 +010042
Fabio Estevam9aaf8802013-11-29 08:46:32 -020043#define HDMI_EDID_LEN 512
44
Fabio Estevam9aaf8802013-11-29 08:46:32 -020045enum hdmi_datamap {
46 RGB444_8B = 0x01,
47 RGB444_10B = 0x03,
48 RGB444_12B = 0x05,
49 RGB444_16B = 0x07,
50 YCbCr444_8B = 0x09,
51 YCbCr444_10B = 0x0B,
52 YCbCr444_12B = 0x0D,
53 YCbCr444_16B = 0x0F,
54 YCbCr422_8B = 0x16,
55 YCbCr422_10B = 0x14,
56 YCbCr422_12B = 0x12,
57};
58
Fabio Estevam9aaf8802013-11-29 08:46:32 -020059static const u16 csc_coeff_default[3][4] = {
60 { 0x2000, 0x0000, 0x0000, 0x0000 },
61 { 0x0000, 0x2000, 0x0000, 0x0000 },
62 { 0x0000, 0x0000, 0x2000, 0x0000 }
63};
64
65static const u16 csc_coeff_rgb_out_eitu601[3][4] = {
66 { 0x2000, 0x6926, 0x74fd, 0x010e },
67 { 0x2000, 0x2cdd, 0x0000, 0x7e9a },
68 { 0x2000, 0x0000, 0x38b4, 0x7e3b }
69};
70
71static const u16 csc_coeff_rgb_out_eitu709[3][4] = {
72 { 0x2000, 0x7106, 0x7a02, 0x00a7 },
73 { 0x2000, 0x3264, 0x0000, 0x7e6d },
74 { 0x2000, 0x0000, 0x3b61, 0x7e25 }
75};
76
77static const u16 csc_coeff_rgb_in_eitu601[3][4] = {
78 { 0x2591, 0x1322, 0x074b, 0x0000 },
79 { 0x6535, 0x2000, 0x7acc, 0x0200 },
80 { 0x6acd, 0x7534, 0x2000, 0x0200 }
81};
82
83static const u16 csc_coeff_rgb_in_eitu709[3][4] = {
84 { 0x2dc5, 0x0d9b, 0x049e, 0x0000 },
85 { 0x62f0, 0x2000, 0x7d11, 0x0200 },
86 { 0x6756, 0x78ab, 0x2000, 0x0200 }
87};
88
89struct hdmi_vmode {
Fabio Estevam9aaf8802013-11-29 08:46:32 -020090 bool mdataenablepolarity;
91
92 unsigned int mpixelclock;
93 unsigned int mpixelrepetitioninput;
94 unsigned int mpixelrepetitionoutput;
95};
96
97struct hdmi_data_info {
Neil Armstrongdef23aa2017-04-04 14:31:57 +020098 unsigned int enc_in_bus_format;
99 unsigned int enc_out_bus_format;
100 unsigned int enc_in_encoding;
101 unsigned int enc_out_encoding;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200102 unsigned int pix_repet_factor;
103 unsigned int hdcp_enable;
104 struct hdmi_vmode video_mode;
105};
106
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +0300107struct dw_hdmi_i2c {
108 struct i2c_adapter adap;
109
110 struct mutex lock; /* used to serialize data transfers */
111 struct completion cmp;
112 u8 stat;
113
114 u8 slave_reg;
115 bool is_regaddr;
Nickey Yang94bb4dc2017-03-20 10:57:31 +0800116 bool is_segment;
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +0300117};
118
Laurent Pinchartfaba6c32017-01-17 10:29:06 +0200119struct dw_hdmi_phy_data {
120 enum dw_hdmi_phy_type type;
121 const char *name;
Laurent Pinchartb0e583e2017-03-06 01:35:39 +0200122 unsigned int gen;
Laurent Pinchartfaba6c32017-01-17 10:29:06 +0200123 bool has_svsret;
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +0200124 int (*configure)(struct dw_hdmi *hdmi,
125 const struct dw_hdmi_plat_data *pdata,
126 unsigned long mpixelclock);
Laurent Pinchartfaba6c32017-01-17 10:29:06 +0200127};
128
Andy Yanb21f4b62014-12-05 14:26:31 +0800129struct dw_hdmi {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200130 struct drm_connector connector;
Laurent Pinchart70c963e2017-01-17 10:28:54 +0200131 struct drm_bridge bridge;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200132
Laurent Pinchartbe41fc52017-01-17 10:29:05 +0200133 unsigned int version;
134
135 struct platform_device *audio;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200136 struct device *dev;
137 struct clk *isfr_clk;
138 struct clk *iahb_clk;
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +0300139 struct dw_hdmi_i2c *i2c;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200140
141 struct hdmi_data_info hdmi_data;
Andy Yanb21f4b62014-12-05 14:26:31 +0800142 const struct dw_hdmi_plat_data *plat_data;
143
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200144 int vic;
145
146 u8 edid[HDMI_EDID_LEN];
147 bool cable_plugin;
148
Laurent Pinchartf1585f62017-03-06 01:36:15 +0200149 struct {
150 const struct dw_hdmi_phy_ops *ops;
151 const char *name;
152 void *data;
153 bool enabled;
154 } phy;
Laurent Pinchartfaba6c32017-01-17 10:29:06 +0200155
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200156 struct drm_display_mode previous_mode;
157
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200158 struct i2c_adapter *ddc;
159 void __iomem *regs;
Russell King05b13422015-07-21 15:35:52 +0100160 bool sink_is_hdmi;
Russell Kingf709ec02015-07-21 16:09:39 +0100161 bool sink_has_audio;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200162
Russell Kingb872a8e2015-06-05 12:22:46 +0100163 struct mutex mutex; /* for state below and previous_mode */
Russell King381f05a2015-06-05 15:25:08 +0100164 enum drm_connector_force force; /* mutex-protected force state */
Russell Kingb872a8e2015-06-05 12:22:46 +0100165 bool disabled; /* DRM has disabled our bridge */
Russell King381f05a2015-06-05 15:25:08 +0100166 bool bridge_is_on; /* indicates the bridge is on */
Russell Kingaeac23b2015-06-05 13:46:22 +0100167 bool rxsense; /* rxsense state */
168 u8 phy_mask; /* desired phy int mask settings */
Russell Kingb872a8e2015-06-05 12:22:46 +0100169
Russell Kingb90120a2015-03-27 12:59:58 +0000170 spinlock_t audio_lock;
Russell King6bcf4952015-02-02 11:01:08 +0000171 struct mutex audio_mutex;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200172 unsigned int sample_rate;
Russell Kingb90120a2015-03-27 12:59:58 +0000173 unsigned int audio_cts;
174 unsigned int audio_n;
175 bool audio_enable;
Andy Yan0cd9d142014-12-05 14:28:24 +0800176
Neil Armstrong80e2f972017-03-03 19:20:06 +0200177 unsigned int reg_shift;
178 struct regmap *regm;
Romain Periera7d555d2017-04-14 10:31:12 +0200179 void (*enable_audio)(struct dw_hdmi *hdmi);
180 void (*disable_audio)(struct dw_hdmi *hdmi);
Russell Kinge84b8d72017-07-31 15:29:41 +0100181
182 struct cec_notifier *cec_notifier;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200183};
184
Russell Kingaeac23b2015-06-05 13:46:22 +0100185#define HDMI_IH_PHY_STAT0_RX_SENSE \
186 (HDMI_IH_PHY_STAT0_RX_SENSE0 | HDMI_IH_PHY_STAT0_RX_SENSE1 | \
187 HDMI_IH_PHY_STAT0_RX_SENSE2 | HDMI_IH_PHY_STAT0_RX_SENSE3)
188
189#define HDMI_PHY_RX_SENSE \
190 (HDMI_PHY_RX_SENSE0 | HDMI_PHY_RX_SENSE1 | \
191 HDMI_PHY_RX_SENSE2 | HDMI_PHY_RX_SENSE3)
192
Andy Yan0cd9d142014-12-05 14:28:24 +0800193static inline void hdmi_writeb(struct dw_hdmi *hdmi, u8 val, int offset)
194{
Neil Armstrong80e2f972017-03-03 19:20:06 +0200195 regmap_write(hdmi->regm, offset << hdmi->reg_shift, val);
Andy Yan0cd9d142014-12-05 14:28:24 +0800196}
197
198static inline u8 hdmi_readb(struct dw_hdmi *hdmi, int offset)
199{
Neil Armstrong80e2f972017-03-03 19:20:06 +0200200 unsigned int val = 0;
201
202 regmap_read(hdmi->regm, offset << hdmi->reg_shift, &val);
203
204 return val;
Andy Yan0cd9d142014-12-05 14:28:24 +0800205}
206
Andy Yanb21f4b62014-12-05 14:26:31 +0800207static void hdmi_modb(struct dw_hdmi *hdmi, u8 data, u8 mask, unsigned reg)
Russell King812bc612013-11-04 12:42:02 +0000208{
Neil Armstrong80e2f972017-03-03 19:20:06 +0200209 regmap_update_bits(hdmi->regm, reg << hdmi->reg_shift, mask, data);
Russell King812bc612013-11-04 12:42:02 +0000210}
211
Andy Yanb21f4b62014-12-05 14:26:31 +0800212static void hdmi_mask_writeb(struct dw_hdmi *hdmi, u8 data, unsigned int reg,
Andy Yanb5878332014-12-05 14:23:52 +0800213 u8 shift, u8 mask)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200214{
Russell King812bc612013-11-04 12:42:02 +0000215 hdmi_modb(hdmi, data << shift, mask, reg);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200216}
217
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +0300218static void dw_hdmi_i2c_init(struct dw_hdmi *hdmi)
219{
220 /* Software reset */
221 hdmi_writeb(hdmi, 0x00, HDMI_I2CM_SOFTRSTZ);
222
223 /* Set Standard Mode speed (determined to be 100KHz on iMX6) */
224 hdmi_writeb(hdmi, 0x00, HDMI_I2CM_DIV);
225
226 /* Set done, not acknowledged and arbitration interrupt polarities */
227 hdmi_writeb(hdmi, HDMI_I2CM_INT_DONE_POL, HDMI_I2CM_INT);
228 hdmi_writeb(hdmi, HDMI_I2CM_CTLINT_NAC_POL | HDMI_I2CM_CTLINT_ARB_POL,
229 HDMI_I2CM_CTLINT);
230
231 /* Clear DONE and ERROR interrupts */
232 hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,
233 HDMI_IH_I2CM_STAT0);
234
235 /* Mute DONE and ERROR interrupts */
236 hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,
237 HDMI_IH_MUTE_I2CM_STAT0);
238}
239
240static int dw_hdmi_i2c_read(struct dw_hdmi *hdmi,
241 unsigned char *buf, unsigned int length)
242{
243 struct dw_hdmi_i2c *i2c = hdmi->i2c;
244 int stat;
245
246 if (!i2c->is_regaddr) {
247 dev_dbg(hdmi->dev, "set read register address to 0\n");
248 i2c->slave_reg = 0x00;
249 i2c->is_regaddr = true;
250 }
251
252 while (length--) {
253 reinit_completion(&i2c->cmp);
254
255 hdmi_writeb(hdmi, i2c->slave_reg++, HDMI_I2CM_ADDRESS);
Nickey Yang94bb4dc2017-03-20 10:57:31 +0800256 if (i2c->is_segment)
257 hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_READ_EXT,
258 HDMI_I2CM_OPERATION);
259 else
260 hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_READ,
261 HDMI_I2CM_OPERATION);
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +0300262
263 stat = wait_for_completion_timeout(&i2c->cmp, HZ / 10);
264 if (!stat)
265 return -EAGAIN;
266
267 /* Check for error condition on the bus */
268 if (i2c->stat & HDMI_IH_I2CM_STAT0_ERROR)
269 return -EIO;
270
271 *buf++ = hdmi_readb(hdmi, HDMI_I2CM_DATAI);
272 }
Nickey Yang94bb4dc2017-03-20 10:57:31 +0800273 i2c->is_segment = false;
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +0300274
275 return 0;
276}
277
278static int dw_hdmi_i2c_write(struct dw_hdmi *hdmi,
279 unsigned char *buf, unsigned int length)
280{
281 struct dw_hdmi_i2c *i2c = hdmi->i2c;
282 int stat;
283
284 if (!i2c->is_regaddr) {
285 /* Use the first write byte as register address */
286 i2c->slave_reg = buf[0];
287 length--;
288 buf++;
289 i2c->is_regaddr = true;
290 }
291
292 while (length--) {
293 reinit_completion(&i2c->cmp);
294
295 hdmi_writeb(hdmi, *buf++, HDMI_I2CM_DATAO);
296 hdmi_writeb(hdmi, i2c->slave_reg++, HDMI_I2CM_ADDRESS);
297 hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_WRITE,
298 HDMI_I2CM_OPERATION);
299
300 stat = wait_for_completion_timeout(&i2c->cmp, HZ / 10);
301 if (!stat)
302 return -EAGAIN;
303
304 /* Check for error condition on the bus */
305 if (i2c->stat & HDMI_IH_I2CM_STAT0_ERROR)
306 return -EIO;
307 }
308
309 return 0;
310}
311
312static int dw_hdmi_i2c_xfer(struct i2c_adapter *adap,
313 struct i2c_msg *msgs, int num)
314{
315 struct dw_hdmi *hdmi = i2c_get_adapdata(adap);
316 struct dw_hdmi_i2c *i2c = hdmi->i2c;
317 u8 addr = msgs[0].addr;
318 int i, ret = 0;
319
320 dev_dbg(hdmi->dev, "xfer: num: %d, addr: %#x\n", num, addr);
321
322 for (i = 0; i < num; i++) {
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +0300323 if (msgs[i].len == 0) {
324 dev_dbg(hdmi->dev,
325 "unsupported transfer %d/%d, no data\n",
326 i + 1, num);
327 return -EOPNOTSUPP;
328 }
329 }
330
331 mutex_lock(&i2c->lock);
332
333 /* Unmute DONE and ERROR interrupts */
334 hdmi_writeb(hdmi, 0x00, HDMI_IH_MUTE_I2CM_STAT0);
335
336 /* Set slave device address taken from the first I2C message */
337 hdmi_writeb(hdmi, addr, HDMI_I2CM_SLAVE);
338
339 /* Set slave device register address on transfer */
340 i2c->is_regaddr = false;
341
Nickey Yang94bb4dc2017-03-20 10:57:31 +0800342 /* Set segment pointer for I2C extended read mode operation */
343 i2c->is_segment = false;
344
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +0300345 for (i = 0; i < num; i++) {
346 dev_dbg(hdmi->dev, "xfer: num: %d/%d, len: %d, flags: %#x\n",
347 i + 1, num, msgs[i].len, msgs[i].flags);
Nickey Yang94bb4dc2017-03-20 10:57:31 +0800348 if (msgs[i].addr == DDC_SEGMENT_ADDR && msgs[i].len == 1) {
349 i2c->is_segment = true;
350 hdmi_writeb(hdmi, DDC_SEGMENT_ADDR, HDMI_I2CM_SEGADDR);
351 hdmi_writeb(hdmi, *msgs[i].buf, HDMI_I2CM_SEGPTR);
352 } else {
353 if (msgs[i].flags & I2C_M_RD)
354 ret = dw_hdmi_i2c_read(hdmi, msgs[i].buf,
355 msgs[i].len);
356 else
357 ret = dw_hdmi_i2c_write(hdmi, msgs[i].buf,
358 msgs[i].len);
359 }
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +0300360 if (ret < 0)
361 break;
362 }
363
364 if (!ret)
365 ret = num;
366
367 /* Mute DONE and ERROR interrupts */
368 hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,
369 HDMI_IH_MUTE_I2CM_STAT0);
370
371 mutex_unlock(&i2c->lock);
372
373 return ret;
374}
375
376static u32 dw_hdmi_i2c_func(struct i2c_adapter *adapter)
377{
378 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
379}
380
381static const struct i2c_algorithm dw_hdmi_algorithm = {
382 .master_xfer = dw_hdmi_i2c_xfer,
383 .functionality = dw_hdmi_i2c_func,
384};
385
386static struct i2c_adapter *dw_hdmi_i2c_adapter(struct dw_hdmi *hdmi)
387{
388 struct i2c_adapter *adap;
389 struct dw_hdmi_i2c *i2c;
390 int ret;
391
392 i2c = devm_kzalloc(hdmi->dev, sizeof(*i2c), GFP_KERNEL);
393 if (!i2c)
394 return ERR_PTR(-ENOMEM);
395
396 mutex_init(&i2c->lock);
397 init_completion(&i2c->cmp);
398
399 adap = &i2c->adap;
400 adap->class = I2C_CLASS_DDC;
401 adap->owner = THIS_MODULE;
402 adap->dev.parent = hdmi->dev;
403 adap->algo = &dw_hdmi_algorithm;
404 strlcpy(adap->name, "DesignWare HDMI", sizeof(adap->name));
405 i2c_set_adapdata(adap, hdmi);
406
407 ret = i2c_add_adapter(adap);
408 if (ret) {
409 dev_warn(hdmi->dev, "cannot add %s I2C adapter\n", adap->name);
410 devm_kfree(hdmi->dev, i2c);
411 return ERR_PTR(ret);
412 }
413
414 hdmi->i2c = i2c;
415
416 dev_info(hdmi->dev, "registered %s I2C bus driver\n", adap->name);
417
418 return adap;
419}
420
Russell King351e1352015-01-31 14:50:23 +0000421static void hdmi_set_cts_n(struct dw_hdmi *hdmi, unsigned int cts,
422 unsigned int n)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200423{
Russell King622494a2015-02-02 10:55:38 +0000424 /* Must be set/cleared first */
425 hdmi_modb(hdmi, 0, HDMI_AUD_CTS3_CTS_MANUAL, HDMI_AUD_CTS3);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200426
427 /* nshift factor = 0 */
Russell King812bc612013-11-04 12:42:02 +0000428 hdmi_modb(hdmi, 0, HDMI_AUD_CTS3_N_SHIFT_MASK, HDMI_AUD_CTS3);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200429
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200430 hdmi_writeb(hdmi, ((cts >> 16) & HDMI_AUD_CTS3_AUDCTS19_16_MASK) |
431 HDMI_AUD_CTS3_CTS_MANUAL, HDMI_AUD_CTS3);
Russell King622494a2015-02-02 10:55:38 +0000432 hdmi_writeb(hdmi, (cts >> 8) & 0xff, HDMI_AUD_CTS2);
433 hdmi_writeb(hdmi, cts & 0xff, HDMI_AUD_CTS1);
434
435 hdmi_writeb(hdmi, (n >> 16) & 0x0f, HDMI_AUD_N3);
436 hdmi_writeb(hdmi, (n >> 8) & 0xff, HDMI_AUD_N2);
437 hdmi_writeb(hdmi, n & 0xff, HDMI_AUD_N1);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200438}
439
Russell Kingb195fbd2015-07-22 11:28:16 +0100440static unsigned int hdmi_compute_n(unsigned int freq, unsigned long pixel_clk)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200441{
442 unsigned int n = (128 * freq) / 1000;
Russell Kingd0c96d12015-07-22 10:35:41 +0100443 unsigned int mult = 1;
444
445 while (freq > 48000) {
446 mult *= 2;
447 freq /= 2;
448 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200449
450 switch (freq) {
451 case 32000:
Russell King426701d2015-07-22 10:39:27 +0100452 if (pixel_clk == 25175000)
Russell Kingb195fbd2015-07-22 11:28:16 +0100453 n = 4576;
Russell King426701d2015-07-22 10:39:27 +0100454 else if (pixel_clk == 27027000)
Russell Kingb195fbd2015-07-22 11:28:16 +0100455 n = 4096;
Russell King426701d2015-07-22 10:39:27 +0100456 else if (pixel_clk == 74176000 || pixel_clk == 148352000)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200457 n = 11648;
458 else
459 n = 4096;
Russell Kingd0c96d12015-07-22 10:35:41 +0100460 n *= mult;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200461 break;
462
463 case 44100:
Russell King426701d2015-07-22 10:39:27 +0100464 if (pixel_clk == 25175000)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200465 n = 7007;
Russell King426701d2015-07-22 10:39:27 +0100466 else if (pixel_clk == 74176000)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200467 n = 17836;
Russell King426701d2015-07-22 10:39:27 +0100468 else if (pixel_clk == 148352000)
Russell Kingb195fbd2015-07-22 11:28:16 +0100469 n = 8918;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200470 else
471 n = 6272;
Russell Kingd0c96d12015-07-22 10:35:41 +0100472 n *= mult;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200473 break;
474
475 case 48000:
Russell King426701d2015-07-22 10:39:27 +0100476 if (pixel_clk == 25175000)
Russell Kingb195fbd2015-07-22 11:28:16 +0100477 n = 6864;
Russell King426701d2015-07-22 10:39:27 +0100478 else if (pixel_clk == 27027000)
Russell Kingb195fbd2015-07-22 11:28:16 +0100479 n = 6144;
Russell King426701d2015-07-22 10:39:27 +0100480 else if (pixel_clk == 74176000)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200481 n = 11648;
Russell King426701d2015-07-22 10:39:27 +0100482 else if (pixel_clk == 148352000)
Russell Kingb195fbd2015-07-22 11:28:16 +0100483 n = 5824;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200484 else
485 n = 6144;
Russell Kingd0c96d12015-07-22 10:35:41 +0100486 n *= mult;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200487 break;
488
489 default:
490 break;
491 }
492
493 return n;
494}
495
Andy Yanb21f4b62014-12-05 14:26:31 +0800496static void hdmi_set_clk_regenerator(struct dw_hdmi *hdmi,
Russell Kingb195fbd2015-07-22 11:28:16 +0100497 unsigned long pixel_clk, unsigned int sample_rate)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200498{
Russell Kingdfbdaf52015-07-22 16:54:37 +0100499 unsigned long ftdms = pixel_clk;
Russell Kingf879b382015-03-27 12:53:29 +0000500 unsigned int n, cts;
Russell Kingdfbdaf52015-07-22 16:54:37 +0100501 u64 tmp;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200502
Russell Kingb195fbd2015-07-22 11:28:16 +0100503 n = hdmi_compute_n(sample_rate, pixel_clk);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200504
Russell Kingdfbdaf52015-07-22 16:54:37 +0100505 /*
506 * Compute the CTS value from the N value. Note that CTS and N
507 * can be up to 20 bits in total, so we need 64-bit math. Also
508 * note that our TDMS clock is not fully accurate; it is accurate
509 * to kHz. This can introduce an unnecessary remainder in the
510 * calculation below, so we don't try to warn about that.
511 */
512 tmp = (u64)ftdms * n;
513 do_div(tmp, 128 * sample_rate);
514 cts = tmp;
515
516 dev_dbg(hdmi->dev, "%s: fs=%uHz ftdms=%lu.%03luMHz N=%d cts=%d\n",
517 __func__, sample_rate, ftdms / 1000000, (ftdms / 1000) % 1000,
518 n, cts);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200519
Russell Kingb90120a2015-03-27 12:59:58 +0000520 spin_lock_irq(&hdmi->audio_lock);
521 hdmi->audio_n = n;
522 hdmi->audio_cts = cts;
523 hdmi_set_cts_n(hdmi, cts, hdmi->audio_enable ? n : 0);
524 spin_unlock_irq(&hdmi->audio_lock);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200525}
526
Andy Yanb21f4b62014-12-05 14:26:31 +0800527static void hdmi_init_clk_regenerator(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200528{
Russell King6bcf4952015-02-02 11:01:08 +0000529 mutex_lock(&hdmi->audio_mutex);
Russell Kingb195fbd2015-07-22 11:28:16 +0100530 hdmi_set_clk_regenerator(hdmi, 74250000, hdmi->sample_rate);
Russell King6bcf4952015-02-02 11:01:08 +0000531 mutex_unlock(&hdmi->audio_mutex);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200532}
533
Andy Yanb21f4b62014-12-05 14:26:31 +0800534static void hdmi_clk_regenerator_update_pixel_clock(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200535{
Russell King6bcf4952015-02-02 11:01:08 +0000536 mutex_lock(&hdmi->audio_mutex);
Russell Kingf879b382015-03-27 12:53:29 +0000537 hdmi_set_clk_regenerator(hdmi, hdmi->hdmi_data.video_mode.mpixelclock,
Russell Kingb195fbd2015-07-22 11:28:16 +0100538 hdmi->sample_rate);
Russell King6bcf4952015-02-02 11:01:08 +0000539 mutex_unlock(&hdmi->audio_mutex);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200540}
541
Russell Kingb5814ff2015-03-27 12:50:58 +0000542void dw_hdmi_set_sample_rate(struct dw_hdmi *hdmi, unsigned int rate)
543{
544 mutex_lock(&hdmi->audio_mutex);
545 hdmi->sample_rate = rate;
546 hdmi_set_clk_regenerator(hdmi, hdmi->hdmi_data.video_mode.mpixelclock,
Russell Kingb195fbd2015-07-22 11:28:16 +0100547 hdmi->sample_rate);
Russell Kingb5814ff2015-03-27 12:50:58 +0000548 mutex_unlock(&hdmi->audio_mutex);
549}
550EXPORT_SYMBOL_GPL(dw_hdmi_set_sample_rate);
551
Romain Perier57fbc052017-04-20 14:34:34 +0530552static void hdmi_enable_audio_clk(struct dw_hdmi *hdmi, bool enable)
553{
554 hdmi_modb(hdmi, enable ? 0 : HDMI_MC_CLKDIS_AUDCLK_DISABLE,
555 HDMI_MC_CLKDIS_AUDCLK_DISABLE, HDMI_MC_CLKDIS);
556}
557
Romain Periera7d555d2017-04-14 10:31:12 +0200558static void dw_hdmi_ahb_audio_enable(struct dw_hdmi *hdmi)
559{
560 hdmi_set_cts_n(hdmi, hdmi->audio_cts, hdmi->audio_n);
561}
562
563static void dw_hdmi_ahb_audio_disable(struct dw_hdmi *hdmi)
564{
565 hdmi_set_cts_n(hdmi, hdmi->audio_cts, 0);
566}
567
568static void dw_hdmi_i2s_audio_enable(struct dw_hdmi *hdmi)
569{
570 hdmi_set_cts_n(hdmi, hdmi->audio_cts, hdmi->audio_n);
Romain Perier57fbc052017-04-20 14:34:34 +0530571 hdmi_enable_audio_clk(hdmi, true);
572}
573
574static void dw_hdmi_i2s_audio_disable(struct dw_hdmi *hdmi)
575{
576 hdmi_enable_audio_clk(hdmi, false);
Romain Periera7d555d2017-04-14 10:31:12 +0200577}
578
Russell Kingb90120a2015-03-27 12:59:58 +0000579void dw_hdmi_audio_enable(struct dw_hdmi *hdmi)
580{
581 unsigned long flags;
582
583 spin_lock_irqsave(&hdmi->audio_lock, flags);
584 hdmi->audio_enable = true;
Romain Periera7d555d2017-04-14 10:31:12 +0200585 if (hdmi->enable_audio)
586 hdmi->enable_audio(hdmi);
Russell Kingb90120a2015-03-27 12:59:58 +0000587 spin_unlock_irqrestore(&hdmi->audio_lock, flags);
588}
589EXPORT_SYMBOL_GPL(dw_hdmi_audio_enable);
590
591void dw_hdmi_audio_disable(struct dw_hdmi *hdmi)
592{
593 unsigned long flags;
594
595 spin_lock_irqsave(&hdmi->audio_lock, flags);
596 hdmi->audio_enable = false;
Romain Periera7d555d2017-04-14 10:31:12 +0200597 if (hdmi->disable_audio)
598 hdmi->disable_audio(hdmi);
Russell Kingb90120a2015-03-27 12:59:58 +0000599 spin_unlock_irqrestore(&hdmi->audio_lock, flags);
600}
601EXPORT_SYMBOL_GPL(dw_hdmi_audio_disable);
602
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200603static bool hdmi_bus_fmt_is_rgb(unsigned int bus_format)
604{
605 switch (bus_format) {
606 case MEDIA_BUS_FMT_RGB888_1X24:
607 case MEDIA_BUS_FMT_RGB101010_1X30:
608 case MEDIA_BUS_FMT_RGB121212_1X36:
609 case MEDIA_BUS_FMT_RGB161616_1X48:
610 return true;
611
612 default:
613 return false;
614 }
615}
616
617static bool hdmi_bus_fmt_is_yuv444(unsigned int bus_format)
618{
619 switch (bus_format) {
620 case MEDIA_BUS_FMT_YUV8_1X24:
621 case MEDIA_BUS_FMT_YUV10_1X30:
622 case MEDIA_BUS_FMT_YUV12_1X36:
623 case MEDIA_BUS_FMT_YUV16_1X48:
624 return true;
625
626 default:
627 return false;
628 }
629}
630
631static bool hdmi_bus_fmt_is_yuv422(unsigned int bus_format)
632{
633 switch (bus_format) {
634 case MEDIA_BUS_FMT_UYVY8_1X16:
635 case MEDIA_BUS_FMT_UYVY10_1X20:
636 case MEDIA_BUS_FMT_UYVY12_1X24:
637 return true;
638
639 default:
640 return false;
641 }
642}
643
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200644static int hdmi_bus_fmt_color_depth(unsigned int bus_format)
645{
646 switch (bus_format) {
647 case MEDIA_BUS_FMT_RGB888_1X24:
648 case MEDIA_BUS_FMT_YUV8_1X24:
649 case MEDIA_BUS_FMT_UYVY8_1X16:
650 case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
651 return 8;
652
653 case MEDIA_BUS_FMT_RGB101010_1X30:
654 case MEDIA_BUS_FMT_YUV10_1X30:
655 case MEDIA_BUS_FMT_UYVY10_1X20:
656 case MEDIA_BUS_FMT_UYYVYY10_0_5X30:
657 return 10;
658
659 case MEDIA_BUS_FMT_RGB121212_1X36:
660 case MEDIA_BUS_FMT_YUV12_1X36:
661 case MEDIA_BUS_FMT_UYVY12_1X24:
662 case MEDIA_BUS_FMT_UYYVYY12_0_5X36:
663 return 12;
664
665 case MEDIA_BUS_FMT_RGB161616_1X48:
666 case MEDIA_BUS_FMT_YUV16_1X48:
667 case MEDIA_BUS_FMT_UYYVYY16_0_5X48:
668 return 16;
669
670 default:
671 return 0;
672 }
673}
674
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200675/*
676 * this submodule is responsible for the video data synchronization.
677 * for example, for RGB 4:4:4 input, the data map is defined as
678 * pin{47~40} <==> R[7:0]
679 * pin{31~24} <==> G[7:0]
680 * pin{15~8} <==> B[7:0]
681 */
Andy Yanb21f4b62014-12-05 14:26:31 +0800682static void hdmi_video_sample(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200683{
684 int color_format = 0;
685 u8 val;
686
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200687 switch (hdmi->hdmi_data.enc_in_bus_format) {
688 case MEDIA_BUS_FMT_RGB888_1X24:
689 color_format = 0x01;
690 break;
691 case MEDIA_BUS_FMT_RGB101010_1X30:
692 color_format = 0x03;
693 break;
694 case MEDIA_BUS_FMT_RGB121212_1X36:
695 color_format = 0x05;
696 break;
697 case MEDIA_BUS_FMT_RGB161616_1X48:
698 color_format = 0x07;
699 break;
700
701 case MEDIA_BUS_FMT_YUV8_1X24:
702 case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
703 color_format = 0x09;
704 break;
705 case MEDIA_BUS_FMT_YUV10_1X30:
706 case MEDIA_BUS_FMT_UYYVYY10_0_5X30:
707 color_format = 0x0B;
708 break;
709 case MEDIA_BUS_FMT_YUV12_1X36:
710 case MEDIA_BUS_FMT_UYYVYY12_0_5X36:
711 color_format = 0x0D;
712 break;
713 case MEDIA_BUS_FMT_YUV16_1X48:
714 case MEDIA_BUS_FMT_UYYVYY16_0_5X48:
715 color_format = 0x0F;
716 break;
717
718 case MEDIA_BUS_FMT_UYVY8_1X16:
719 color_format = 0x16;
720 break;
721 case MEDIA_BUS_FMT_UYVY10_1X20:
722 color_format = 0x14;
723 break;
724 case MEDIA_BUS_FMT_UYVY12_1X24:
725 color_format = 0x12;
726 break;
727
728 default:
729 return;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200730 }
731
732 val = HDMI_TX_INVID0_INTERNAL_DE_GENERATOR_DISABLE |
733 ((color_format << HDMI_TX_INVID0_VIDEO_MAPPING_OFFSET) &
734 HDMI_TX_INVID0_VIDEO_MAPPING_MASK);
735 hdmi_writeb(hdmi, val, HDMI_TX_INVID0);
736
737 /* Enable TX stuffing: When DE is inactive, fix the output data to 0 */
738 val = HDMI_TX_INSTUFFING_BDBDATA_STUFFING_ENABLE |
739 HDMI_TX_INSTUFFING_RCRDATA_STUFFING_ENABLE |
740 HDMI_TX_INSTUFFING_GYDATA_STUFFING_ENABLE;
741 hdmi_writeb(hdmi, val, HDMI_TX_INSTUFFING);
742 hdmi_writeb(hdmi, 0x0, HDMI_TX_GYDATA0);
743 hdmi_writeb(hdmi, 0x0, HDMI_TX_GYDATA1);
744 hdmi_writeb(hdmi, 0x0, HDMI_TX_RCRDATA0);
745 hdmi_writeb(hdmi, 0x0, HDMI_TX_RCRDATA1);
746 hdmi_writeb(hdmi, 0x0, HDMI_TX_BCBDATA0);
747 hdmi_writeb(hdmi, 0x0, HDMI_TX_BCBDATA1);
748}
749
Andy Yanb21f4b62014-12-05 14:26:31 +0800750static int is_color_space_conversion(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200751{
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200752 return hdmi->hdmi_data.enc_in_bus_format != hdmi->hdmi_data.enc_out_bus_format;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200753}
754
Andy Yanb21f4b62014-12-05 14:26:31 +0800755static int is_color_space_decimation(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200756{
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200757 if (!hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format))
Fabio Estevamba92b222014-02-06 10:12:03 -0200758 return 0;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200759
760 if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_in_bus_format) ||
761 hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_in_bus_format))
Fabio Estevamba92b222014-02-06 10:12:03 -0200762 return 1;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200763
Fabio Estevamba92b222014-02-06 10:12:03 -0200764 return 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200765}
766
Andy Yanb21f4b62014-12-05 14:26:31 +0800767static int is_color_space_interpolation(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200768{
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200769 if (!hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_in_bus_format))
Fabio Estevamba92b222014-02-06 10:12:03 -0200770 return 0;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200771
772 if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format) ||
773 hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format))
Fabio Estevamba92b222014-02-06 10:12:03 -0200774 return 1;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200775
Fabio Estevamba92b222014-02-06 10:12:03 -0200776 return 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200777}
778
Andy Yanb21f4b62014-12-05 14:26:31 +0800779static void dw_hdmi_update_csc_coeffs(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200780{
781 const u16 (*csc_coeff)[3][4] = &csc_coeff_default;
Russell Kingc082f9d2013-11-04 12:10:40 +0000782 unsigned i;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200783 u32 csc_scale = 1;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200784
785 if (is_color_space_conversion(hdmi)) {
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200786 if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format)) {
787 if (hdmi->hdmi_data.enc_out_encoding ==
788 V4L2_YCBCR_ENC_601)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200789 csc_coeff = &csc_coeff_rgb_out_eitu601;
790 else
791 csc_coeff = &csc_coeff_rgb_out_eitu709;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200792 } else if (hdmi_bus_fmt_is_rgb(
793 hdmi->hdmi_data.enc_in_bus_format)) {
794 if (hdmi->hdmi_data.enc_out_encoding ==
795 V4L2_YCBCR_ENC_601)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200796 csc_coeff = &csc_coeff_rgb_in_eitu601;
797 else
798 csc_coeff = &csc_coeff_rgb_in_eitu709;
799 csc_scale = 0;
800 }
801 }
802
Russell Kingc082f9d2013-11-04 12:10:40 +0000803 /* The CSC registers are sequential, alternating MSB then LSB */
804 for (i = 0; i < ARRAY_SIZE(csc_coeff_default[0]); i++) {
805 u16 coeff_a = (*csc_coeff)[0][i];
806 u16 coeff_b = (*csc_coeff)[1][i];
807 u16 coeff_c = (*csc_coeff)[2][i];
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200808
Andy Yanb5878332014-12-05 14:23:52 +0800809 hdmi_writeb(hdmi, coeff_a & 0xff, HDMI_CSC_COEF_A1_LSB + i * 2);
Russell Kingc082f9d2013-11-04 12:10:40 +0000810 hdmi_writeb(hdmi, coeff_a >> 8, HDMI_CSC_COEF_A1_MSB + i * 2);
811 hdmi_writeb(hdmi, coeff_b & 0xff, HDMI_CSC_COEF_B1_LSB + i * 2);
812 hdmi_writeb(hdmi, coeff_b >> 8, HDMI_CSC_COEF_B1_MSB + i * 2);
Andy Yanb5878332014-12-05 14:23:52 +0800813 hdmi_writeb(hdmi, coeff_c & 0xff, HDMI_CSC_COEF_C1_LSB + i * 2);
Russell Kingc082f9d2013-11-04 12:10:40 +0000814 hdmi_writeb(hdmi, coeff_c >> 8, HDMI_CSC_COEF_C1_MSB + i * 2);
815 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200816
Russell King812bc612013-11-04 12:42:02 +0000817 hdmi_modb(hdmi, csc_scale, HDMI_CSC_SCALE_CSCSCALE_MASK,
818 HDMI_CSC_SCALE);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200819}
820
Andy Yanb21f4b62014-12-05 14:26:31 +0800821static void hdmi_video_csc(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200822{
823 int color_depth = 0;
824 int interpolation = HDMI_CSC_CFG_INTMODE_DISABLE;
825 int decimation = 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200826
827 /* YCC422 interpolation to 444 mode */
828 if (is_color_space_interpolation(hdmi))
829 interpolation = HDMI_CSC_CFG_INTMODE_CHROMA_INT_FORMULA1;
830 else if (is_color_space_decimation(hdmi))
831 decimation = HDMI_CSC_CFG_DECMODE_CHROMA_INT_FORMULA3;
832
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200833 switch (hdmi_bus_fmt_color_depth(hdmi->hdmi_data.enc_out_bus_format)) {
834 case 8:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200835 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_24BPP;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200836 break;
837 case 10:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200838 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_30BPP;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200839 break;
840 case 12:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200841 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_36BPP;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200842 break;
843 case 16:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200844 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_48BPP;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200845 break;
846
847 default:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200848 return;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200849 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200850
851 /* Configure the CSC registers */
852 hdmi_writeb(hdmi, interpolation | decimation, HDMI_CSC_CFG);
Russell King812bc612013-11-04 12:42:02 +0000853 hdmi_modb(hdmi, color_depth, HDMI_CSC_SCALE_CSC_COLORDE_PTH_MASK,
854 HDMI_CSC_SCALE);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200855
Andy Yanb21f4b62014-12-05 14:26:31 +0800856 dw_hdmi_update_csc_coeffs(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200857}
858
859/*
860 * HDMI video packetizer is used to packetize the data.
861 * for example, if input is YCC422 mode or repeater is used,
862 * data should be repacked this module can be bypassed.
863 */
Andy Yanb21f4b62014-12-05 14:26:31 +0800864static void hdmi_video_packetize(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200865{
866 unsigned int color_depth = 0;
867 unsigned int remap_size = HDMI_VP_REMAP_YCC422_16bit;
868 unsigned int output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_PP;
869 struct hdmi_data_info *hdmi_data = &hdmi->hdmi_data;
Russell Kingbebdf662013-11-04 12:55:30 +0000870 u8 val, vp_conf;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200871
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200872 if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format) ||
873 hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format)) {
874 switch (hdmi_bus_fmt_color_depth(
875 hdmi->hdmi_data.enc_out_bus_format)) {
876 case 8:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200877 color_depth = 4;
878 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200879 break;
880 case 10:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200881 color_depth = 5;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200882 break;
883 case 12:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200884 color_depth = 6;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200885 break;
886 case 16:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200887 color_depth = 7;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200888 break;
889 default:
890 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;
891 }
892 } else if (hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format)) {
893 switch (hdmi_bus_fmt_color_depth(
894 hdmi->hdmi_data.enc_out_bus_format)) {
895 case 0:
896 case 8:
897 remap_size = HDMI_VP_REMAP_YCC422_16bit;
898 break;
899 case 10:
900 remap_size = HDMI_VP_REMAP_YCC422_20bit;
901 break;
902 case 12:
903 remap_size = HDMI_VP_REMAP_YCC422_24bit;
904 break;
905
906 default:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200907 return;
Andy Yanb5878332014-12-05 14:23:52 +0800908 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200909 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_YCC422;
Andy Yanb5878332014-12-05 14:23:52 +0800910 } else {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200911 return;
Andy Yanb5878332014-12-05 14:23:52 +0800912 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200913
914 /* set the packetizer registers */
915 val = ((color_depth << HDMI_VP_PR_CD_COLOR_DEPTH_OFFSET) &
916 HDMI_VP_PR_CD_COLOR_DEPTH_MASK) |
917 ((hdmi_data->pix_repet_factor <<
918 HDMI_VP_PR_CD_DESIRED_PR_FACTOR_OFFSET) &
919 HDMI_VP_PR_CD_DESIRED_PR_FACTOR_MASK);
920 hdmi_writeb(hdmi, val, HDMI_VP_PR_CD);
921
Russell King812bc612013-11-04 12:42:02 +0000922 hdmi_modb(hdmi, HDMI_VP_STUFF_PR_STUFFING_STUFFING_MODE,
923 HDMI_VP_STUFF_PR_STUFFING_MASK, HDMI_VP_STUFF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200924
925 /* Data from pixel repeater block */
926 if (hdmi_data->pix_repet_factor > 1) {
Russell Kingbebdf662013-11-04 12:55:30 +0000927 vp_conf = HDMI_VP_CONF_PR_EN_ENABLE |
928 HDMI_VP_CONF_BYPASS_SELECT_PIX_REPEATER;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200929 } else { /* data from packetizer block */
Russell Kingbebdf662013-11-04 12:55:30 +0000930 vp_conf = HDMI_VP_CONF_PR_EN_DISABLE |
931 HDMI_VP_CONF_BYPASS_SELECT_VID_PACKETIZER;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200932 }
933
Russell Kingbebdf662013-11-04 12:55:30 +0000934 hdmi_modb(hdmi, vp_conf,
935 HDMI_VP_CONF_PR_EN_MASK |
936 HDMI_VP_CONF_BYPASS_SELECT_MASK, HDMI_VP_CONF);
937
Russell King812bc612013-11-04 12:42:02 +0000938 hdmi_modb(hdmi, 1 << HDMI_VP_STUFF_IDEFAULT_PHASE_OFFSET,
939 HDMI_VP_STUFF_IDEFAULT_PHASE_MASK, HDMI_VP_STUFF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200940
941 hdmi_writeb(hdmi, remap_size, HDMI_VP_REMAP);
942
943 if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_PP) {
Russell Kingbebdf662013-11-04 12:55:30 +0000944 vp_conf = HDMI_VP_CONF_BYPASS_EN_DISABLE |
945 HDMI_VP_CONF_PP_EN_ENABLE |
946 HDMI_VP_CONF_YCC422_EN_DISABLE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200947 } else if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_YCC422) {
Russell Kingbebdf662013-11-04 12:55:30 +0000948 vp_conf = HDMI_VP_CONF_BYPASS_EN_DISABLE |
949 HDMI_VP_CONF_PP_EN_DISABLE |
950 HDMI_VP_CONF_YCC422_EN_ENABLE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200951 } else if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS) {
Russell Kingbebdf662013-11-04 12:55:30 +0000952 vp_conf = HDMI_VP_CONF_BYPASS_EN_ENABLE |
953 HDMI_VP_CONF_PP_EN_DISABLE |
954 HDMI_VP_CONF_YCC422_EN_DISABLE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200955 } else {
956 return;
957 }
958
Russell Kingbebdf662013-11-04 12:55:30 +0000959 hdmi_modb(hdmi, vp_conf,
960 HDMI_VP_CONF_BYPASS_EN_MASK | HDMI_VP_CONF_PP_EN_ENMASK |
961 HDMI_VP_CONF_YCC422_EN_MASK, HDMI_VP_CONF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200962
Russell King812bc612013-11-04 12:42:02 +0000963 hdmi_modb(hdmi, HDMI_VP_STUFF_PP_STUFFING_STUFFING_MODE |
964 HDMI_VP_STUFF_YCC422_STUFFING_STUFFING_MODE,
965 HDMI_VP_STUFF_PP_STUFFING_MASK |
966 HDMI_VP_STUFF_YCC422_STUFFING_MASK, HDMI_VP_STUFF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200967
Russell King812bc612013-11-04 12:42:02 +0000968 hdmi_modb(hdmi, output_select, HDMI_VP_CONF_OUTPUT_SELECTOR_MASK,
969 HDMI_VP_CONF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200970}
971
Laurent Pinchartf1585f62017-03-06 01:36:15 +0200972/* -----------------------------------------------------------------------------
973 * Synopsys PHY Handling
974 */
975
Andy Yanb21f4b62014-12-05 14:26:31 +0800976static inline void hdmi_phy_test_clear(struct dw_hdmi *hdmi,
Andy Yanb5878332014-12-05 14:23:52 +0800977 unsigned char bit)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200978{
Russell King812bc612013-11-04 12:42:02 +0000979 hdmi_modb(hdmi, bit << HDMI_PHY_TST0_TSTCLR_OFFSET,
980 HDMI_PHY_TST0_TSTCLR_MASK, HDMI_PHY_TST0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200981}
982
Andy Yanb21f4b62014-12-05 14:26:31 +0800983static bool hdmi_phy_wait_i2c_done(struct dw_hdmi *hdmi, int msec)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200984{
Andy Yana4d3b8b2014-12-05 14:31:09 +0800985 u32 val;
986
987 while ((val = hdmi_readb(hdmi, HDMI_IH_I2CMPHY_STAT0) & 0x3) == 0) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200988 if (msec-- == 0)
989 return false;
Emil Renner Berthing0e6bcf32014-03-30 00:21:21 +0100990 udelay(1000);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200991 }
Andy Yana4d3b8b2014-12-05 14:31:09 +0800992 hdmi_writeb(hdmi, val, HDMI_IH_I2CMPHY_STAT0);
993
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200994 return true;
995}
996
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +0200997void dw_hdmi_phy_i2c_write(struct dw_hdmi *hdmi, unsigned short data,
998 unsigned char addr)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200999{
1000 hdmi_writeb(hdmi, 0xFF, HDMI_IH_I2CMPHY_STAT0);
1001 hdmi_writeb(hdmi, addr, HDMI_PHY_I2CM_ADDRESS_ADDR);
1002 hdmi_writeb(hdmi, (unsigned char)(data >> 8),
Andy Yanb5878332014-12-05 14:23:52 +08001003 HDMI_PHY_I2CM_DATAO_1_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001004 hdmi_writeb(hdmi, (unsigned char)(data >> 0),
Andy Yanb5878332014-12-05 14:23:52 +08001005 HDMI_PHY_I2CM_DATAO_0_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001006 hdmi_writeb(hdmi, HDMI_PHY_I2CM_OPERATION_ADDR_WRITE,
Andy Yanb5878332014-12-05 14:23:52 +08001007 HDMI_PHY_I2CM_OPERATION_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001008 hdmi_phy_wait_i2c_done(hdmi, 1000);
1009}
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001010EXPORT_SYMBOL_GPL(dw_hdmi_phy_i2c_write);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001011
Russell King2fada102015-07-28 12:21:34 +01001012static void dw_hdmi_phy_enable_powerdown(struct dw_hdmi *hdmi, bool enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001013{
Russell King2fada102015-07-28 12:21:34 +01001014 hdmi_mask_writeb(hdmi, !enable, HDMI_PHY_CONF0,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001015 HDMI_PHY_CONF0_PDZ_OFFSET,
1016 HDMI_PHY_CONF0_PDZ_MASK);
1017}
1018
Andy Yanb21f4b62014-12-05 14:26:31 +08001019static void dw_hdmi_phy_enable_tmds(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001020{
1021 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
1022 HDMI_PHY_CONF0_ENTMDS_OFFSET,
1023 HDMI_PHY_CONF0_ENTMDS_MASK);
1024}
1025
Laurent Pinchartf4104e82017-01-17 10:29:02 +02001026static void dw_hdmi_phy_enable_svsret(struct dw_hdmi *hdmi, u8 enable)
Andy Yand346c142014-12-05 14:31:53 +08001027{
1028 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
Laurent Pinchartf4104e82017-01-17 10:29:02 +02001029 HDMI_PHY_CONF0_SVSRET_OFFSET,
1030 HDMI_PHY_CONF0_SVSRET_MASK);
Andy Yand346c142014-12-05 14:31:53 +08001031}
1032
Andy Yanb21f4b62014-12-05 14:26:31 +08001033static void dw_hdmi_phy_gen2_pddq(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001034{
1035 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
1036 HDMI_PHY_CONF0_GEN2_PDDQ_OFFSET,
1037 HDMI_PHY_CONF0_GEN2_PDDQ_MASK);
1038}
1039
Andy Yanb21f4b62014-12-05 14:26:31 +08001040static void dw_hdmi_phy_gen2_txpwron(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001041{
1042 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
1043 HDMI_PHY_CONF0_GEN2_TXPWRON_OFFSET,
1044 HDMI_PHY_CONF0_GEN2_TXPWRON_MASK);
1045}
1046
Andy Yanb21f4b62014-12-05 14:26:31 +08001047static void dw_hdmi_phy_sel_data_en_pol(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001048{
1049 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
1050 HDMI_PHY_CONF0_SELDATAENPOL_OFFSET,
1051 HDMI_PHY_CONF0_SELDATAENPOL_MASK);
1052}
1053
Andy Yanb21f4b62014-12-05 14:26:31 +08001054static void dw_hdmi_phy_sel_interface_control(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001055{
1056 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
1057 HDMI_PHY_CONF0_SELDIPIF_OFFSET,
1058 HDMI_PHY_CONF0_SELDIPIF_MASK);
1059}
1060
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02001061static void dw_hdmi_phy_power_off(struct dw_hdmi *hdmi)
1062{
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001063 const struct dw_hdmi_phy_data *phy = hdmi->phy.data;
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02001064 unsigned int i;
1065 u16 val;
1066
1067 if (phy->gen == 1) {
1068 dw_hdmi_phy_enable_tmds(hdmi, 0);
1069 dw_hdmi_phy_enable_powerdown(hdmi, true);
1070 return;
1071 }
1072
1073 dw_hdmi_phy_gen2_txpwron(hdmi, 0);
1074
1075 /*
1076 * Wait for TX_PHY_LOCK to be deasserted to indicate that the PHY went
1077 * to low power mode.
1078 */
1079 for (i = 0; i < 5; ++i) {
1080 val = hdmi_readb(hdmi, HDMI_PHY_STAT0);
1081 if (!(val & HDMI_PHY_TX_PHY_LOCK))
1082 break;
1083
1084 usleep_range(1000, 2000);
1085 }
1086
1087 if (val & HDMI_PHY_TX_PHY_LOCK)
1088 dev_warn(hdmi->dev, "PHY failed to power down\n");
1089 else
1090 dev_dbg(hdmi->dev, "PHY powered down in %u iterations\n", i);
1091
1092 dw_hdmi_phy_gen2_pddq(hdmi, 1);
1093}
1094
Laurent Pinchart181e0ef2017-03-06 01:35:57 +02001095static int dw_hdmi_phy_power_on(struct dw_hdmi *hdmi)
1096{
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001097 const struct dw_hdmi_phy_data *phy = hdmi->phy.data;
Laurent Pinchart181e0ef2017-03-06 01:35:57 +02001098 unsigned int i;
1099 u8 val;
1100
1101 if (phy->gen == 1) {
1102 dw_hdmi_phy_enable_powerdown(hdmi, false);
1103
1104 /* Toggle TMDS enable. */
1105 dw_hdmi_phy_enable_tmds(hdmi, 0);
1106 dw_hdmi_phy_enable_tmds(hdmi, 1);
1107 return 0;
1108 }
1109
1110 dw_hdmi_phy_gen2_txpwron(hdmi, 1);
1111 dw_hdmi_phy_gen2_pddq(hdmi, 0);
1112
1113 /* Wait for PHY PLL lock */
1114 for (i = 0; i < 5; ++i) {
1115 val = hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_TX_PHY_LOCK;
1116 if (val)
1117 break;
1118
1119 usleep_range(1000, 2000);
1120 }
1121
1122 if (!val) {
1123 dev_err(hdmi->dev, "PHY PLL failed to lock\n");
1124 return -ETIMEDOUT;
1125 }
1126
1127 dev_dbg(hdmi->dev, "PHY PLL locked %u iterations\n", i);
1128 return 0;
1129}
1130
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001131/*
1132 * PHY configuration function for the DWC HDMI 3D TX PHY. Based on the available
1133 * information the DWC MHL PHY has the same register layout and is thus also
1134 * supported by this function.
1135 */
1136static int hdmi_phy_configure_dwc_hdmi_3d_tx(struct dw_hdmi *hdmi,
1137 const struct dw_hdmi_plat_data *pdata,
1138 unsigned long mpixelclock)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001139{
Russell King39cc1532015-03-31 18:34:11 +01001140 const struct dw_hdmi_mpll_config *mpll_config = pdata->mpll_cfg;
1141 const struct dw_hdmi_curr_ctrl *curr_ctrl = pdata->cur_ctr;
1142 const struct dw_hdmi_phy_config *phy_config = pdata->phy_config;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001143
Russell King39cc1532015-03-31 18:34:11 +01001144 /* PLL/MPLL Cfg - always match on final entry */
1145 for (; mpll_config->mpixelclock != ~0UL; mpll_config++)
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001146 if (mpixelclock <= mpll_config->mpixelclock)
Russell King39cc1532015-03-31 18:34:11 +01001147 break;
1148
1149 for (; curr_ctrl->mpixelclock != ~0UL; curr_ctrl++)
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001150 if (mpixelclock <= curr_ctrl->mpixelclock)
Russell King39cc1532015-03-31 18:34:11 +01001151 break;
1152
1153 for (; phy_config->mpixelclock != ~0UL; phy_config++)
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001154 if (mpixelclock <= phy_config->mpixelclock)
Russell King39cc1532015-03-31 18:34:11 +01001155 break;
1156
1157 if (mpll_config->mpixelclock == ~0UL ||
1158 curr_ctrl->mpixelclock == ~0UL ||
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001159 phy_config->mpixelclock == ~0UL)
Russell King39cc1532015-03-31 18:34:11 +01001160 return -EINVAL;
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001161
1162 dw_hdmi_phy_i2c_write(hdmi, mpll_config->res[0].cpce,
1163 HDMI_3D_TX_PHY_CPCE_CTRL);
1164 dw_hdmi_phy_i2c_write(hdmi, mpll_config->res[0].gmp,
1165 HDMI_3D_TX_PHY_GMPCTRL);
1166 dw_hdmi_phy_i2c_write(hdmi, curr_ctrl->curr[0],
1167 HDMI_3D_TX_PHY_CURRCTRL);
1168
1169 dw_hdmi_phy_i2c_write(hdmi, 0, HDMI_3D_TX_PHY_PLLPHBYCTRL);
1170 dw_hdmi_phy_i2c_write(hdmi, HDMI_3D_TX_PHY_MSM_CTRL_CKO_SEL_FB_CLK,
1171 HDMI_3D_TX_PHY_MSM_CTRL);
1172
1173 dw_hdmi_phy_i2c_write(hdmi, phy_config->term, HDMI_3D_TX_PHY_TXTERM);
1174 dw_hdmi_phy_i2c_write(hdmi, phy_config->sym_ctr,
1175 HDMI_3D_TX_PHY_CKSYMTXCTRL);
1176 dw_hdmi_phy_i2c_write(hdmi, phy_config->vlev_ctr,
1177 HDMI_3D_TX_PHY_VLEVCTRL);
1178
1179 /* Override and disable clock termination. */
1180 dw_hdmi_phy_i2c_write(hdmi, HDMI_3D_TX_PHY_CKCALCTRL_OVERRIDE,
1181 HDMI_3D_TX_PHY_CKCALCTRL);
1182
1183 return 0;
1184}
1185
1186static int hdmi_phy_configure(struct dw_hdmi *hdmi)
1187{
1188 const struct dw_hdmi_phy_data *phy = hdmi->phy.data;
1189 const struct dw_hdmi_plat_data *pdata = hdmi->plat_data;
1190 unsigned long mpixelclock = hdmi->hdmi_data.video_mode.mpixelclock;
1191 int ret;
Russell King39cc1532015-03-31 18:34:11 +01001192
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02001193 dw_hdmi_phy_power_off(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001194
Laurent Pinchart2668db32017-01-17 10:29:09 +02001195 /* Leave low power consumption mode by asserting SVSRET. */
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001196 if (phy->has_svsret)
Laurent Pinchart2668db32017-01-17 10:29:09 +02001197 dw_hdmi_phy_enable_svsret(hdmi, 1);
1198
Laurent Pinchart54d72732017-01-17 10:29:08 +02001199 /* PHY reset. The reset signal is active high on Gen2 PHYs. */
1200 hdmi_writeb(hdmi, HDMI_MC_PHYRSTZ_PHYRSTZ, HDMI_MC_PHYRSTZ);
1201 hdmi_writeb(hdmi, 0, HDMI_MC_PHYRSTZ);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001202
1203 hdmi_writeb(hdmi, HDMI_MC_HEACPHY_RST_ASSERT, HDMI_MC_HEACPHY_RST);
1204
1205 hdmi_phy_test_clear(hdmi, 1);
1206 hdmi_writeb(hdmi, HDMI_PHY_I2CM_SLAVE_ADDR_PHY_GEN2,
Andy Yanb5878332014-12-05 14:23:52 +08001207 HDMI_PHY_I2CM_SLAVE_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001208 hdmi_phy_test_clear(hdmi, 0);
1209
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001210 /* Write to the PHY as configured by the platform */
1211 if (pdata->configure_phy)
1212 ret = pdata->configure_phy(hdmi, pdata, mpixelclock);
1213 else
1214 ret = phy->configure(hdmi, pdata, mpixelclock);
1215 if (ret) {
1216 dev_err(hdmi->dev, "PHY configuration failed (clock %lu)\n",
1217 mpixelclock);
1218 return ret;
1219 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001220
Laurent Pinchart181e0ef2017-03-06 01:35:57 +02001221 return dw_hdmi_phy_power_on(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001222}
1223
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001224static int dw_hdmi_phy_init(struct dw_hdmi *hdmi, void *data,
1225 struct drm_display_mode *mode)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001226{
1227 int i, ret;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001228
1229 /* HDMI Phy spec says to do the phy initialization sequence twice */
1230 for (i = 0; i < 2; i++) {
Andy Yanb21f4b62014-12-05 14:26:31 +08001231 dw_hdmi_phy_sel_data_en_pol(hdmi, 1);
1232 dw_hdmi_phy_sel_interface_control(hdmi, 0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001233
Laurent Pinchart8b9e1c02017-03-03 19:19:59 +02001234 ret = hdmi_phy_configure(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001235 if (ret)
1236 return ret;
1237 }
1238
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001239 return 0;
1240}
1241
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001242static void dw_hdmi_phy_disable(struct dw_hdmi *hdmi, void *data)
1243{
1244 dw_hdmi_phy_power_off(hdmi);
1245}
1246
1247static enum drm_connector_status dw_hdmi_phy_read_hpd(struct dw_hdmi *hdmi,
1248 void *data)
1249{
1250 return hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_HPD ?
1251 connector_status_connected : connector_status_disconnected;
1252}
1253
Neil Armstrong386d3292017-04-04 14:31:59 +02001254static void dw_hdmi_phy_update_hpd(struct dw_hdmi *hdmi, void *data,
1255 bool force, bool disabled, bool rxsense)
1256{
1257 u8 old_mask = hdmi->phy_mask;
1258
1259 if (force || disabled || !rxsense)
1260 hdmi->phy_mask |= HDMI_PHY_RX_SENSE;
1261 else
1262 hdmi->phy_mask &= ~HDMI_PHY_RX_SENSE;
1263
1264 if (old_mask != hdmi->phy_mask)
1265 hdmi_writeb(hdmi, hdmi->phy_mask, HDMI_PHY_MASK0);
1266}
1267
1268static void dw_hdmi_phy_setup_hpd(struct dw_hdmi *hdmi, void *data)
1269{
1270 /*
1271 * Configure the PHY RX SENSE and HPD interrupts polarities and clear
1272 * any pending interrupt.
1273 */
1274 hdmi_writeb(hdmi, HDMI_PHY_HPD | HDMI_PHY_RX_SENSE, HDMI_PHY_POL0);
1275 hdmi_writeb(hdmi, HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE,
1276 HDMI_IH_PHY_STAT0);
1277
1278 /* Enable cable hot plug irq. */
1279 hdmi_writeb(hdmi, hdmi->phy_mask, HDMI_PHY_MASK0);
1280
1281 /* Clear and unmute interrupts. */
1282 hdmi_writeb(hdmi, HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE,
1283 HDMI_IH_PHY_STAT0);
1284 hdmi_writeb(hdmi, ~(HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE),
1285 HDMI_IH_MUTE_PHY_STAT0);
1286}
1287
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001288static const struct dw_hdmi_phy_ops dw_hdmi_synopsys_phy_ops = {
1289 .init = dw_hdmi_phy_init,
1290 .disable = dw_hdmi_phy_disable,
1291 .read_hpd = dw_hdmi_phy_read_hpd,
Neil Armstrong386d3292017-04-04 14:31:59 +02001292 .update_hpd = dw_hdmi_phy_update_hpd,
1293 .setup_hpd = dw_hdmi_phy_setup_hpd,
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001294};
1295
1296/* -----------------------------------------------------------------------------
1297 * HDMI TX Setup
1298 */
1299
Andy Yanb21f4b62014-12-05 14:26:31 +08001300static void hdmi_tx_hdcp_config(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001301{
Russell King812bc612013-11-04 12:42:02 +00001302 u8 de;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001303
1304 if (hdmi->hdmi_data.video_mode.mdataenablepolarity)
1305 de = HDMI_A_VIDPOLCFG_DATAENPOL_ACTIVE_HIGH;
1306 else
1307 de = HDMI_A_VIDPOLCFG_DATAENPOL_ACTIVE_LOW;
1308
1309 /* disable rx detect */
Russell King812bc612013-11-04 12:42:02 +00001310 hdmi_modb(hdmi, HDMI_A_HDCPCFG0_RXDETECT_DISABLE,
1311 HDMI_A_HDCPCFG0_RXDETECT_MASK, HDMI_A_HDCPCFG0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001312
Russell King812bc612013-11-04 12:42:02 +00001313 hdmi_modb(hdmi, de, HDMI_A_VIDPOLCFG_DATAENPOL_MASK, HDMI_A_VIDPOLCFG);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001314
Russell King812bc612013-11-04 12:42:02 +00001315 hdmi_modb(hdmi, HDMI_A_HDCPCFG1_ENCRYPTIONDISABLE_DISABLE,
1316 HDMI_A_HDCPCFG1_ENCRYPTIONDISABLE_MASK, HDMI_A_HDCPCFG1);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001317}
1318
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001319static void hdmi_config_AVI(struct dw_hdmi *hdmi, struct drm_display_mode *mode)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001320{
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001321 struct hdmi_avi_infoframe frame;
1322 u8 val;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001323
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001324 /* Initialise info frame from DRM mode */
Shashank Sharma0c1f5282017-07-13 21:03:07 +05301325 drm_hdmi_avi_infoframe_from_display_mode(&frame, mode, false);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001326
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001327 if (hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format))
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001328 frame.colorspace = HDMI_COLORSPACE_YUV444;
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001329 else if (hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format))
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001330 frame.colorspace = HDMI_COLORSPACE_YUV422;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001331 else
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001332 frame.colorspace = HDMI_COLORSPACE_RGB;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001333
1334 /* Set up colorimetry */
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001335 switch (hdmi->hdmi_data.enc_out_encoding) {
1336 case V4L2_YCBCR_ENC_601:
1337 if (hdmi->hdmi_data.enc_in_encoding == V4L2_YCBCR_ENC_XV601)
1338 frame.colorimetry = HDMI_COLORIMETRY_EXTENDED;
1339 else
1340 frame.colorimetry = HDMI_COLORIMETRY_ITU_601;
1341 frame.extended_colorimetry =
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001342 HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
Dan Carpenterf40d6562017-04-06 08:21:32 +03001343 break;
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001344 case V4L2_YCBCR_ENC_709:
1345 if (hdmi->hdmi_data.enc_in_encoding == V4L2_YCBCR_ENC_XV709)
1346 frame.colorimetry = HDMI_COLORIMETRY_EXTENDED;
1347 else
1348 frame.colorimetry = HDMI_COLORIMETRY_ITU_709;
1349 frame.extended_colorimetry =
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001350 HDMI_EXTENDED_COLORIMETRY_XV_YCC_709;
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001351 break;
1352 default: /* Carries no data */
1353 frame.colorimetry = HDMI_COLORIMETRY_ITU_601;
1354 frame.extended_colorimetry =
1355 HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
1356 break;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001357 }
1358
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001359 frame.scan_mode = HDMI_SCAN_MODE_NONE;
1360
1361 /*
1362 * The Designware IP uses a different byte format from standard
1363 * AVI info frames, though generally the bits are in the correct
1364 * bytes.
1365 */
1366
1367 /*
Jose Abreub0118e72016-08-29 10:30:51 +01001368 * AVI data byte 1 differences: Colorspace in bits 0,1 rather than 5,6,
1369 * scan info in bits 4,5 rather than 0,1 and active aspect present in
1370 * bit 6 rather than 4.
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001371 */
Jose Abreub0118e72016-08-29 10:30:51 +01001372 val = (frame.scan_mode & 3) << 4 | (frame.colorspace & 3);
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001373 if (frame.active_aspect & 15)
1374 val |= HDMI_FC_AVICONF0_ACTIVE_FMT_INFO_PRESENT;
1375 if (frame.top_bar || frame.bottom_bar)
1376 val |= HDMI_FC_AVICONF0_BAR_DATA_HORIZ_BAR;
1377 if (frame.left_bar || frame.right_bar)
1378 val |= HDMI_FC_AVICONF0_BAR_DATA_VERT_BAR;
1379 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF0);
1380
1381 /* AVI data byte 2 differences: none */
1382 val = ((frame.colorimetry & 0x3) << 6) |
1383 ((frame.picture_aspect & 0x3) << 4) |
1384 (frame.active_aspect & 0xf);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001385 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF1);
1386
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001387 /* AVI data byte 3 differences: none */
1388 val = ((frame.extended_colorimetry & 0x7) << 4) |
1389 ((frame.quantization_range & 0x3) << 2) |
1390 (frame.nups & 0x3);
1391 if (frame.itc)
1392 val |= HDMI_FC_AVICONF2_IT_CONTENT_VALID;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001393 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF2);
1394
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001395 /* AVI data byte 4 differences: none */
1396 val = frame.video_code & 0x7f;
1397 hdmi_writeb(hdmi, val, HDMI_FC_AVIVID);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001398
1399 /* AVI Data Byte 5- set up input and output pixel repetition */
1400 val = (((hdmi->hdmi_data.video_mode.mpixelrepetitioninput + 1) <<
1401 HDMI_FC_PRCONF_INCOMING_PR_FACTOR_OFFSET) &
1402 HDMI_FC_PRCONF_INCOMING_PR_FACTOR_MASK) |
1403 ((hdmi->hdmi_data.video_mode.mpixelrepetitionoutput <<
1404 HDMI_FC_PRCONF_OUTPUT_PR_FACTOR_OFFSET) &
1405 HDMI_FC_PRCONF_OUTPUT_PR_FACTOR_MASK);
1406 hdmi_writeb(hdmi, val, HDMI_FC_PRCONF);
1407
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001408 /*
1409 * AVI data byte 5 differences: content type in 0,1 rather than 4,5,
1410 * ycc range in bits 2,3 rather than 6,7
1411 */
1412 val = ((frame.ycc_quantization_range & 0x3) << 2) |
1413 (frame.content_type & 0x3);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001414 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF3);
1415
1416 /* AVI Data Bytes 6-13 */
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001417 hdmi_writeb(hdmi, frame.top_bar & 0xff, HDMI_FC_AVIETB0);
1418 hdmi_writeb(hdmi, (frame.top_bar >> 8) & 0xff, HDMI_FC_AVIETB1);
1419 hdmi_writeb(hdmi, frame.bottom_bar & 0xff, HDMI_FC_AVISBB0);
1420 hdmi_writeb(hdmi, (frame.bottom_bar >> 8) & 0xff, HDMI_FC_AVISBB1);
1421 hdmi_writeb(hdmi, frame.left_bar & 0xff, HDMI_FC_AVIELB0);
1422 hdmi_writeb(hdmi, (frame.left_bar >> 8) & 0xff, HDMI_FC_AVIELB1);
1423 hdmi_writeb(hdmi, frame.right_bar & 0xff, HDMI_FC_AVISRB0);
1424 hdmi_writeb(hdmi, (frame.right_bar >> 8) & 0xff, HDMI_FC_AVISRB1);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001425}
1426
Nickey Yang9aa1eca2017-03-21 15:36:17 +08001427static void hdmi_config_vendor_specific_infoframe(struct dw_hdmi *hdmi,
1428 struct drm_display_mode *mode)
1429{
1430 struct hdmi_vendor_infoframe frame;
1431 u8 buffer[10];
1432 ssize_t err;
1433
1434 err = drm_hdmi_vendor_infoframe_from_display_mode(&frame, mode);
1435 if (err < 0)
1436 /*
1437 * Going into that statement does not means vendor infoframe
1438 * fails. It just informed us that vendor infoframe is not
1439 * needed for the selected mode. Only 4k or stereoscopic 3D
1440 * mode requires vendor infoframe. So just simply return.
1441 */
1442 return;
1443
1444 err = hdmi_vendor_infoframe_pack(&frame, buffer, sizeof(buffer));
1445 if (err < 0) {
1446 dev_err(hdmi->dev, "Failed to pack vendor infoframe: %zd\n",
1447 err);
1448 return;
1449 }
1450 hdmi_mask_writeb(hdmi, 0, HDMI_FC_DATAUTO0, HDMI_FC_DATAUTO0_VSD_OFFSET,
1451 HDMI_FC_DATAUTO0_VSD_MASK);
1452
1453 /* Set the length of HDMI vendor specific InfoFrame payload */
1454 hdmi_writeb(hdmi, buffer[2], HDMI_FC_VSDSIZE);
1455
1456 /* Set 24bit IEEE Registration Identifier */
1457 hdmi_writeb(hdmi, buffer[4], HDMI_FC_VSDIEEEID0);
1458 hdmi_writeb(hdmi, buffer[5], HDMI_FC_VSDIEEEID1);
1459 hdmi_writeb(hdmi, buffer[6], HDMI_FC_VSDIEEEID2);
1460
1461 /* Set HDMI_Video_Format and HDMI_VIC/3D_Structure */
1462 hdmi_writeb(hdmi, buffer[7], HDMI_FC_VSDPAYLOAD0);
1463 hdmi_writeb(hdmi, buffer[8], HDMI_FC_VSDPAYLOAD1);
1464
1465 if (frame.s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF)
1466 hdmi_writeb(hdmi, buffer[9], HDMI_FC_VSDPAYLOAD2);
1467
1468 /* Packet frame interpolation */
1469 hdmi_writeb(hdmi, 1, HDMI_FC_DATAUTO1);
1470
1471 /* Auto packets per frame and line spacing */
1472 hdmi_writeb(hdmi, 0x11, HDMI_FC_DATAUTO2);
1473
1474 /* Configures the Frame Composer On RDRB mode */
1475 hdmi_mask_writeb(hdmi, 1, HDMI_FC_DATAUTO0, HDMI_FC_DATAUTO0_VSD_OFFSET,
1476 HDMI_FC_DATAUTO0_VSD_MASK);
1477}
1478
Andy Yanb21f4b62014-12-05 14:26:31 +08001479static void hdmi_av_composer(struct dw_hdmi *hdmi,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001480 const struct drm_display_mode *mode)
1481{
1482 u8 inv_val;
1483 struct hdmi_vmode *vmode = &hdmi->hdmi_data.video_mode;
1484 int hblank, vblank, h_de_hs, v_de_vs, hsync_len, vsync_len;
Russell Kinge80b9f42015-07-21 11:08:25 +01001485 unsigned int vdisplay;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001486
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001487 vmode->mpixelclock = mode->clock * 1000;
1488
1489 dev_dbg(hdmi->dev, "final pixclk = %d\n", vmode->mpixelclock);
1490
1491 /* Set up HDMI_FC_INVIDCONF */
1492 inv_val = (hdmi->hdmi_data.hdcp_enable ?
1493 HDMI_FC_INVIDCONF_HDCP_KEEPOUT_ACTIVE :
1494 HDMI_FC_INVIDCONF_HDCP_KEEPOUT_INACTIVE);
1495
Russell Kingb91eee82015-03-27 23:27:17 +00001496 inv_val |= mode->flags & DRM_MODE_FLAG_PVSYNC ?
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001497 HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_HIGH :
Russell Kingb91eee82015-03-27 23:27:17 +00001498 HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_LOW;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001499
Russell Kingb91eee82015-03-27 23:27:17 +00001500 inv_val |= mode->flags & DRM_MODE_FLAG_PHSYNC ?
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001501 HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_HIGH :
Russell Kingb91eee82015-03-27 23:27:17 +00001502 HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_LOW;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001503
1504 inv_val |= (vmode->mdataenablepolarity ?
1505 HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_HIGH :
1506 HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_LOW);
1507
1508 if (hdmi->vic == 39)
1509 inv_val |= HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_HIGH;
1510 else
Russell Kingb91eee82015-03-27 23:27:17 +00001511 inv_val |= mode->flags & DRM_MODE_FLAG_INTERLACE ?
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001512 HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_HIGH :
Russell Kingb91eee82015-03-27 23:27:17 +00001513 HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_LOW;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001514
Russell Kingb91eee82015-03-27 23:27:17 +00001515 inv_val |= mode->flags & DRM_MODE_FLAG_INTERLACE ?
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001516 HDMI_FC_INVIDCONF_IN_I_P_INTERLACED :
Russell Kingb91eee82015-03-27 23:27:17 +00001517 HDMI_FC_INVIDCONF_IN_I_P_PROGRESSIVE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001518
Russell King05b13422015-07-21 15:35:52 +01001519 inv_val |= hdmi->sink_is_hdmi ?
1520 HDMI_FC_INVIDCONF_DVI_MODEZ_HDMI_MODE :
1521 HDMI_FC_INVIDCONF_DVI_MODEZ_DVI_MODE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001522
1523 hdmi_writeb(hdmi, inv_val, HDMI_FC_INVIDCONF);
1524
Russell Kinge80b9f42015-07-21 11:08:25 +01001525 vdisplay = mode->vdisplay;
1526 vblank = mode->vtotal - mode->vdisplay;
1527 v_de_vs = mode->vsync_start - mode->vdisplay;
1528 vsync_len = mode->vsync_end - mode->vsync_start;
1529
1530 /*
1531 * When we're setting an interlaced mode, we need
1532 * to adjust the vertical timing to suit.
1533 */
1534 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
1535 vdisplay /= 2;
1536 vblank /= 2;
1537 v_de_vs /= 2;
1538 vsync_len /= 2;
1539 }
1540
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001541 /* Set up horizontal active pixel width */
1542 hdmi_writeb(hdmi, mode->hdisplay >> 8, HDMI_FC_INHACTV1);
1543 hdmi_writeb(hdmi, mode->hdisplay, HDMI_FC_INHACTV0);
1544
1545 /* Set up vertical active lines */
Russell Kinge80b9f42015-07-21 11:08:25 +01001546 hdmi_writeb(hdmi, vdisplay >> 8, HDMI_FC_INVACTV1);
1547 hdmi_writeb(hdmi, vdisplay, HDMI_FC_INVACTV0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001548
1549 /* Set up horizontal blanking pixel region width */
1550 hblank = mode->htotal - mode->hdisplay;
1551 hdmi_writeb(hdmi, hblank >> 8, HDMI_FC_INHBLANK1);
1552 hdmi_writeb(hdmi, hblank, HDMI_FC_INHBLANK0);
1553
1554 /* Set up vertical blanking pixel region width */
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001555 hdmi_writeb(hdmi, vblank, HDMI_FC_INVBLANK);
1556
1557 /* Set up HSYNC active edge delay width (in pixel clks) */
1558 h_de_hs = mode->hsync_start - mode->hdisplay;
1559 hdmi_writeb(hdmi, h_de_hs >> 8, HDMI_FC_HSYNCINDELAY1);
1560 hdmi_writeb(hdmi, h_de_hs, HDMI_FC_HSYNCINDELAY0);
1561
1562 /* Set up VSYNC active edge delay (in lines) */
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001563 hdmi_writeb(hdmi, v_de_vs, HDMI_FC_VSYNCINDELAY);
1564
1565 /* Set up HSYNC active pulse width (in pixel clks) */
1566 hsync_len = mode->hsync_end - mode->hsync_start;
1567 hdmi_writeb(hdmi, hsync_len >> 8, HDMI_FC_HSYNCINWIDTH1);
1568 hdmi_writeb(hdmi, hsync_len, HDMI_FC_HSYNCINWIDTH0);
1569
1570 /* Set up VSYNC active edge delay (in lines) */
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001571 hdmi_writeb(hdmi, vsync_len, HDMI_FC_VSYNCINWIDTH);
1572}
1573
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001574/* HDMI Initialization Step B.4 */
Andy Yanb21f4b62014-12-05 14:26:31 +08001575static void dw_hdmi_enable_video_path(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001576{
1577 u8 clkdis;
1578
1579 /* control period minimum duration */
1580 hdmi_writeb(hdmi, 12, HDMI_FC_CTRLDUR);
1581 hdmi_writeb(hdmi, 32, HDMI_FC_EXCTRLDUR);
1582 hdmi_writeb(hdmi, 1, HDMI_FC_EXCTRLSPAC);
1583
1584 /* Set to fill TMDS data channels */
1585 hdmi_writeb(hdmi, 0x0B, HDMI_FC_CH0PREAM);
1586 hdmi_writeb(hdmi, 0x16, HDMI_FC_CH1PREAM);
1587 hdmi_writeb(hdmi, 0x21, HDMI_FC_CH2PREAM);
1588
1589 /* Enable pixel clock and tmds data path */
1590 clkdis = 0x7F;
1591 clkdis &= ~HDMI_MC_CLKDIS_PIXELCLK_DISABLE;
1592 hdmi_writeb(hdmi, clkdis, HDMI_MC_CLKDIS);
1593
1594 clkdis &= ~HDMI_MC_CLKDIS_TMDSCLK_DISABLE;
1595 hdmi_writeb(hdmi, clkdis, HDMI_MC_CLKDIS);
1596
1597 /* Enable csc path */
1598 if (is_color_space_conversion(hdmi)) {
1599 clkdis &= ~HDMI_MC_CLKDIS_CSCCLK_DISABLE;
1600 hdmi_writeb(hdmi, clkdis, HDMI_MC_CLKDIS);
1601 }
Laurent Pinchart8b9e1c02017-03-03 19:19:59 +02001602
Neil Armstrong14247d72017-03-03 19:20:00 +02001603 /* Enable color space conversion if needed */
1604 if (is_color_space_conversion(hdmi))
Laurent Pinchart8b9e1c02017-03-03 19:19:59 +02001605 hdmi_writeb(hdmi, HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_IN_PATH,
1606 HDMI_MC_FLOWCTRL);
1607 else
1608 hdmi_writeb(hdmi, HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_BYPASS,
1609 HDMI_MC_FLOWCTRL);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001610}
1611
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001612/* Workaround to clear the overflow condition */
Andy Yanb21f4b62014-12-05 14:26:31 +08001613static void dw_hdmi_clear_overflow(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001614{
Laurent Pinchartbe41fc52017-01-17 10:29:05 +02001615 unsigned int count;
1616 unsigned int i;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001617 u8 val;
1618
Laurent Pinchartbe41fc52017-01-17 10:29:05 +02001619 /*
1620 * Under some circumstances the Frame Composer arithmetic unit can miss
1621 * an FC register write due to being busy processing the previous one.
1622 * The issue can be worked around by issuing a TMDS software reset and
1623 * then write one of the FC registers several times.
1624 *
1625 * The number of iterations matters and depends on the HDMI TX revision
1626 * (and possibly on the platform). So far only i.MX6Q (v1.30a) and
1627 * i.MX6DL (v1.31a) have been identified as needing the workaround, with
1628 * 4 and 1 iterations respectively.
1629 */
1630
1631 switch (hdmi->version) {
1632 case 0x130a:
1633 count = 4;
1634 break;
1635 case 0x131a:
1636 count = 1;
1637 break;
1638 default:
1639 return;
1640 }
1641
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001642 /* TMDS software reset */
1643 hdmi_writeb(hdmi, (u8)~HDMI_MC_SWRSTZ_TMDSSWRST_REQ, HDMI_MC_SWRSTZ);
1644
1645 val = hdmi_readb(hdmi, HDMI_FC_INVIDCONF);
Laurent Pinchartbe41fc52017-01-17 10:29:05 +02001646 for (i = 0; i < count; i++)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001647 hdmi_writeb(hdmi, val, HDMI_FC_INVIDCONF);
1648}
1649
Andy Yanb21f4b62014-12-05 14:26:31 +08001650static void hdmi_enable_overflow_interrupts(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001651{
1652 hdmi_writeb(hdmi, 0, HDMI_FC_MASK2);
1653 hdmi_writeb(hdmi, 0, HDMI_IH_MUTE_FC_STAT2);
1654}
1655
Andy Yanb21f4b62014-12-05 14:26:31 +08001656static void hdmi_disable_overflow_interrupts(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001657{
1658 hdmi_writeb(hdmi, HDMI_IH_MUTE_FC_STAT2_OVERFLOW_MASK,
1659 HDMI_IH_MUTE_FC_STAT2);
1660}
1661
Andy Yanb21f4b62014-12-05 14:26:31 +08001662static int dw_hdmi_setup(struct dw_hdmi *hdmi, struct drm_display_mode *mode)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001663{
1664 int ret;
1665
1666 hdmi_disable_overflow_interrupts(hdmi);
1667
1668 hdmi->vic = drm_match_cea_mode(mode);
1669
1670 if (!hdmi->vic) {
1671 dev_dbg(hdmi->dev, "Non-CEA mode used in HDMI\n");
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001672 } else {
1673 dev_dbg(hdmi->dev, "CEA mode used vic=%d\n", hdmi->vic);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001674 }
1675
1676 if ((hdmi->vic == 6) || (hdmi->vic == 7) ||
Andy Yanb5878332014-12-05 14:23:52 +08001677 (hdmi->vic == 21) || (hdmi->vic == 22) ||
1678 (hdmi->vic == 2) || (hdmi->vic == 3) ||
1679 (hdmi->vic == 17) || (hdmi->vic == 18))
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001680 hdmi->hdmi_data.enc_out_encoding = V4L2_YCBCR_ENC_601;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001681 else
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001682 hdmi->hdmi_data.enc_out_encoding = V4L2_YCBCR_ENC_709;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001683
Russell Kingd10ca822015-07-21 11:25:00 +01001684 hdmi->hdmi_data.video_mode.mpixelrepetitionoutput = 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001685 hdmi->hdmi_data.video_mode.mpixelrepetitioninput = 0;
1686
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001687 /* TOFIX: Get input format from plat data or fallback to RGB888 */
Neil Armstronge20c29a2017-04-06 11:34:04 +02001688 if (hdmi->plat_data->input_bus_format)
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001689 hdmi->hdmi_data.enc_in_bus_format =
1690 hdmi->plat_data->input_bus_format;
1691 else
1692 hdmi->hdmi_data.enc_in_bus_format = MEDIA_BUS_FMT_RGB888_1X24;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001693
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001694 /* TOFIX: Get input encoding from plat data or fallback to none */
Neil Armstronge20c29a2017-04-06 11:34:04 +02001695 if (hdmi->plat_data->input_bus_encoding)
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001696 hdmi->hdmi_data.enc_in_encoding =
1697 hdmi->plat_data->input_bus_encoding;
1698 else
1699 hdmi->hdmi_data.enc_in_encoding = V4L2_YCBCR_ENC_DEFAULT;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001700
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001701 /* TOFIX: Default to RGB888 output format */
1702 hdmi->hdmi_data.enc_out_bus_format = MEDIA_BUS_FMT_RGB888_1X24;
1703
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001704 hdmi->hdmi_data.pix_repet_factor = 0;
1705 hdmi->hdmi_data.hdcp_enable = 0;
1706 hdmi->hdmi_data.video_mode.mdataenablepolarity = true;
1707
1708 /* HDMI Initialization Step B.1 */
1709 hdmi_av_composer(hdmi, mode);
1710
1711 /* HDMI Initializateion Step B.2 */
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001712 ret = hdmi->phy.ops->init(hdmi, hdmi->phy.data, &hdmi->previous_mode);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001713 if (ret)
1714 return ret;
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001715 hdmi->phy.enabled = true;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001716
1717 /* HDMI Initialization Step B.3 */
Andy Yanb21f4b62014-12-05 14:26:31 +08001718 dw_hdmi_enable_video_path(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001719
Russell Kingf709ec02015-07-21 16:09:39 +01001720 if (hdmi->sink_has_audio) {
1721 dev_dbg(hdmi->dev, "sink has audio support\n");
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001722
1723 /* HDMI Initialization Step E - Configure audio */
1724 hdmi_clk_regenerator_update_pixel_clock(hdmi);
Romain Perier57fbc052017-04-20 14:34:34 +05301725 hdmi_enable_audio_clk(hdmi, true);
Russell Kingf709ec02015-07-21 16:09:39 +01001726 }
1727
1728 /* not for DVI mode */
1729 if (hdmi->sink_is_hdmi) {
1730 dev_dbg(hdmi->dev, "%s HDMI mode\n", __func__);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001731
1732 /* HDMI Initialization Step F - Configure AVI InfoFrame */
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001733 hdmi_config_AVI(hdmi, mode);
Nickey Yang9aa1eca2017-03-21 15:36:17 +08001734 hdmi_config_vendor_specific_infoframe(hdmi, mode);
Russell King05b13422015-07-21 15:35:52 +01001735 } else {
1736 dev_dbg(hdmi->dev, "%s DVI mode\n", __func__);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001737 }
1738
1739 hdmi_video_packetize(hdmi);
1740 hdmi_video_csc(hdmi);
1741 hdmi_video_sample(hdmi);
1742 hdmi_tx_hdcp_config(hdmi);
1743
Andy Yanb21f4b62014-12-05 14:26:31 +08001744 dw_hdmi_clear_overflow(hdmi);
Russell King05b13422015-07-21 15:35:52 +01001745 if (hdmi->cable_plugin && hdmi->sink_is_hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001746 hdmi_enable_overflow_interrupts(hdmi);
1747
1748 return 0;
1749}
1750
Laurent Pincharta23d6262017-04-04 14:31:56 +02001751static void dw_hdmi_setup_i2c(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001752{
1753 hdmi_writeb(hdmi, HDMI_PHY_I2CM_INT_ADDR_DONE_POL,
1754 HDMI_PHY_I2CM_INT_ADDR);
1755
1756 hdmi_writeb(hdmi, HDMI_PHY_I2CM_CTLINT_ADDR_NAC_POL |
1757 HDMI_PHY_I2CM_CTLINT_ADDR_ARBITRATION_POL,
1758 HDMI_PHY_I2CM_CTLINT_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001759}
1760
Andy Yanb21f4b62014-12-05 14:26:31 +08001761static void initialize_hdmi_ih_mutes(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001762{
1763 u8 ih_mute;
1764
1765 /*
1766 * Boot up defaults are:
1767 * HDMI_IH_MUTE = 0x03 (disabled)
1768 * HDMI_IH_MUTE_* = 0x00 (enabled)
1769 *
1770 * Disable top level interrupt bits in HDMI block
1771 */
1772 ih_mute = hdmi_readb(hdmi, HDMI_IH_MUTE) |
1773 HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |
1774 HDMI_IH_MUTE_MUTE_ALL_INTERRUPT;
1775
1776 hdmi_writeb(hdmi, ih_mute, HDMI_IH_MUTE);
1777
1778 /* by default mask all interrupts */
1779 hdmi_writeb(hdmi, 0xff, HDMI_VP_MASK);
1780 hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK0);
1781 hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK1);
1782 hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK2);
1783 hdmi_writeb(hdmi, 0xff, HDMI_PHY_MASK0);
1784 hdmi_writeb(hdmi, 0xff, HDMI_PHY_I2CM_INT_ADDR);
1785 hdmi_writeb(hdmi, 0xff, HDMI_PHY_I2CM_CTLINT_ADDR);
1786 hdmi_writeb(hdmi, 0xff, HDMI_AUD_INT);
1787 hdmi_writeb(hdmi, 0xff, HDMI_AUD_SPDIFINT);
1788 hdmi_writeb(hdmi, 0xff, HDMI_AUD_HBR_MASK);
1789 hdmi_writeb(hdmi, 0xff, HDMI_GP_MASK);
1790 hdmi_writeb(hdmi, 0xff, HDMI_A_APIINTMSK);
1791 hdmi_writeb(hdmi, 0xff, HDMI_CEC_MASK);
1792 hdmi_writeb(hdmi, 0xff, HDMI_I2CM_INT);
1793 hdmi_writeb(hdmi, 0xff, HDMI_I2CM_CTLINT);
1794
1795 /* Disable interrupts in the IH_MUTE_* registers */
1796 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT0);
1797 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT1);
1798 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT2);
1799 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_AS_STAT0);
1800 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_PHY_STAT0);
1801 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_I2CM_STAT0);
1802 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_CEC_STAT0);
1803 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_VP_STAT0);
1804 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_I2CMPHY_STAT0);
1805 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_AHBDMAAUD_STAT0);
1806
1807 /* Enable top level interrupt bits in HDMI block */
1808 ih_mute &= ~(HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |
1809 HDMI_IH_MUTE_MUTE_ALL_INTERRUPT);
1810 hdmi_writeb(hdmi, ih_mute, HDMI_IH_MUTE);
1811}
1812
Andy Yanb21f4b62014-12-05 14:26:31 +08001813static void dw_hdmi_poweron(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001814{
Russell King381f05a2015-06-05 15:25:08 +01001815 hdmi->bridge_is_on = true;
Andy Yanb21f4b62014-12-05 14:26:31 +08001816 dw_hdmi_setup(hdmi, &hdmi->previous_mode);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001817}
1818
Andy Yanb21f4b62014-12-05 14:26:31 +08001819static void dw_hdmi_poweroff(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001820{
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001821 if (hdmi->phy.enabled) {
1822 hdmi->phy.ops->disable(hdmi, hdmi->phy.data);
1823 hdmi->phy.enabled = false;
1824 }
1825
Russell King381f05a2015-06-05 15:25:08 +01001826 hdmi->bridge_is_on = false;
1827}
1828
1829static void dw_hdmi_update_power(struct dw_hdmi *hdmi)
1830{
1831 int force = hdmi->force;
1832
1833 if (hdmi->disabled) {
1834 force = DRM_FORCE_OFF;
1835 } else if (force == DRM_FORCE_UNSPECIFIED) {
Russell Kingaeac23b2015-06-05 13:46:22 +01001836 if (hdmi->rxsense)
Russell King381f05a2015-06-05 15:25:08 +01001837 force = DRM_FORCE_ON;
1838 else
1839 force = DRM_FORCE_OFF;
1840 }
1841
1842 if (force == DRM_FORCE_OFF) {
1843 if (hdmi->bridge_is_on)
1844 dw_hdmi_poweroff(hdmi);
1845 } else {
1846 if (!hdmi->bridge_is_on)
1847 dw_hdmi_poweron(hdmi);
1848 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001849}
1850
Russell Kingaeac23b2015-06-05 13:46:22 +01001851/*
1852 * Adjust the detection of RXSENSE according to whether we have a forced
1853 * connection mode enabled, or whether we have been disabled. There is
1854 * no point processing RXSENSE interrupts if we have a forced connection
1855 * state, or DRM has us disabled.
1856 *
1857 * We also disable rxsense interrupts when we think we're disconnected
1858 * to avoid floating TDMS signals giving false rxsense interrupts.
1859 *
1860 * Note: we still need to listen for HPD interrupts even when DRM has us
1861 * disabled so that we can detect a connect event.
1862 */
1863static void dw_hdmi_update_phy_mask(struct dw_hdmi *hdmi)
1864{
Neil Armstrong386d3292017-04-04 14:31:59 +02001865 if (hdmi->phy.ops->update_hpd)
1866 hdmi->phy.ops->update_hpd(hdmi, hdmi->phy.data,
1867 hdmi->force, hdmi->disabled,
1868 hdmi->rxsense);
Laurent Pincharta23d6262017-04-04 14:31:56 +02001869}
1870
Andy Yanb21f4b62014-12-05 14:26:31 +08001871static enum drm_connector_status
1872dw_hdmi_connector_detect(struct drm_connector *connector, bool force)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001873{
Andy Yanb21f4b62014-12-05 14:26:31 +08001874 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
Russell Kingd94905e2013-11-03 22:23:24 +00001875 connector);
Russell King98dbead2014-04-18 10:46:45 +01001876
Russell King381f05a2015-06-05 15:25:08 +01001877 mutex_lock(&hdmi->mutex);
1878 hdmi->force = DRM_FORCE_UNSPECIFIED;
1879 dw_hdmi_update_power(hdmi);
Russell Kingaeac23b2015-06-05 13:46:22 +01001880 dw_hdmi_update_phy_mask(hdmi);
Russell King381f05a2015-06-05 15:25:08 +01001881 mutex_unlock(&hdmi->mutex);
1882
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001883 return hdmi->phy.ops->read_hpd(hdmi, hdmi->phy.data);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001884}
1885
Andy Yanb21f4b62014-12-05 14:26:31 +08001886static int dw_hdmi_connector_get_modes(struct drm_connector *connector)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001887{
Andy Yanb21f4b62014-12-05 14:26:31 +08001888 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001889 connector);
1890 struct edid *edid;
Doug Anderson6c7e66e2015-06-04 11:04:36 -07001891 int ret = 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001892
1893 if (!hdmi->ddc)
1894 return 0;
1895
1896 edid = drm_get_edid(connector, hdmi->ddc);
1897 if (edid) {
1898 dev_dbg(hdmi->dev, "got edid: width[%d] x height[%d]\n",
1899 edid->width_cm, edid->height_cm);
1900
Russell King05b13422015-07-21 15:35:52 +01001901 hdmi->sink_is_hdmi = drm_detect_hdmi_monitor(edid);
Russell Kingf709ec02015-07-21 16:09:39 +01001902 hdmi->sink_has_audio = drm_detect_monitor_audio(edid);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001903 drm_mode_connector_update_edid_property(connector, edid);
Russell Kinge84b8d72017-07-31 15:29:41 +01001904 cec_notifier_set_phys_addr_from_edid(hdmi->cec_notifier, edid);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001905 ret = drm_add_edid_modes(connector, edid);
Russell Kingf5ce4052013-11-07 16:06:01 +00001906 /* Store the ELD */
1907 drm_edid_to_eld(connector, edid);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001908 kfree(edid);
1909 } else {
1910 dev_dbg(hdmi->dev, "failed to get edid\n");
1911 }
1912
Doug Anderson6c7e66e2015-06-04 11:04:36 -07001913 return ret;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001914}
1915
Russell King381f05a2015-06-05 15:25:08 +01001916static void dw_hdmi_connector_force(struct drm_connector *connector)
1917{
1918 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
1919 connector);
1920
1921 mutex_lock(&hdmi->mutex);
1922 hdmi->force = connector->force;
1923 dw_hdmi_update_power(hdmi);
Russell Kingaeac23b2015-06-05 13:46:22 +01001924 dw_hdmi_update_phy_mask(hdmi);
Russell King381f05a2015-06-05 15:25:08 +01001925 mutex_unlock(&hdmi->mutex);
1926}
1927
Ville Syrjälädae91e42015-12-15 12:21:02 +01001928static const struct drm_connector_funcs dw_hdmi_connector_funcs = {
Mark Yao2c5b2cc2015-11-30 18:33:40 +08001929 .dpms = drm_atomic_helper_connector_dpms,
1930 .fill_modes = drm_helper_probe_single_connector_modes,
1931 .detect = dw_hdmi_connector_detect,
Marek Vasutfdd83262016-10-05 16:31:33 +02001932 .destroy = drm_connector_cleanup,
Mark Yao2c5b2cc2015-11-30 18:33:40 +08001933 .force = dw_hdmi_connector_force,
1934 .reset = drm_atomic_helper_connector_reset,
1935 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1936 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1937};
1938
Ville Syrjälädae91e42015-12-15 12:21:02 +01001939static const struct drm_connector_helper_funcs dw_hdmi_connector_helper_funcs = {
Andy Yanb21f4b62014-12-05 14:26:31 +08001940 .get_modes = dw_hdmi_connector_get_modes,
Boris Brezillonc2a441f2016-06-07 13:48:15 +02001941 .best_encoder = drm_atomic_helper_best_encoder,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001942};
1943
Laurent Pinchartd2ae94a2017-01-17 10:28:59 +02001944static int dw_hdmi_bridge_attach(struct drm_bridge *bridge)
1945{
1946 struct dw_hdmi *hdmi = bridge->driver_private;
1947 struct drm_encoder *encoder = bridge->encoder;
1948 struct drm_connector *connector = &hdmi->connector;
1949
1950 connector->interlace_allowed = 1;
1951 connector->polled = DRM_CONNECTOR_POLL_HPD;
1952
1953 drm_connector_helper_add(connector, &dw_hdmi_connector_helper_funcs);
1954
1955 drm_connector_init(bridge->dev, connector, &dw_hdmi_connector_funcs,
1956 DRM_MODE_CONNECTOR_HDMIA);
1957
1958 drm_mode_connector_attach_encoder(connector, encoder);
1959
1960 return 0;
1961}
1962
Jose Abreub0febde2017-05-25 15:19:19 +01001963static enum drm_mode_status
1964dw_hdmi_bridge_mode_valid(struct drm_bridge *bridge,
1965 const struct drm_display_mode *mode)
Romain Perier6ce2ca52017-04-07 14:17:43 +02001966{
1967 struct dw_hdmi *hdmi = bridge->driver_private;
1968 struct drm_connector *connector = &hdmi->connector;
Jose Abreub0febde2017-05-25 15:19:19 +01001969 enum drm_mode_status mode_status = MODE_OK;
Romain Perier6ce2ca52017-04-07 14:17:43 +02001970
Jose Abreub0febde2017-05-25 15:19:19 +01001971 /* We don't support double-clocked modes */
1972 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
1973 return MODE_BAD;
1974
1975 if (hdmi->plat_data->mode_valid)
1976 mode_status = hdmi->plat_data->mode_valid(connector, mode);
1977
1978 return mode_status;
Romain Perier6ce2ca52017-04-07 14:17:43 +02001979}
1980
Laurent Pinchartfd30b382017-01-17 10:28:58 +02001981static void dw_hdmi_bridge_mode_set(struct drm_bridge *bridge,
1982 struct drm_display_mode *orig_mode,
1983 struct drm_display_mode *mode)
1984{
1985 struct dw_hdmi *hdmi = bridge->driver_private;
1986
1987 mutex_lock(&hdmi->mutex);
1988
1989 /* Store the display mode for plugin/DKMS poweron events */
1990 memcpy(&hdmi->previous_mode, mode, sizeof(hdmi->previous_mode));
1991
1992 mutex_unlock(&hdmi->mutex);
1993}
1994
1995static void dw_hdmi_bridge_disable(struct drm_bridge *bridge)
1996{
1997 struct dw_hdmi *hdmi = bridge->driver_private;
1998
1999 mutex_lock(&hdmi->mutex);
2000 hdmi->disabled = true;
2001 dw_hdmi_update_power(hdmi);
2002 dw_hdmi_update_phy_mask(hdmi);
2003 mutex_unlock(&hdmi->mutex);
2004}
2005
2006static void dw_hdmi_bridge_enable(struct drm_bridge *bridge)
2007{
2008 struct dw_hdmi *hdmi = bridge->driver_private;
2009
2010 mutex_lock(&hdmi->mutex);
2011 hdmi->disabled = false;
2012 dw_hdmi_update_power(hdmi);
2013 dw_hdmi_update_phy_mask(hdmi);
2014 mutex_unlock(&hdmi->mutex);
2015}
2016
Ville Syrjälädae91e42015-12-15 12:21:02 +01002017static const struct drm_bridge_funcs dw_hdmi_bridge_funcs = {
Laurent Pinchartd2ae94a2017-01-17 10:28:59 +02002018 .attach = dw_hdmi_bridge_attach,
Andy Yanb21f4b62014-12-05 14:26:31 +08002019 .enable = dw_hdmi_bridge_enable,
2020 .disable = dw_hdmi_bridge_disable,
Andy Yanb21f4b62014-12-05 14:26:31 +08002021 .mode_set = dw_hdmi_bridge_mode_set,
Jose Abreub0febde2017-05-25 15:19:19 +01002022 .mode_valid = dw_hdmi_bridge_mode_valid,
Andy Yan3d1b35a2014-12-05 14:25:05 +08002023};
2024
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002025static irqreturn_t dw_hdmi_i2c_irq(struct dw_hdmi *hdmi)
2026{
2027 struct dw_hdmi_i2c *i2c = hdmi->i2c;
2028 unsigned int stat;
2029
2030 stat = hdmi_readb(hdmi, HDMI_IH_I2CM_STAT0);
2031 if (!stat)
2032 return IRQ_NONE;
2033
2034 hdmi_writeb(hdmi, stat, HDMI_IH_I2CM_STAT0);
2035
2036 i2c->stat = stat;
2037
2038 complete(&i2c->cmp);
2039
2040 return IRQ_HANDLED;
2041}
2042
Andy Yanb21f4b62014-12-05 14:26:31 +08002043static irqreturn_t dw_hdmi_hardirq(int irq, void *dev_id)
Russell Kingd94905e2013-11-03 22:23:24 +00002044{
Andy Yanb21f4b62014-12-05 14:26:31 +08002045 struct dw_hdmi *hdmi = dev_id;
Russell Kingd94905e2013-11-03 22:23:24 +00002046 u8 intr_stat;
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002047 irqreturn_t ret = IRQ_NONE;
2048
2049 if (hdmi->i2c)
2050 ret = dw_hdmi_i2c_irq(hdmi);
Russell Kingd94905e2013-11-03 22:23:24 +00002051
2052 intr_stat = hdmi_readb(hdmi, HDMI_IH_PHY_STAT0);
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002053 if (intr_stat) {
Russell Kingd94905e2013-11-03 22:23:24 +00002054 hdmi_writeb(hdmi, ~0, HDMI_IH_MUTE_PHY_STAT0);
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002055 return IRQ_WAKE_THREAD;
2056 }
Russell Kingd94905e2013-11-03 22:23:24 +00002057
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002058 return ret;
Russell Kingd94905e2013-11-03 22:23:24 +00002059}
2060
Neil Armstrong386d3292017-04-04 14:31:59 +02002061void __dw_hdmi_setup_rx_sense(struct dw_hdmi *hdmi, bool hpd, bool rx_sense)
2062{
2063 mutex_lock(&hdmi->mutex);
2064
2065 if (!hdmi->force) {
2066 /*
2067 * If the RX sense status indicates we're disconnected,
2068 * clear the software rxsense status.
2069 */
2070 if (!rx_sense)
2071 hdmi->rxsense = false;
2072
2073 /*
2074 * Only set the software rxsense status when both
2075 * rxsense and hpd indicates we're connected.
2076 * This avoids what seems to be bad behaviour in
2077 * at least iMX6S versions of the phy.
2078 */
2079 if (hpd)
2080 hdmi->rxsense = true;
2081
2082 dw_hdmi_update_power(hdmi);
2083 dw_hdmi_update_phy_mask(hdmi);
2084 }
2085 mutex_unlock(&hdmi->mutex);
2086}
2087
2088void dw_hdmi_setup_rx_sense(struct device *dev, bool hpd, bool rx_sense)
2089{
2090 struct dw_hdmi *hdmi = dev_get_drvdata(dev);
2091
2092 __dw_hdmi_setup_rx_sense(hdmi, hpd, rx_sense);
2093}
2094EXPORT_SYMBOL_GPL(dw_hdmi_setup_rx_sense);
2095
Andy Yanb21f4b62014-12-05 14:26:31 +08002096static irqreturn_t dw_hdmi_irq(int irq, void *dev_id)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002097{
Andy Yanb21f4b62014-12-05 14:26:31 +08002098 struct dw_hdmi *hdmi = dev_id;
Russell Kingaeac23b2015-06-05 13:46:22 +01002099 u8 intr_stat, phy_int_pol, phy_pol_mask, phy_stat;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002100
2101 intr_stat = hdmi_readb(hdmi, HDMI_IH_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002102 phy_int_pol = hdmi_readb(hdmi, HDMI_PHY_POL0);
Russell Kingaeac23b2015-06-05 13:46:22 +01002103 phy_stat = hdmi_readb(hdmi, HDMI_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002104
Russell Kingaeac23b2015-06-05 13:46:22 +01002105 phy_pol_mask = 0;
2106 if (intr_stat & HDMI_IH_PHY_STAT0_HPD)
2107 phy_pol_mask |= HDMI_PHY_HPD;
2108 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE0)
2109 phy_pol_mask |= HDMI_PHY_RX_SENSE0;
2110 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE1)
2111 phy_pol_mask |= HDMI_PHY_RX_SENSE1;
2112 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE2)
2113 phy_pol_mask |= HDMI_PHY_RX_SENSE2;
2114 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE3)
2115 phy_pol_mask |= HDMI_PHY_RX_SENSE3;
2116
2117 if (phy_pol_mask)
2118 hdmi_modb(hdmi, ~phy_int_pol, phy_pol_mask, HDMI_PHY_POL0);
2119
2120 /*
2121 * RX sense tells us whether the TDMS transmitters are detecting
2122 * load - in other words, there's something listening on the
2123 * other end of the link. Use this to decide whether we should
2124 * power on the phy as HPD may be toggled by the sink to merely
2125 * ask the source to re-read the EDID.
2126 */
2127 if (intr_stat &
Russell Kinge84b8d72017-07-31 15:29:41 +01002128 (HDMI_IH_PHY_STAT0_RX_SENSE | HDMI_IH_PHY_STAT0_HPD)) {
Neil Armstrong386d3292017-04-04 14:31:59 +02002129 __dw_hdmi_setup_rx_sense(hdmi,
2130 phy_stat & HDMI_PHY_HPD,
2131 phy_stat & HDMI_PHY_RX_SENSE);
Russell Kingaeac23b2015-06-05 13:46:22 +01002132
Russell Kinge84b8d72017-07-31 15:29:41 +01002133 if ((phy_stat & (HDMI_PHY_RX_SENSE | HDMI_PHY_HPD)) == 0)
2134 cec_notifier_set_phys_addr(hdmi->cec_notifier,
2135 CEC_PHYS_ADDR_INVALID);
2136 }
2137
Russell Kingaeac23b2015-06-05 13:46:22 +01002138 if (intr_stat & HDMI_IH_PHY_STAT0_HPD) {
2139 dev_dbg(hdmi->dev, "EVENT=%s\n",
2140 phy_int_pol & HDMI_PHY_HPD ? "plugin" : "plugout");
Laurent Pinchartba5d7e62017-01-17 10:28:56 +02002141 if (hdmi->bridge.dev)
2142 drm_helper_hpd_irq_event(hdmi->bridge.dev);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002143 }
2144
2145 hdmi_writeb(hdmi, intr_stat, HDMI_IH_PHY_STAT0);
Russell Kingaeac23b2015-06-05 13:46:22 +01002146 hdmi_writeb(hdmi, ~(HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE),
2147 HDMI_IH_MUTE_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002148
2149 return IRQ_HANDLED;
2150}
2151
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002152static const struct dw_hdmi_phy_data dw_hdmi_phys[] = {
2153 {
2154 .type = DW_HDMI_PHY_DWC_HDMI_TX_PHY,
2155 .name = "DWC HDMI TX PHY",
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02002156 .gen = 1,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002157 }, {
2158 .type = DW_HDMI_PHY_DWC_MHL_PHY_HEAC,
2159 .name = "DWC MHL PHY + HEAC PHY",
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02002160 .gen = 2,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002161 .has_svsret = true,
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02002162 .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002163 }, {
2164 .type = DW_HDMI_PHY_DWC_MHL_PHY,
2165 .name = "DWC MHL PHY",
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02002166 .gen = 2,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002167 .has_svsret = true,
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02002168 .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002169 }, {
2170 .type = DW_HDMI_PHY_DWC_HDMI_3D_TX_PHY_HEAC,
2171 .name = "DWC HDMI 3D TX PHY + HEAC PHY",
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02002172 .gen = 2,
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02002173 .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002174 }, {
2175 .type = DW_HDMI_PHY_DWC_HDMI_3D_TX_PHY,
2176 .name = "DWC HDMI 3D TX PHY",
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02002177 .gen = 2,
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02002178 .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002179 }, {
2180 .type = DW_HDMI_PHY_DWC_HDMI20_TX_PHY,
2181 .name = "DWC HDMI 2.0 TX PHY",
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02002182 .gen = 2,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002183 .has_svsret = true,
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02002184 }, {
2185 .type = DW_HDMI_PHY_VENDOR_PHY,
2186 .name = "Vendor PHY",
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002187 }
2188};
2189
2190static int dw_hdmi_detect_phy(struct dw_hdmi *hdmi)
2191{
2192 unsigned int i;
2193 u8 phy_type;
2194
2195 phy_type = hdmi_readb(hdmi, HDMI_CONFIG2_ID);
2196
Laurent Pinchartf1585f62017-03-06 01:36:15 +02002197 if (phy_type == DW_HDMI_PHY_VENDOR_PHY) {
2198 /* Vendor PHYs require support from the glue layer. */
2199 if (!hdmi->plat_data->phy_ops || !hdmi->plat_data->phy_name) {
2200 dev_err(hdmi->dev,
2201 "Vendor HDMI PHY not supported by glue layer\n");
2202 return -ENODEV;
2203 }
2204
2205 hdmi->phy.ops = hdmi->plat_data->phy_ops;
2206 hdmi->phy.data = hdmi->plat_data->phy_data;
2207 hdmi->phy.name = hdmi->plat_data->phy_name;
2208 return 0;
2209 }
2210
2211 /* Synopsys PHYs are handled internally. */
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002212 for (i = 0; i < ARRAY_SIZE(dw_hdmi_phys); ++i) {
2213 if (dw_hdmi_phys[i].type == phy_type) {
Laurent Pinchartf1585f62017-03-06 01:36:15 +02002214 hdmi->phy.ops = &dw_hdmi_synopsys_phy_ops;
2215 hdmi->phy.name = dw_hdmi_phys[i].name;
2216 hdmi->phy.data = (void *)&dw_hdmi_phys[i];
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02002217
2218 if (!dw_hdmi_phys[i].configure &&
2219 !hdmi->plat_data->configure_phy) {
2220 dev_err(hdmi->dev, "%s requires platform support\n",
2221 hdmi->phy.name);
2222 return -ENODEV;
2223 }
2224
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002225 return 0;
2226 }
2227 }
2228
Laurent Pinchartf1585f62017-03-06 01:36:15 +02002229 dev_err(hdmi->dev, "Unsupported HDMI PHY type (%02x)\n", phy_type);
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002230 return -ENODEV;
2231}
2232
Neil Armstrong80e2f972017-03-03 19:20:06 +02002233static const struct regmap_config hdmi_regmap_8bit_config = {
2234 .reg_bits = 32,
2235 .val_bits = 8,
2236 .reg_stride = 1,
2237 .max_register = HDMI_I2CM_FS_SCL_LCNT_0_ADDR,
2238};
2239
2240static const struct regmap_config hdmi_regmap_32bit_config = {
2241 .reg_bits = 32,
2242 .val_bits = 32,
2243 .reg_stride = 4,
2244 .max_register = HDMI_I2CM_FS_SCL_LCNT_0_ADDR << 2,
2245};
2246
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002247static struct dw_hdmi *
2248__dw_hdmi_probe(struct platform_device *pdev,
2249 const struct dw_hdmi_plat_data *plat_data)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002250{
Laurent Pinchartc6081192017-01-17 10:28:57 +02002251 struct device *dev = &pdev->dev;
Russell King17b50012013-11-03 11:23:34 +00002252 struct device_node *np = dev->of_node;
Russell King7ed6c662013-11-07 16:01:45 +00002253 struct platform_device_info pdevinfo;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002254 struct device_node *ddc_node;
Andy Yanb21f4b62014-12-05 14:26:31 +08002255 struct dw_hdmi *hdmi;
Neil Armstrong80e2f972017-03-03 19:20:06 +02002256 struct resource *iores = NULL;
Laurent Pinchartc6081192017-01-17 10:28:57 +02002257 int irq;
Andy Yan3d1b35a2014-12-05 14:25:05 +08002258 int ret;
Andy Yan0cd9d142014-12-05 14:28:24 +08002259 u32 val = 1;
Laurent Pinchart0527e122017-01-17 10:29:03 +02002260 u8 prod_id0;
2261 u8 prod_id1;
Kuninori Morimoto2761ba62016-11-08 01:00:57 +00002262 u8 config0;
Laurent Pinchart0c674942017-01-17 10:29:04 +02002263 u8 config3;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002264
Russell King17b50012013-11-03 11:23:34 +00002265 hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002266 if (!hdmi)
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002267 return ERR_PTR(-ENOMEM);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002268
Andy Yan3d1b35a2014-12-05 14:25:05 +08002269 hdmi->plat_data = plat_data;
Russell King17b50012013-11-03 11:23:34 +00002270 hdmi->dev = dev;
Russell King40678382013-11-07 15:35:06 +00002271 hdmi->sample_rate = 48000;
Russell Kingb872a8e2015-06-05 12:22:46 +01002272 hdmi->disabled = true;
Russell Kingaeac23b2015-06-05 13:46:22 +01002273 hdmi->rxsense = true;
2274 hdmi->phy_mask = (u8)~(HDMI_PHY_HPD | HDMI_PHY_RX_SENSE);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002275
Russell Kingb872a8e2015-06-05 12:22:46 +01002276 mutex_init(&hdmi->mutex);
Russell King6bcf4952015-02-02 11:01:08 +00002277 mutex_init(&hdmi->audio_mutex);
Russell Kingb90120a2015-03-27 12:59:58 +00002278 spin_lock_init(&hdmi->audio_lock);
Russell King6bcf4952015-02-02 11:01:08 +00002279
Philipp Zabelb5d45902014-03-05 10:20:56 +01002280 ddc_node = of_parse_phandle(np, "ddc-i2c-bus", 0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002281 if (ddc_node) {
Vladimir Zapolskiy9f04a1f2016-08-16 23:26:43 +03002282 hdmi->ddc = of_get_i2c_adapter_by_node(ddc_node);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002283 of_node_put(ddc_node);
Andy Yanc2c38482014-12-05 14:24:28 +08002284 if (!hdmi->ddc) {
2285 dev_dbg(hdmi->dev, "failed to read ddc node\n");
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002286 return ERR_PTR(-EPROBE_DEFER);
Andy Yanc2c38482014-12-05 14:24:28 +08002287 }
2288
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002289 } else {
2290 dev_dbg(hdmi->dev, "no ddc property found\n");
2291 }
2292
Neil Armstrong80e2f972017-03-03 19:20:06 +02002293 if (!plat_data->regm) {
2294 const struct regmap_config *reg_config;
2295
2296 of_property_read_u32(np, "reg-io-width", &val);
2297 switch (val) {
2298 case 4:
2299 reg_config = &hdmi_regmap_32bit_config;
2300 hdmi->reg_shift = 2;
2301 break;
2302 case 1:
2303 reg_config = &hdmi_regmap_8bit_config;
2304 break;
2305 default:
2306 dev_err(dev, "reg-io-width must be 1 or 4\n");
2307 return ERR_PTR(-EINVAL);
2308 }
2309
2310 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2311 hdmi->regs = devm_ioremap_resource(dev, iores);
2312 if (IS_ERR(hdmi->regs)) {
2313 ret = PTR_ERR(hdmi->regs);
2314 goto err_res;
2315 }
2316
2317 hdmi->regm = devm_regmap_init_mmio(dev, hdmi->regs, reg_config);
2318 if (IS_ERR(hdmi->regm)) {
2319 dev_err(dev, "Failed to configure regmap\n");
2320 ret = PTR_ERR(hdmi->regm);
2321 goto err_res;
2322 }
2323 } else {
2324 hdmi->regm = plat_data->regm;
Vladimir Zapolskiy9f04a1f2016-08-16 23:26:43 +03002325 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002326
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002327 hdmi->isfr_clk = devm_clk_get(hdmi->dev, "isfr");
2328 if (IS_ERR(hdmi->isfr_clk)) {
2329 ret = PTR_ERR(hdmi->isfr_clk);
Andy Yanb5878332014-12-05 14:23:52 +08002330 dev_err(hdmi->dev, "Unable to get HDMI isfr clk: %d\n", ret);
Vladimir Zapolskiy9f04a1f2016-08-16 23:26:43 +03002331 goto err_res;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002332 }
2333
2334 ret = clk_prepare_enable(hdmi->isfr_clk);
2335 if (ret) {
Andy Yanb5878332014-12-05 14:23:52 +08002336 dev_err(hdmi->dev, "Cannot enable HDMI isfr clock: %d\n", ret);
Vladimir Zapolskiy9f04a1f2016-08-16 23:26:43 +03002337 goto err_res;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002338 }
2339
2340 hdmi->iahb_clk = devm_clk_get(hdmi->dev, "iahb");
2341 if (IS_ERR(hdmi->iahb_clk)) {
2342 ret = PTR_ERR(hdmi->iahb_clk);
Andy Yanb5878332014-12-05 14:23:52 +08002343 dev_err(hdmi->dev, "Unable to get HDMI iahb clk: %d\n", ret);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002344 goto err_isfr;
2345 }
2346
2347 ret = clk_prepare_enable(hdmi->iahb_clk);
2348 if (ret) {
Andy Yanb5878332014-12-05 14:23:52 +08002349 dev_err(hdmi->dev, "Cannot enable HDMI iahb clock: %d\n", ret);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002350 goto err_isfr;
2351 }
2352
2353 /* Product and revision IDs */
Laurent Pinchartbe41fc52017-01-17 10:29:05 +02002354 hdmi->version = (hdmi_readb(hdmi, HDMI_DESIGN_ID) << 8)
2355 | (hdmi_readb(hdmi, HDMI_REVISION_ID) << 0);
Laurent Pinchart0527e122017-01-17 10:29:03 +02002356 prod_id0 = hdmi_readb(hdmi, HDMI_PRODUCT_ID0);
2357 prod_id1 = hdmi_readb(hdmi, HDMI_PRODUCT_ID1);
2358
2359 if (prod_id0 != HDMI_PRODUCT_ID0_HDMI_TX ||
2360 (prod_id1 & ~HDMI_PRODUCT_ID1_HDCP) != HDMI_PRODUCT_ID1_HDMI_TX) {
2361 dev_err(dev, "Unsupported HDMI controller (%04x:%02x:%02x)\n",
Laurent Pinchartbe41fc52017-01-17 10:29:05 +02002362 hdmi->version, prod_id0, prod_id1);
Laurent Pinchart0527e122017-01-17 10:29:03 +02002363 ret = -ENODEV;
2364 goto err_iahb;
2365 }
2366
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002367 ret = dw_hdmi_detect_phy(hdmi);
2368 if (ret < 0)
2369 goto err_iahb;
2370
2371 dev_info(dev, "Detected HDMI TX controller v%x.%03x %s HDCP (%s)\n",
Laurent Pinchartbe41fc52017-01-17 10:29:05 +02002372 hdmi->version >> 12, hdmi->version & 0xfff,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002373 prod_id1 & HDMI_PRODUCT_ID1_HDCP ? "with" : "without",
Laurent Pinchartf1585f62017-03-06 01:36:15 +02002374 hdmi->phy.name);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002375
2376 initialize_hdmi_ih_mutes(hdmi);
2377
Laurent Pinchartc6081192017-01-17 10:28:57 +02002378 irq = platform_get_irq(pdev, 0);
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002379 if (irq < 0) {
2380 ret = irq;
Laurent Pinchartc6081192017-01-17 10:28:57 +02002381 goto err_iahb;
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002382 }
Laurent Pinchartc6081192017-01-17 10:28:57 +02002383
Philipp Zabel639a2022015-01-07 13:43:50 +01002384 ret = devm_request_threaded_irq(dev, irq, dw_hdmi_hardirq,
2385 dw_hdmi_irq, IRQF_SHARED,
2386 dev_name(dev), hdmi);
2387 if (ret)
Fabio Estevamb33ef612015-01-27 10:54:12 -02002388 goto err_iahb;
Philipp Zabel639a2022015-01-07 13:43:50 +01002389
Russell Kinge84b8d72017-07-31 15:29:41 +01002390 hdmi->cec_notifier = cec_notifier_get(dev);
2391 if (!hdmi->cec_notifier) {
2392 ret = -ENOMEM;
2393 goto err_iahb;
2394 }
2395
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002396 /*
2397 * To prevent overflows in HDMI_IH_FC_STAT2, set the clk regenerator
2398 * N and cts values before enabling phy
2399 */
2400 hdmi_init_clk_regenerator(hdmi);
2401
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002402 /* If DDC bus is not specified, try to register HDMI I2C bus */
2403 if (!hdmi->ddc) {
2404 hdmi->ddc = dw_hdmi_i2c_adapter(hdmi);
2405 if (IS_ERR(hdmi->ddc))
2406 hdmi->ddc = NULL;
2407 }
2408
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002409 hdmi->bridge.driver_private = hdmi;
2410 hdmi->bridge.funcs = &dw_hdmi_bridge_funcs;
Arnd Bergmannd5ad7842017-01-23 13:20:38 +01002411#ifdef CONFIG_OF
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002412 hdmi->bridge.of_node = pdev->dev.of_node;
Arnd Bergmannd5ad7842017-01-23 13:20:38 +01002413#endif
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002414
Laurent Pincharta23d6262017-04-04 14:31:56 +02002415 dw_hdmi_setup_i2c(hdmi);
Neil Armstrong386d3292017-04-04 14:31:59 +02002416 if (hdmi->phy.ops->setup_hpd)
2417 hdmi->phy.ops->setup_hpd(hdmi, hdmi->phy.data);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002418
Russell King7ed6c662013-11-07 16:01:45 +00002419 memset(&pdevinfo, 0, sizeof(pdevinfo));
2420 pdevinfo.parent = dev;
2421 pdevinfo.id = PLATFORM_DEVID_AUTO;
2422
Kuninori Morimoto2761ba62016-11-08 01:00:57 +00002423 config0 = hdmi_readb(hdmi, HDMI_CONFIG0_ID);
Laurent Pinchart0c674942017-01-17 10:29:04 +02002424 config3 = hdmi_readb(hdmi, HDMI_CONFIG3_ID);
Kuninori Morimoto2761ba62016-11-08 01:00:57 +00002425
Neil Armstrong80e2f972017-03-03 19:20:06 +02002426 if (iores && config3 & HDMI_CONFIG3_AHBAUDDMA) {
Kuninori Morimoto2761ba62016-11-08 01:00:57 +00002427 struct dw_hdmi_audio_data audio;
2428
Russell King7ed6c662013-11-07 16:01:45 +00002429 audio.phys = iores->start;
2430 audio.base = hdmi->regs;
2431 audio.irq = irq;
2432 audio.hdmi = hdmi;
Russell Kingf5ce4052013-11-07 16:06:01 +00002433 audio.eld = hdmi->connector.eld;
Romain Periera7d555d2017-04-14 10:31:12 +02002434 hdmi->enable_audio = dw_hdmi_ahb_audio_enable;
2435 hdmi->disable_audio = dw_hdmi_ahb_audio_disable;
Russell King7ed6c662013-11-07 16:01:45 +00002436
2437 pdevinfo.name = "dw-hdmi-ahb-audio";
2438 pdevinfo.data = &audio;
2439 pdevinfo.size_data = sizeof(audio);
2440 pdevinfo.dma_mask = DMA_BIT_MASK(32);
2441 hdmi->audio = platform_device_register_full(&pdevinfo);
Kuninori Morimoto2761ba62016-11-08 01:00:57 +00002442 } else if (config0 & HDMI_CONFIG0_I2S) {
2443 struct dw_hdmi_i2s_audio_data audio;
2444
2445 audio.hdmi = hdmi;
2446 audio.write = hdmi_writeb;
2447 audio.read = hdmi_readb;
Romain Periera7d555d2017-04-14 10:31:12 +02002448 hdmi->enable_audio = dw_hdmi_i2s_audio_enable;
Romain Perier57fbc052017-04-20 14:34:34 +05302449 hdmi->disable_audio = dw_hdmi_i2s_audio_disable;
Kuninori Morimoto2761ba62016-11-08 01:00:57 +00002450
2451 pdevinfo.name = "dw-hdmi-i2s-audio";
2452 pdevinfo.data = &audio;
2453 pdevinfo.size_data = sizeof(audio);
2454 pdevinfo.dma_mask = DMA_BIT_MASK(32);
2455 hdmi->audio = platform_device_register_full(&pdevinfo);
Russell King7ed6c662013-11-07 16:01:45 +00002456 }
2457
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002458 /* Reset HDMI DDC I2C master controller and mute I2CM interrupts */
2459 if (hdmi->i2c)
2460 dw_hdmi_i2c_init(hdmi);
2461
Laurent Pinchartc6081192017-01-17 10:28:57 +02002462 platform_set_drvdata(pdev, hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002463
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002464 return hdmi;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002465
2466err_iahb:
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002467 if (hdmi->i2c) {
2468 i2c_del_adapter(&hdmi->i2c->adap);
2469 hdmi->ddc = NULL;
2470 }
2471
Russell Kinge84b8d72017-07-31 15:29:41 +01002472 if (hdmi->cec_notifier)
2473 cec_notifier_put(hdmi->cec_notifier);
2474
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002475 clk_disable_unprepare(hdmi->iahb_clk);
2476err_isfr:
2477 clk_disable_unprepare(hdmi->isfr_clk);
Vladimir Zapolskiy9f04a1f2016-08-16 23:26:43 +03002478err_res:
2479 i2c_put_adapter(hdmi->ddc);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002480
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002481 return ERR_PTR(ret);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002482}
2483
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002484static void __dw_hdmi_remove(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002485{
Russell King7ed6c662013-11-07 16:01:45 +00002486 if (hdmi->audio && !IS_ERR(hdmi->audio))
2487 platform_device_unregister(hdmi->audio);
2488
Russell Kingd94905e2013-11-03 22:23:24 +00002489 /* Disable all interrupts */
2490 hdmi_writeb(hdmi, ~0, HDMI_IH_MUTE_PHY_STAT0);
2491
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002492 clk_disable_unprepare(hdmi->iahb_clk);
2493 clk_disable_unprepare(hdmi->isfr_clk);
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002494
2495 if (hdmi->i2c)
2496 i2c_del_adapter(&hdmi->i2c->adap);
2497 else
2498 i2c_put_adapter(hdmi->ddc);
Russell King17b50012013-11-03 11:23:34 +00002499}
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002500
2501/* -----------------------------------------------------------------------------
2502 * Probe/remove API, used from platforms based on the DRM bridge API.
2503 */
2504int dw_hdmi_probe(struct platform_device *pdev,
2505 const struct dw_hdmi_plat_data *plat_data)
2506{
2507 struct dw_hdmi *hdmi;
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002508
2509 hdmi = __dw_hdmi_probe(pdev, plat_data);
2510 if (IS_ERR(hdmi))
2511 return PTR_ERR(hdmi);
2512
Inki Daeb6786822017-07-03 17:42:25 +09002513 drm_bridge_add(&hdmi->bridge);
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002514
2515 return 0;
2516}
2517EXPORT_SYMBOL_GPL(dw_hdmi_probe);
2518
2519void dw_hdmi_remove(struct platform_device *pdev)
2520{
2521 struct dw_hdmi *hdmi = platform_get_drvdata(pdev);
2522
2523 drm_bridge_remove(&hdmi->bridge);
2524
2525 __dw_hdmi_remove(hdmi);
2526}
2527EXPORT_SYMBOL_GPL(dw_hdmi_remove);
2528
2529/* -----------------------------------------------------------------------------
2530 * Bind/unbind API, used from platforms based on the component framework.
2531 */
2532int dw_hdmi_bind(struct platform_device *pdev, struct drm_encoder *encoder,
2533 const struct dw_hdmi_plat_data *plat_data)
2534{
2535 struct dw_hdmi *hdmi;
2536 int ret;
2537
2538 hdmi = __dw_hdmi_probe(pdev, plat_data);
2539 if (IS_ERR(hdmi))
2540 return PTR_ERR(hdmi);
2541
2542 ret = drm_bridge_attach(encoder, &hdmi->bridge, NULL);
2543 if (ret) {
2544 dw_hdmi_remove(pdev);
2545 DRM_ERROR("Failed to initialize bridge with drm\n");
2546 return ret;
2547 }
2548
2549 return 0;
2550}
2551EXPORT_SYMBOL_GPL(dw_hdmi_bind);
2552
2553void dw_hdmi_unbind(struct device *dev)
2554{
2555 struct dw_hdmi *hdmi = dev_get_drvdata(dev);
2556
2557 __dw_hdmi_remove(hdmi);
2558}
Andy Yanb21f4b62014-12-05 14:26:31 +08002559EXPORT_SYMBOL_GPL(dw_hdmi_unbind);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002560
2561MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
Andy Yan3d1b35a2014-12-05 14:25:05 +08002562MODULE_AUTHOR("Andy Yan <andy.yan@rock-chips.com>");
2563MODULE_AUTHOR("Yakir Yang <ykk@rock-chips.com>");
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002564MODULE_AUTHOR("Vladimir Zapolskiy <vladimir_zapolskiy@mentor.com>");
Andy Yanb21f4b62014-12-05 14:26:31 +08002565MODULE_DESCRIPTION("DW HDMI transmitter driver");
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002566MODULE_LICENSE("GPL");
Andy Yanb21f4b62014-12-05 14:26:31 +08002567MODULE_ALIAS("platform:dw-hdmi");