blob: 94efe0e9e37b2de45c13937133faae44eb64e975 [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
Nickey Yang94bb4dc2017-03-20 10:57:31 +080039#define DDC_SEGMENT_ADDR 0x30
Fabio Estevam9aaf8802013-11-29 08:46:32 -020040#define HDMI_EDID_LEN 512
41
Fabio Estevam9aaf8802013-11-29 08:46:32 -020042enum hdmi_datamap {
43 RGB444_8B = 0x01,
44 RGB444_10B = 0x03,
45 RGB444_12B = 0x05,
46 RGB444_16B = 0x07,
47 YCbCr444_8B = 0x09,
48 YCbCr444_10B = 0x0B,
49 YCbCr444_12B = 0x0D,
50 YCbCr444_16B = 0x0F,
51 YCbCr422_8B = 0x16,
52 YCbCr422_10B = 0x14,
53 YCbCr422_12B = 0x12,
54};
55
Fabio Estevam9aaf8802013-11-29 08:46:32 -020056static const u16 csc_coeff_default[3][4] = {
57 { 0x2000, 0x0000, 0x0000, 0x0000 },
58 { 0x0000, 0x2000, 0x0000, 0x0000 },
59 { 0x0000, 0x0000, 0x2000, 0x0000 }
60};
61
62static const u16 csc_coeff_rgb_out_eitu601[3][4] = {
63 { 0x2000, 0x6926, 0x74fd, 0x010e },
64 { 0x2000, 0x2cdd, 0x0000, 0x7e9a },
65 { 0x2000, 0x0000, 0x38b4, 0x7e3b }
66};
67
68static const u16 csc_coeff_rgb_out_eitu709[3][4] = {
69 { 0x2000, 0x7106, 0x7a02, 0x00a7 },
70 { 0x2000, 0x3264, 0x0000, 0x7e6d },
71 { 0x2000, 0x0000, 0x3b61, 0x7e25 }
72};
73
74static const u16 csc_coeff_rgb_in_eitu601[3][4] = {
75 { 0x2591, 0x1322, 0x074b, 0x0000 },
76 { 0x6535, 0x2000, 0x7acc, 0x0200 },
77 { 0x6acd, 0x7534, 0x2000, 0x0200 }
78};
79
80static const u16 csc_coeff_rgb_in_eitu709[3][4] = {
81 { 0x2dc5, 0x0d9b, 0x049e, 0x0000 },
82 { 0x62f0, 0x2000, 0x7d11, 0x0200 },
83 { 0x6756, 0x78ab, 0x2000, 0x0200 }
84};
85
86struct hdmi_vmode {
Fabio Estevam9aaf8802013-11-29 08:46:32 -020087 bool mdataenablepolarity;
88
89 unsigned int mpixelclock;
90 unsigned int mpixelrepetitioninput;
91 unsigned int mpixelrepetitionoutput;
92};
93
94struct hdmi_data_info {
Neil Armstrongdef23aa2017-04-04 14:31:57 +020095 unsigned int enc_in_bus_format;
96 unsigned int enc_out_bus_format;
97 unsigned int enc_in_encoding;
98 unsigned int enc_out_encoding;
Fabio Estevam9aaf8802013-11-29 08:46:32 -020099 unsigned int pix_repet_factor;
100 unsigned int hdcp_enable;
101 struct hdmi_vmode video_mode;
102};
103
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +0300104struct dw_hdmi_i2c {
105 struct i2c_adapter adap;
106
107 struct mutex lock; /* used to serialize data transfers */
108 struct completion cmp;
109 u8 stat;
110
111 u8 slave_reg;
112 bool is_regaddr;
Nickey Yang94bb4dc2017-03-20 10:57:31 +0800113 bool is_segment;
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +0300114};
115
Laurent Pinchartfaba6c32017-01-17 10:29:06 +0200116struct dw_hdmi_phy_data {
117 enum dw_hdmi_phy_type type;
118 const char *name;
Laurent Pinchartb0e583e2017-03-06 01:35:39 +0200119 unsigned int gen;
Laurent Pinchartfaba6c32017-01-17 10:29:06 +0200120 bool has_svsret;
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +0200121 int (*configure)(struct dw_hdmi *hdmi,
122 const struct dw_hdmi_plat_data *pdata,
123 unsigned long mpixelclock);
Laurent Pinchartfaba6c32017-01-17 10:29:06 +0200124};
125
Andy Yanb21f4b62014-12-05 14:26:31 +0800126struct dw_hdmi {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200127 struct drm_connector connector;
Laurent Pinchart70c963e2017-01-17 10:28:54 +0200128 struct drm_bridge bridge;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200129
Laurent Pinchartbe41fc52017-01-17 10:29:05 +0200130 unsigned int version;
131
132 struct platform_device *audio;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200133 struct device *dev;
134 struct clk *isfr_clk;
135 struct clk *iahb_clk;
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +0300136 struct dw_hdmi_i2c *i2c;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200137
138 struct hdmi_data_info hdmi_data;
Andy Yanb21f4b62014-12-05 14:26:31 +0800139 const struct dw_hdmi_plat_data *plat_data;
140
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200141 int vic;
142
143 u8 edid[HDMI_EDID_LEN];
144 bool cable_plugin;
145
Laurent Pinchartf1585f62017-03-06 01:36:15 +0200146 struct {
147 const struct dw_hdmi_phy_ops *ops;
148 const char *name;
149 void *data;
150 bool enabled;
151 } phy;
Laurent Pinchartfaba6c32017-01-17 10:29:06 +0200152
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200153 struct drm_display_mode previous_mode;
154
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200155 struct i2c_adapter *ddc;
156 void __iomem *regs;
Russell King05b13422015-07-21 15:35:52 +0100157 bool sink_is_hdmi;
Russell Kingf709ec02015-07-21 16:09:39 +0100158 bool sink_has_audio;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200159
Russell Kingb872a8e2015-06-05 12:22:46 +0100160 struct mutex mutex; /* for state below and previous_mode */
Russell King381f05a2015-06-05 15:25:08 +0100161 enum drm_connector_force force; /* mutex-protected force state */
Russell Kingb872a8e2015-06-05 12:22:46 +0100162 bool disabled; /* DRM has disabled our bridge */
Russell King381f05a2015-06-05 15:25:08 +0100163 bool bridge_is_on; /* indicates the bridge is on */
Russell Kingaeac23b2015-06-05 13:46:22 +0100164 bool rxsense; /* rxsense state */
165 u8 phy_mask; /* desired phy int mask settings */
Russell Kingb872a8e2015-06-05 12:22:46 +0100166
Russell Kingb90120a2015-03-27 12:59:58 +0000167 spinlock_t audio_lock;
Russell King6bcf4952015-02-02 11:01:08 +0000168 struct mutex audio_mutex;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200169 unsigned int sample_rate;
Russell Kingb90120a2015-03-27 12:59:58 +0000170 unsigned int audio_cts;
171 unsigned int audio_n;
172 bool audio_enable;
Andy Yan0cd9d142014-12-05 14:28:24 +0800173
Neil Armstrong80e2f972017-03-03 19:20:06 +0200174 unsigned int reg_shift;
175 struct regmap *regm;
Romain Periera7d555d2017-04-14 10:31:12 +0200176 void (*enable_audio)(struct dw_hdmi *hdmi);
177 void (*disable_audio)(struct dw_hdmi *hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200178};
179
Russell Kingaeac23b2015-06-05 13:46:22 +0100180#define HDMI_IH_PHY_STAT0_RX_SENSE \
181 (HDMI_IH_PHY_STAT0_RX_SENSE0 | HDMI_IH_PHY_STAT0_RX_SENSE1 | \
182 HDMI_IH_PHY_STAT0_RX_SENSE2 | HDMI_IH_PHY_STAT0_RX_SENSE3)
183
184#define HDMI_PHY_RX_SENSE \
185 (HDMI_PHY_RX_SENSE0 | HDMI_PHY_RX_SENSE1 | \
186 HDMI_PHY_RX_SENSE2 | HDMI_PHY_RX_SENSE3)
187
Andy Yan0cd9d142014-12-05 14:28:24 +0800188static inline void hdmi_writeb(struct dw_hdmi *hdmi, u8 val, int offset)
189{
Neil Armstrong80e2f972017-03-03 19:20:06 +0200190 regmap_write(hdmi->regm, offset << hdmi->reg_shift, val);
Andy Yan0cd9d142014-12-05 14:28:24 +0800191}
192
193static inline u8 hdmi_readb(struct dw_hdmi *hdmi, int offset)
194{
Neil Armstrong80e2f972017-03-03 19:20:06 +0200195 unsigned int val = 0;
196
197 regmap_read(hdmi->regm, offset << hdmi->reg_shift, &val);
198
199 return val;
Andy Yan0cd9d142014-12-05 14:28:24 +0800200}
201
Andy Yanb21f4b62014-12-05 14:26:31 +0800202static void hdmi_modb(struct dw_hdmi *hdmi, u8 data, u8 mask, unsigned reg)
Russell King812bc612013-11-04 12:42:02 +0000203{
Neil Armstrong80e2f972017-03-03 19:20:06 +0200204 regmap_update_bits(hdmi->regm, reg << hdmi->reg_shift, mask, data);
Russell King812bc612013-11-04 12:42:02 +0000205}
206
Andy Yanb21f4b62014-12-05 14:26:31 +0800207static void hdmi_mask_writeb(struct dw_hdmi *hdmi, u8 data, unsigned int reg,
Andy Yanb5878332014-12-05 14:23:52 +0800208 u8 shift, u8 mask)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200209{
Russell King812bc612013-11-04 12:42:02 +0000210 hdmi_modb(hdmi, data << shift, mask, reg);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200211}
212
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +0300213static void dw_hdmi_i2c_init(struct dw_hdmi *hdmi)
214{
215 /* Software reset */
216 hdmi_writeb(hdmi, 0x00, HDMI_I2CM_SOFTRSTZ);
217
218 /* Set Standard Mode speed (determined to be 100KHz on iMX6) */
219 hdmi_writeb(hdmi, 0x00, HDMI_I2CM_DIV);
220
221 /* Set done, not acknowledged and arbitration interrupt polarities */
222 hdmi_writeb(hdmi, HDMI_I2CM_INT_DONE_POL, HDMI_I2CM_INT);
223 hdmi_writeb(hdmi, HDMI_I2CM_CTLINT_NAC_POL | HDMI_I2CM_CTLINT_ARB_POL,
224 HDMI_I2CM_CTLINT);
225
226 /* Clear DONE and ERROR interrupts */
227 hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,
228 HDMI_IH_I2CM_STAT0);
229
230 /* Mute DONE and ERROR interrupts */
231 hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,
232 HDMI_IH_MUTE_I2CM_STAT0);
233}
234
235static int dw_hdmi_i2c_read(struct dw_hdmi *hdmi,
236 unsigned char *buf, unsigned int length)
237{
238 struct dw_hdmi_i2c *i2c = hdmi->i2c;
239 int stat;
240
241 if (!i2c->is_regaddr) {
242 dev_dbg(hdmi->dev, "set read register address to 0\n");
243 i2c->slave_reg = 0x00;
244 i2c->is_regaddr = true;
245 }
246
247 while (length--) {
248 reinit_completion(&i2c->cmp);
249
250 hdmi_writeb(hdmi, i2c->slave_reg++, HDMI_I2CM_ADDRESS);
Nickey Yang94bb4dc2017-03-20 10:57:31 +0800251 if (i2c->is_segment)
252 hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_READ_EXT,
253 HDMI_I2CM_OPERATION);
254 else
255 hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_READ,
256 HDMI_I2CM_OPERATION);
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +0300257
258 stat = wait_for_completion_timeout(&i2c->cmp, HZ / 10);
259 if (!stat)
260 return -EAGAIN;
261
262 /* Check for error condition on the bus */
263 if (i2c->stat & HDMI_IH_I2CM_STAT0_ERROR)
264 return -EIO;
265
266 *buf++ = hdmi_readb(hdmi, HDMI_I2CM_DATAI);
267 }
Nickey Yang94bb4dc2017-03-20 10:57:31 +0800268 i2c->is_segment = false;
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +0300269
270 return 0;
271}
272
273static int dw_hdmi_i2c_write(struct dw_hdmi *hdmi,
274 unsigned char *buf, unsigned int length)
275{
276 struct dw_hdmi_i2c *i2c = hdmi->i2c;
277 int stat;
278
279 if (!i2c->is_regaddr) {
280 /* Use the first write byte as register address */
281 i2c->slave_reg = buf[0];
282 length--;
283 buf++;
284 i2c->is_regaddr = true;
285 }
286
287 while (length--) {
288 reinit_completion(&i2c->cmp);
289
290 hdmi_writeb(hdmi, *buf++, HDMI_I2CM_DATAO);
291 hdmi_writeb(hdmi, i2c->slave_reg++, HDMI_I2CM_ADDRESS);
292 hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_WRITE,
293 HDMI_I2CM_OPERATION);
294
295 stat = wait_for_completion_timeout(&i2c->cmp, HZ / 10);
296 if (!stat)
297 return -EAGAIN;
298
299 /* Check for error condition on the bus */
300 if (i2c->stat & HDMI_IH_I2CM_STAT0_ERROR)
301 return -EIO;
302 }
303
304 return 0;
305}
306
307static int dw_hdmi_i2c_xfer(struct i2c_adapter *adap,
308 struct i2c_msg *msgs, int num)
309{
310 struct dw_hdmi *hdmi = i2c_get_adapdata(adap);
311 struct dw_hdmi_i2c *i2c = hdmi->i2c;
312 u8 addr = msgs[0].addr;
313 int i, ret = 0;
314
315 dev_dbg(hdmi->dev, "xfer: num: %d, addr: %#x\n", num, addr);
316
317 for (i = 0; i < num; i++) {
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +0300318 if (msgs[i].len == 0) {
319 dev_dbg(hdmi->dev,
320 "unsupported transfer %d/%d, no data\n",
321 i + 1, num);
322 return -EOPNOTSUPP;
323 }
324 }
325
326 mutex_lock(&i2c->lock);
327
328 /* Unmute DONE and ERROR interrupts */
329 hdmi_writeb(hdmi, 0x00, HDMI_IH_MUTE_I2CM_STAT0);
330
331 /* Set slave device address taken from the first I2C message */
332 hdmi_writeb(hdmi, addr, HDMI_I2CM_SLAVE);
333
334 /* Set slave device register address on transfer */
335 i2c->is_regaddr = false;
336
Nickey Yang94bb4dc2017-03-20 10:57:31 +0800337 /* Set segment pointer for I2C extended read mode operation */
338 i2c->is_segment = false;
339
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +0300340 for (i = 0; i < num; i++) {
341 dev_dbg(hdmi->dev, "xfer: num: %d/%d, len: %d, flags: %#x\n",
342 i + 1, num, msgs[i].len, msgs[i].flags);
Nickey Yang94bb4dc2017-03-20 10:57:31 +0800343 if (msgs[i].addr == DDC_SEGMENT_ADDR && msgs[i].len == 1) {
344 i2c->is_segment = true;
345 hdmi_writeb(hdmi, DDC_SEGMENT_ADDR, HDMI_I2CM_SEGADDR);
346 hdmi_writeb(hdmi, *msgs[i].buf, HDMI_I2CM_SEGPTR);
347 } else {
348 if (msgs[i].flags & I2C_M_RD)
349 ret = dw_hdmi_i2c_read(hdmi, msgs[i].buf,
350 msgs[i].len);
351 else
352 ret = dw_hdmi_i2c_write(hdmi, msgs[i].buf,
353 msgs[i].len);
354 }
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +0300355 if (ret < 0)
356 break;
357 }
358
359 if (!ret)
360 ret = num;
361
362 /* Mute DONE and ERROR interrupts */
363 hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,
364 HDMI_IH_MUTE_I2CM_STAT0);
365
366 mutex_unlock(&i2c->lock);
367
368 return ret;
369}
370
371static u32 dw_hdmi_i2c_func(struct i2c_adapter *adapter)
372{
373 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
374}
375
376static const struct i2c_algorithm dw_hdmi_algorithm = {
377 .master_xfer = dw_hdmi_i2c_xfer,
378 .functionality = dw_hdmi_i2c_func,
379};
380
381static struct i2c_adapter *dw_hdmi_i2c_adapter(struct dw_hdmi *hdmi)
382{
383 struct i2c_adapter *adap;
384 struct dw_hdmi_i2c *i2c;
385 int ret;
386
387 i2c = devm_kzalloc(hdmi->dev, sizeof(*i2c), GFP_KERNEL);
388 if (!i2c)
389 return ERR_PTR(-ENOMEM);
390
391 mutex_init(&i2c->lock);
392 init_completion(&i2c->cmp);
393
394 adap = &i2c->adap;
395 adap->class = I2C_CLASS_DDC;
396 adap->owner = THIS_MODULE;
397 adap->dev.parent = hdmi->dev;
398 adap->algo = &dw_hdmi_algorithm;
399 strlcpy(adap->name, "DesignWare HDMI", sizeof(adap->name));
400 i2c_set_adapdata(adap, hdmi);
401
402 ret = i2c_add_adapter(adap);
403 if (ret) {
404 dev_warn(hdmi->dev, "cannot add %s I2C adapter\n", adap->name);
405 devm_kfree(hdmi->dev, i2c);
406 return ERR_PTR(ret);
407 }
408
409 hdmi->i2c = i2c;
410
411 dev_info(hdmi->dev, "registered %s I2C bus driver\n", adap->name);
412
413 return adap;
414}
415
Russell King351e1352015-01-31 14:50:23 +0000416static void hdmi_set_cts_n(struct dw_hdmi *hdmi, unsigned int cts,
417 unsigned int n)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200418{
Russell King622494a2015-02-02 10:55:38 +0000419 /* Must be set/cleared first */
420 hdmi_modb(hdmi, 0, HDMI_AUD_CTS3_CTS_MANUAL, HDMI_AUD_CTS3);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200421
422 /* nshift factor = 0 */
Russell King812bc612013-11-04 12:42:02 +0000423 hdmi_modb(hdmi, 0, HDMI_AUD_CTS3_N_SHIFT_MASK, HDMI_AUD_CTS3);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200424
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200425 hdmi_writeb(hdmi, ((cts >> 16) & HDMI_AUD_CTS3_AUDCTS19_16_MASK) |
426 HDMI_AUD_CTS3_CTS_MANUAL, HDMI_AUD_CTS3);
Russell King622494a2015-02-02 10:55:38 +0000427 hdmi_writeb(hdmi, (cts >> 8) & 0xff, HDMI_AUD_CTS2);
428 hdmi_writeb(hdmi, cts & 0xff, HDMI_AUD_CTS1);
429
430 hdmi_writeb(hdmi, (n >> 16) & 0x0f, HDMI_AUD_N3);
431 hdmi_writeb(hdmi, (n >> 8) & 0xff, HDMI_AUD_N2);
432 hdmi_writeb(hdmi, n & 0xff, HDMI_AUD_N1);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200433}
434
Russell Kingb195fbd2015-07-22 11:28:16 +0100435static unsigned int hdmi_compute_n(unsigned int freq, unsigned long pixel_clk)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200436{
437 unsigned int n = (128 * freq) / 1000;
Russell Kingd0c96d12015-07-22 10:35:41 +0100438 unsigned int mult = 1;
439
440 while (freq > 48000) {
441 mult *= 2;
442 freq /= 2;
443 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200444
445 switch (freq) {
446 case 32000:
Russell King426701d2015-07-22 10:39:27 +0100447 if (pixel_clk == 25175000)
Russell Kingb195fbd2015-07-22 11:28:16 +0100448 n = 4576;
Russell King426701d2015-07-22 10:39:27 +0100449 else if (pixel_clk == 27027000)
Russell Kingb195fbd2015-07-22 11:28:16 +0100450 n = 4096;
Russell King426701d2015-07-22 10:39:27 +0100451 else if (pixel_clk == 74176000 || pixel_clk == 148352000)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200452 n = 11648;
453 else
454 n = 4096;
Russell Kingd0c96d12015-07-22 10:35:41 +0100455 n *= mult;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200456 break;
457
458 case 44100:
Russell King426701d2015-07-22 10:39:27 +0100459 if (pixel_clk == 25175000)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200460 n = 7007;
Russell King426701d2015-07-22 10:39:27 +0100461 else if (pixel_clk == 74176000)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200462 n = 17836;
Russell King426701d2015-07-22 10:39:27 +0100463 else if (pixel_clk == 148352000)
Russell Kingb195fbd2015-07-22 11:28:16 +0100464 n = 8918;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200465 else
466 n = 6272;
Russell Kingd0c96d12015-07-22 10:35:41 +0100467 n *= mult;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200468 break;
469
470 case 48000:
Russell King426701d2015-07-22 10:39:27 +0100471 if (pixel_clk == 25175000)
Russell Kingb195fbd2015-07-22 11:28:16 +0100472 n = 6864;
Russell King426701d2015-07-22 10:39:27 +0100473 else if (pixel_clk == 27027000)
Russell Kingb195fbd2015-07-22 11:28:16 +0100474 n = 6144;
Russell King426701d2015-07-22 10:39:27 +0100475 else if (pixel_clk == 74176000)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200476 n = 11648;
Russell King426701d2015-07-22 10:39:27 +0100477 else if (pixel_clk == 148352000)
Russell Kingb195fbd2015-07-22 11:28:16 +0100478 n = 5824;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200479 else
480 n = 6144;
Russell Kingd0c96d12015-07-22 10:35:41 +0100481 n *= mult;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200482 break;
483
484 default:
485 break;
486 }
487
488 return n;
489}
490
Andy Yanb21f4b62014-12-05 14:26:31 +0800491static void hdmi_set_clk_regenerator(struct dw_hdmi *hdmi,
Russell Kingb195fbd2015-07-22 11:28:16 +0100492 unsigned long pixel_clk, unsigned int sample_rate)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200493{
Russell Kingdfbdaf52015-07-22 16:54:37 +0100494 unsigned long ftdms = pixel_clk;
Russell Kingf879b382015-03-27 12:53:29 +0000495 unsigned int n, cts;
Russell Kingdfbdaf52015-07-22 16:54:37 +0100496 u64 tmp;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200497
Russell Kingb195fbd2015-07-22 11:28:16 +0100498 n = hdmi_compute_n(sample_rate, pixel_clk);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200499
Russell Kingdfbdaf52015-07-22 16:54:37 +0100500 /*
501 * Compute the CTS value from the N value. Note that CTS and N
502 * can be up to 20 bits in total, so we need 64-bit math. Also
503 * note that our TDMS clock is not fully accurate; it is accurate
504 * to kHz. This can introduce an unnecessary remainder in the
505 * calculation below, so we don't try to warn about that.
506 */
507 tmp = (u64)ftdms * n;
508 do_div(tmp, 128 * sample_rate);
509 cts = tmp;
510
511 dev_dbg(hdmi->dev, "%s: fs=%uHz ftdms=%lu.%03luMHz N=%d cts=%d\n",
512 __func__, sample_rate, ftdms / 1000000, (ftdms / 1000) % 1000,
513 n, cts);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200514
Russell Kingb90120a2015-03-27 12:59:58 +0000515 spin_lock_irq(&hdmi->audio_lock);
516 hdmi->audio_n = n;
517 hdmi->audio_cts = cts;
518 hdmi_set_cts_n(hdmi, cts, hdmi->audio_enable ? n : 0);
519 spin_unlock_irq(&hdmi->audio_lock);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200520}
521
Andy Yanb21f4b62014-12-05 14:26:31 +0800522static void hdmi_init_clk_regenerator(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200523{
Russell King6bcf4952015-02-02 11:01:08 +0000524 mutex_lock(&hdmi->audio_mutex);
Russell Kingb195fbd2015-07-22 11:28:16 +0100525 hdmi_set_clk_regenerator(hdmi, 74250000, hdmi->sample_rate);
Russell King6bcf4952015-02-02 11:01:08 +0000526 mutex_unlock(&hdmi->audio_mutex);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200527}
528
Andy Yanb21f4b62014-12-05 14:26:31 +0800529static void hdmi_clk_regenerator_update_pixel_clock(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200530{
Russell King6bcf4952015-02-02 11:01:08 +0000531 mutex_lock(&hdmi->audio_mutex);
Russell Kingf879b382015-03-27 12:53:29 +0000532 hdmi_set_clk_regenerator(hdmi, hdmi->hdmi_data.video_mode.mpixelclock,
Russell Kingb195fbd2015-07-22 11:28:16 +0100533 hdmi->sample_rate);
Russell King6bcf4952015-02-02 11:01:08 +0000534 mutex_unlock(&hdmi->audio_mutex);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200535}
536
Russell Kingb5814ff2015-03-27 12:50:58 +0000537void dw_hdmi_set_sample_rate(struct dw_hdmi *hdmi, unsigned int rate)
538{
539 mutex_lock(&hdmi->audio_mutex);
540 hdmi->sample_rate = rate;
541 hdmi_set_clk_regenerator(hdmi, hdmi->hdmi_data.video_mode.mpixelclock,
Russell Kingb195fbd2015-07-22 11:28:16 +0100542 hdmi->sample_rate);
Russell Kingb5814ff2015-03-27 12:50:58 +0000543 mutex_unlock(&hdmi->audio_mutex);
544}
545EXPORT_SYMBOL_GPL(dw_hdmi_set_sample_rate);
546
Romain Periera7d555d2017-04-14 10:31:12 +0200547static void dw_hdmi_ahb_audio_enable(struct dw_hdmi *hdmi)
548{
549 hdmi_set_cts_n(hdmi, hdmi->audio_cts, hdmi->audio_n);
550}
551
552static void dw_hdmi_ahb_audio_disable(struct dw_hdmi *hdmi)
553{
554 hdmi_set_cts_n(hdmi, hdmi->audio_cts, 0);
555}
556
557static void dw_hdmi_i2s_audio_enable(struct dw_hdmi *hdmi)
558{
559 hdmi_set_cts_n(hdmi, hdmi->audio_cts, hdmi->audio_n);
560}
561
Russell Kingb90120a2015-03-27 12:59:58 +0000562void dw_hdmi_audio_enable(struct dw_hdmi *hdmi)
563{
564 unsigned long flags;
565
566 spin_lock_irqsave(&hdmi->audio_lock, flags);
567 hdmi->audio_enable = true;
Romain Periera7d555d2017-04-14 10:31:12 +0200568 if (hdmi->enable_audio)
569 hdmi->enable_audio(hdmi);
Russell Kingb90120a2015-03-27 12:59:58 +0000570 spin_unlock_irqrestore(&hdmi->audio_lock, flags);
571}
572EXPORT_SYMBOL_GPL(dw_hdmi_audio_enable);
573
574void dw_hdmi_audio_disable(struct dw_hdmi *hdmi)
575{
576 unsigned long flags;
577
578 spin_lock_irqsave(&hdmi->audio_lock, flags);
579 hdmi->audio_enable = false;
Romain Periera7d555d2017-04-14 10:31:12 +0200580 if (hdmi->disable_audio)
581 hdmi->disable_audio(hdmi);
Russell Kingb90120a2015-03-27 12:59:58 +0000582 spin_unlock_irqrestore(&hdmi->audio_lock, flags);
583}
584EXPORT_SYMBOL_GPL(dw_hdmi_audio_disable);
585
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200586static bool hdmi_bus_fmt_is_rgb(unsigned int bus_format)
587{
588 switch (bus_format) {
589 case MEDIA_BUS_FMT_RGB888_1X24:
590 case MEDIA_BUS_FMT_RGB101010_1X30:
591 case MEDIA_BUS_FMT_RGB121212_1X36:
592 case MEDIA_BUS_FMT_RGB161616_1X48:
593 return true;
594
595 default:
596 return false;
597 }
598}
599
600static bool hdmi_bus_fmt_is_yuv444(unsigned int bus_format)
601{
602 switch (bus_format) {
603 case MEDIA_BUS_FMT_YUV8_1X24:
604 case MEDIA_BUS_FMT_YUV10_1X30:
605 case MEDIA_BUS_FMT_YUV12_1X36:
606 case MEDIA_BUS_FMT_YUV16_1X48:
607 return true;
608
609 default:
610 return false;
611 }
612}
613
614static bool hdmi_bus_fmt_is_yuv422(unsigned int bus_format)
615{
616 switch (bus_format) {
617 case MEDIA_BUS_FMT_UYVY8_1X16:
618 case MEDIA_BUS_FMT_UYVY10_1X20:
619 case MEDIA_BUS_FMT_UYVY12_1X24:
620 return true;
621
622 default:
623 return false;
624 }
625}
626
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200627static int hdmi_bus_fmt_color_depth(unsigned int bus_format)
628{
629 switch (bus_format) {
630 case MEDIA_BUS_FMT_RGB888_1X24:
631 case MEDIA_BUS_FMT_YUV8_1X24:
632 case MEDIA_BUS_FMT_UYVY8_1X16:
633 case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
634 return 8;
635
636 case MEDIA_BUS_FMT_RGB101010_1X30:
637 case MEDIA_BUS_FMT_YUV10_1X30:
638 case MEDIA_BUS_FMT_UYVY10_1X20:
639 case MEDIA_BUS_FMT_UYYVYY10_0_5X30:
640 return 10;
641
642 case MEDIA_BUS_FMT_RGB121212_1X36:
643 case MEDIA_BUS_FMT_YUV12_1X36:
644 case MEDIA_BUS_FMT_UYVY12_1X24:
645 case MEDIA_BUS_FMT_UYYVYY12_0_5X36:
646 return 12;
647
648 case MEDIA_BUS_FMT_RGB161616_1X48:
649 case MEDIA_BUS_FMT_YUV16_1X48:
650 case MEDIA_BUS_FMT_UYYVYY16_0_5X48:
651 return 16;
652
653 default:
654 return 0;
655 }
656}
657
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200658/*
659 * this submodule is responsible for the video data synchronization.
660 * for example, for RGB 4:4:4 input, the data map is defined as
661 * pin{47~40} <==> R[7:0]
662 * pin{31~24} <==> G[7:0]
663 * pin{15~8} <==> B[7:0]
664 */
Andy Yanb21f4b62014-12-05 14:26:31 +0800665static void hdmi_video_sample(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200666{
667 int color_format = 0;
668 u8 val;
669
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200670 switch (hdmi->hdmi_data.enc_in_bus_format) {
671 case MEDIA_BUS_FMT_RGB888_1X24:
672 color_format = 0x01;
673 break;
674 case MEDIA_BUS_FMT_RGB101010_1X30:
675 color_format = 0x03;
676 break;
677 case MEDIA_BUS_FMT_RGB121212_1X36:
678 color_format = 0x05;
679 break;
680 case MEDIA_BUS_FMT_RGB161616_1X48:
681 color_format = 0x07;
682 break;
683
684 case MEDIA_BUS_FMT_YUV8_1X24:
685 case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
686 color_format = 0x09;
687 break;
688 case MEDIA_BUS_FMT_YUV10_1X30:
689 case MEDIA_BUS_FMT_UYYVYY10_0_5X30:
690 color_format = 0x0B;
691 break;
692 case MEDIA_BUS_FMT_YUV12_1X36:
693 case MEDIA_BUS_FMT_UYYVYY12_0_5X36:
694 color_format = 0x0D;
695 break;
696 case MEDIA_BUS_FMT_YUV16_1X48:
697 case MEDIA_BUS_FMT_UYYVYY16_0_5X48:
698 color_format = 0x0F;
699 break;
700
701 case MEDIA_BUS_FMT_UYVY8_1X16:
702 color_format = 0x16;
703 break;
704 case MEDIA_BUS_FMT_UYVY10_1X20:
705 color_format = 0x14;
706 break;
707 case MEDIA_BUS_FMT_UYVY12_1X24:
708 color_format = 0x12;
709 break;
710
711 default:
712 return;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200713 }
714
715 val = HDMI_TX_INVID0_INTERNAL_DE_GENERATOR_DISABLE |
716 ((color_format << HDMI_TX_INVID0_VIDEO_MAPPING_OFFSET) &
717 HDMI_TX_INVID0_VIDEO_MAPPING_MASK);
718 hdmi_writeb(hdmi, val, HDMI_TX_INVID0);
719
720 /* Enable TX stuffing: When DE is inactive, fix the output data to 0 */
721 val = HDMI_TX_INSTUFFING_BDBDATA_STUFFING_ENABLE |
722 HDMI_TX_INSTUFFING_RCRDATA_STUFFING_ENABLE |
723 HDMI_TX_INSTUFFING_GYDATA_STUFFING_ENABLE;
724 hdmi_writeb(hdmi, val, HDMI_TX_INSTUFFING);
725 hdmi_writeb(hdmi, 0x0, HDMI_TX_GYDATA0);
726 hdmi_writeb(hdmi, 0x0, HDMI_TX_GYDATA1);
727 hdmi_writeb(hdmi, 0x0, HDMI_TX_RCRDATA0);
728 hdmi_writeb(hdmi, 0x0, HDMI_TX_RCRDATA1);
729 hdmi_writeb(hdmi, 0x0, HDMI_TX_BCBDATA0);
730 hdmi_writeb(hdmi, 0x0, HDMI_TX_BCBDATA1);
731}
732
Andy Yanb21f4b62014-12-05 14:26:31 +0800733static int is_color_space_conversion(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200734{
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200735 return hdmi->hdmi_data.enc_in_bus_format != hdmi->hdmi_data.enc_out_bus_format;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200736}
737
Andy Yanb21f4b62014-12-05 14:26:31 +0800738static int is_color_space_decimation(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200739{
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200740 if (!hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format))
Fabio Estevamba92b222014-02-06 10:12:03 -0200741 return 0;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200742
743 if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_in_bus_format) ||
744 hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_in_bus_format))
Fabio Estevamba92b222014-02-06 10:12:03 -0200745 return 1;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200746
Fabio Estevamba92b222014-02-06 10:12:03 -0200747 return 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200748}
749
Andy Yanb21f4b62014-12-05 14:26:31 +0800750static int is_color_space_interpolation(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200751{
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200752 if (!hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_in_bus_format))
Fabio Estevamba92b222014-02-06 10:12:03 -0200753 return 0;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200754
755 if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format) ||
756 hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format))
Fabio Estevamba92b222014-02-06 10:12:03 -0200757 return 1;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200758
Fabio Estevamba92b222014-02-06 10:12:03 -0200759 return 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200760}
761
Andy Yanb21f4b62014-12-05 14:26:31 +0800762static void dw_hdmi_update_csc_coeffs(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200763{
764 const u16 (*csc_coeff)[3][4] = &csc_coeff_default;
Russell Kingc082f9d2013-11-04 12:10:40 +0000765 unsigned i;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200766 u32 csc_scale = 1;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200767
768 if (is_color_space_conversion(hdmi)) {
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200769 if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format)) {
770 if (hdmi->hdmi_data.enc_out_encoding ==
771 V4L2_YCBCR_ENC_601)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200772 csc_coeff = &csc_coeff_rgb_out_eitu601;
773 else
774 csc_coeff = &csc_coeff_rgb_out_eitu709;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200775 } else if (hdmi_bus_fmt_is_rgb(
776 hdmi->hdmi_data.enc_in_bus_format)) {
777 if (hdmi->hdmi_data.enc_out_encoding ==
778 V4L2_YCBCR_ENC_601)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200779 csc_coeff = &csc_coeff_rgb_in_eitu601;
780 else
781 csc_coeff = &csc_coeff_rgb_in_eitu709;
782 csc_scale = 0;
783 }
784 }
785
Russell Kingc082f9d2013-11-04 12:10:40 +0000786 /* The CSC registers are sequential, alternating MSB then LSB */
787 for (i = 0; i < ARRAY_SIZE(csc_coeff_default[0]); i++) {
788 u16 coeff_a = (*csc_coeff)[0][i];
789 u16 coeff_b = (*csc_coeff)[1][i];
790 u16 coeff_c = (*csc_coeff)[2][i];
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200791
Andy Yanb5878332014-12-05 14:23:52 +0800792 hdmi_writeb(hdmi, coeff_a & 0xff, HDMI_CSC_COEF_A1_LSB + i * 2);
Russell Kingc082f9d2013-11-04 12:10:40 +0000793 hdmi_writeb(hdmi, coeff_a >> 8, HDMI_CSC_COEF_A1_MSB + i * 2);
794 hdmi_writeb(hdmi, coeff_b & 0xff, HDMI_CSC_COEF_B1_LSB + i * 2);
795 hdmi_writeb(hdmi, coeff_b >> 8, HDMI_CSC_COEF_B1_MSB + i * 2);
Andy Yanb5878332014-12-05 14:23:52 +0800796 hdmi_writeb(hdmi, coeff_c & 0xff, HDMI_CSC_COEF_C1_LSB + i * 2);
Russell Kingc082f9d2013-11-04 12:10:40 +0000797 hdmi_writeb(hdmi, coeff_c >> 8, HDMI_CSC_COEF_C1_MSB + i * 2);
798 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200799
Russell King812bc612013-11-04 12:42:02 +0000800 hdmi_modb(hdmi, csc_scale, HDMI_CSC_SCALE_CSCSCALE_MASK,
801 HDMI_CSC_SCALE);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200802}
803
Andy Yanb21f4b62014-12-05 14:26:31 +0800804static void hdmi_video_csc(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200805{
806 int color_depth = 0;
807 int interpolation = HDMI_CSC_CFG_INTMODE_DISABLE;
808 int decimation = 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200809
810 /* YCC422 interpolation to 444 mode */
811 if (is_color_space_interpolation(hdmi))
812 interpolation = HDMI_CSC_CFG_INTMODE_CHROMA_INT_FORMULA1;
813 else if (is_color_space_decimation(hdmi))
814 decimation = HDMI_CSC_CFG_DECMODE_CHROMA_INT_FORMULA3;
815
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200816 switch (hdmi_bus_fmt_color_depth(hdmi->hdmi_data.enc_out_bus_format)) {
817 case 8:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200818 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_24BPP;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200819 break;
820 case 10:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200821 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_30BPP;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200822 break;
823 case 12:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200824 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_36BPP;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200825 break;
826 case 16:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200827 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_48BPP;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200828 break;
829
830 default:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200831 return;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200832 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200833
834 /* Configure the CSC registers */
835 hdmi_writeb(hdmi, interpolation | decimation, HDMI_CSC_CFG);
Russell King812bc612013-11-04 12:42:02 +0000836 hdmi_modb(hdmi, color_depth, HDMI_CSC_SCALE_CSC_COLORDE_PTH_MASK,
837 HDMI_CSC_SCALE);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200838
Andy Yanb21f4b62014-12-05 14:26:31 +0800839 dw_hdmi_update_csc_coeffs(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200840}
841
842/*
843 * HDMI video packetizer is used to packetize the data.
844 * for example, if input is YCC422 mode or repeater is used,
845 * data should be repacked this module can be bypassed.
846 */
Andy Yanb21f4b62014-12-05 14:26:31 +0800847static void hdmi_video_packetize(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200848{
849 unsigned int color_depth = 0;
850 unsigned int remap_size = HDMI_VP_REMAP_YCC422_16bit;
851 unsigned int output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_PP;
852 struct hdmi_data_info *hdmi_data = &hdmi->hdmi_data;
Russell Kingbebdf662013-11-04 12:55:30 +0000853 u8 val, vp_conf;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200854
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200855 if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format) ||
856 hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format)) {
857 switch (hdmi_bus_fmt_color_depth(
858 hdmi->hdmi_data.enc_out_bus_format)) {
859 case 8:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200860 color_depth = 4;
861 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200862 break;
863 case 10:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200864 color_depth = 5;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200865 break;
866 case 12:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200867 color_depth = 6;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200868 break;
869 case 16:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200870 color_depth = 7;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200871 break;
872 default:
873 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;
874 }
875 } else if (hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format)) {
876 switch (hdmi_bus_fmt_color_depth(
877 hdmi->hdmi_data.enc_out_bus_format)) {
878 case 0:
879 case 8:
880 remap_size = HDMI_VP_REMAP_YCC422_16bit;
881 break;
882 case 10:
883 remap_size = HDMI_VP_REMAP_YCC422_20bit;
884 break;
885 case 12:
886 remap_size = HDMI_VP_REMAP_YCC422_24bit;
887 break;
888
889 default:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200890 return;
Andy Yanb5878332014-12-05 14:23:52 +0800891 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200892 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_YCC422;
Andy Yanb5878332014-12-05 14:23:52 +0800893 } else {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200894 return;
Andy Yanb5878332014-12-05 14:23:52 +0800895 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200896
897 /* set the packetizer registers */
898 val = ((color_depth << HDMI_VP_PR_CD_COLOR_DEPTH_OFFSET) &
899 HDMI_VP_PR_CD_COLOR_DEPTH_MASK) |
900 ((hdmi_data->pix_repet_factor <<
901 HDMI_VP_PR_CD_DESIRED_PR_FACTOR_OFFSET) &
902 HDMI_VP_PR_CD_DESIRED_PR_FACTOR_MASK);
903 hdmi_writeb(hdmi, val, HDMI_VP_PR_CD);
904
Russell King812bc612013-11-04 12:42:02 +0000905 hdmi_modb(hdmi, HDMI_VP_STUFF_PR_STUFFING_STUFFING_MODE,
906 HDMI_VP_STUFF_PR_STUFFING_MASK, HDMI_VP_STUFF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200907
908 /* Data from pixel repeater block */
909 if (hdmi_data->pix_repet_factor > 1) {
Russell Kingbebdf662013-11-04 12:55:30 +0000910 vp_conf = HDMI_VP_CONF_PR_EN_ENABLE |
911 HDMI_VP_CONF_BYPASS_SELECT_PIX_REPEATER;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200912 } else { /* data from packetizer block */
Russell Kingbebdf662013-11-04 12:55:30 +0000913 vp_conf = HDMI_VP_CONF_PR_EN_DISABLE |
914 HDMI_VP_CONF_BYPASS_SELECT_VID_PACKETIZER;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200915 }
916
Russell Kingbebdf662013-11-04 12:55:30 +0000917 hdmi_modb(hdmi, vp_conf,
918 HDMI_VP_CONF_PR_EN_MASK |
919 HDMI_VP_CONF_BYPASS_SELECT_MASK, HDMI_VP_CONF);
920
Russell King812bc612013-11-04 12:42:02 +0000921 hdmi_modb(hdmi, 1 << HDMI_VP_STUFF_IDEFAULT_PHASE_OFFSET,
922 HDMI_VP_STUFF_IDEFAULT_PHASE_MASK, HDMI_VP_STUFF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200923
924 hdmi_writeb(hdmi, remap_size, HDMI_VP_REMAP);
925
926 if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_PP) {
Russell Kingbebdf662013-11-04 12:55:30 +0000927 vp_conf = HDMI_VP_CONF_BYPASS_EN_DISABLE |
928 HDMI_VP_CONF_PP_EN_ENABLE |
929 HDMI_VP_CONF_YCC422_EN_DISABLE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200930 } else if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_YCC422) {
Russell Kingbebdf662013-11-04 12:55:30 +0000931 vp_conf = HDMI_VP_CONF_BYPASS_EN_DISABLE |
932 HDMI_VP_CONF_PP_EN_DISABLE |
933 HDMI_VP_CONF_YCC422_EN_ENABLE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200934 } else if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS) {
Russell Kingbebdf662013-11-04 12:55:30 +0000935 vp_conf = HDMI_VP_CONF_BYPASS_EN_ENABLE |
936 HDMI_VP_CONF_PP_EN_DISABLE |
937 HDMI_VP_CONF_YCC422_EN_DISABLE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200938 } else {
939 return;
940 }
941
Russell Kingbebdf662013-11-04 12:55:30 +0000942 hdmi_modb(hdmi, vp_conf,
943 HDMI_VP_CONF_BYPASS_EN_MASK | HDMI_VP_CONF_PP_EN_ENMASK |
944 HDMI_VP_CONF_YCC422_EN_MASK, HDMI_VP_CONF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200945
Russell King812bc612013-11-04 12:42:02 +0000946 hdmi_modb(hdmi, HDMI_VP_STUFF_PP_STUFFING_STUFFING_MODE |
947 HDMI_VP_STUFF_YCC422_STUFFING_STUFFING_MODE,
948 HDMI_VP_STUFF_PP_STUFFING_MASK |
949 HDMI_VP_STUFF_YCC422_STUFFING_MASK, HDMI_VP_STUFF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200950
Russell King812bc612013-11-04 12:42:02 +0000951 hdmi_modb(hdmi, output_select, HDMI_VP_CONF_OUTPUT_SELECTOR_MASK,
952 HDMI_VP_CONF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200953}
954
Laurent Pinchartf1585f62017-03-06 01:36:15 +0200955/* -----------------------------------------------------------------------------
956 * Synopsys PHY Handling
957 */
958
Andy Yanb21f4b62014-12-05 14:26:31 +0800959static inline void hdmi_phy_test_clear(struct dw_hdmi *hdmi,
Andy Yanb5878332014-12-05 14:23:52 +0800960 unsigned char bit)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200961{
Russell King812bc612013-11-04 12:42:02 +0000962 hdmi_modb(hdmi, bit << HDMI_PHY_TST0_TSTCLR_OFFSET,
963 HDMI_PHY_TST0_TSTCLR_MASK, HDMI_PHY_TST0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200964}
965
Andy Yanb21f4b62014-12-05 14:26:31 +0800966static bool hdmi_phy_wait_i2c_done(struct dw_hdmi *hdmi, int msec)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200967{
Andy Yana4d3b8b2014-12-05 14:31:09 +0800968 u32 val;
969
970 while ((val = hdmi_readb(hdmi, HDMI_IH_I2CMPHY_STAT0) & 0x3) == 0) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200971 if (msec-- == 0)
972 return false;
Emil Renner Berthing0e6bcf32014-03-30 00:21:21 +0100973 udelay(1000);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200974 }
Andy Yana4d3b8b2014-12-05 14:31:09 +0800975 hdmi_writeb(hdmi, val, HDMI_IH_I2CMPHY_STAT0);
976
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200977 return true;
978}
979
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +0200980void dw_hdmi_phy_i2c_write(struct dw_hdmi *hdmi, unsigned short data,
981 unsigned char addr)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200982{
983 hdmi_writeb(hdmi, 0xFF, HDMI_IH_I2CMPHY_STAT0);
984 hdmi_writeb(hdmi, addr, HDMI_PHY_I2CM_ADDRESS_ADDR);
985 hdmi_writeb(hdmi, (unsigned char)(data >> 8),
Andy Yanb5878332014-12-05 14:23:52 +0800986 HDMI_PHY_I2CM_DATAO_1_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200987 hdmi_writeb(hdmi, (unsigned char)(data >> 0),
Andy Yanb5878332014-12-05 14:23:52 +0800988 HDMI_PHY_I2CM_DATAO_0_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200989 hdmi_writeb(hdmi, HDMI_PHY_I2CM_OPERATION_ADDR_WRITE,
Andy Yanb5878332014-12-05 14:23:52 +0800990 HDMI_PHY_I2CM_OPERATION_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200991 hdmi_phy_wait_i2c_done(hdmi, 1000);
992}
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +0200993EXPORT_SYMBOL_GPL(dw_hdmi_phy_i2c_write);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200994
Russell King2fada102015-07-28 12:21:34 +0100995static void dw_hdmi_phy_enable_powerdown(struct dw_hdmi *hdmi, bool enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200996{
Russell King2fada102015-07-28 12:21:34 +0100997 hdmi_mask_writeb(hdmi, !enable, HDMI_PHY_CONF0,
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200998 HDMI_PHY_CONF0_PDZ_OFFSET,
999 HDMI_PHY_CONF0_PDZ_MASK);
1000}
1001
Andy Yanb21f4b62014-12-05 14:26:31 +08001002static void dw_hdmi_phy_enable_tmds(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001003{
1004 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
1005 HDMI_PHY_CONF0_ENTMDS_OFFSET,
1006 HDMI_PHY_CONF0_ENTMDS_MASK);
1007}
1008
Laurent Pinchartf4104e82017-01-17 10:29:02 +02001009static void dw_hdmi_phy_enable_svsret(struct dw_hdmi *hdmi, u8 enable)
Andy Yand346c142014-12-05 14:31:53 +08001010{
1011 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
Laurent Pinchartf4104e82017-01-17 10:29:02 +02001012 HDMI_PHY_CONF0_SVSRET_OFFSET,
1013 HDMI_PHY_CONF0_SVSRET_MASK);
Andy Yand346c142014-12-05 14:31:53 +08001014}
1015
Andy Yanb21f4b62014-12-05 14:26:31 +08001016static void dw_hdmi_phy_gen2_pddq(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001017{
1018 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
1019 HDMI_PHY_CONF0_GEN2_PDDQ_OFFSET,
1020 HDMI_PHY_CONF0_GEN2_PDDQ_MASK);
1021}
1022
Andy Yanb21f4b62014-12-05 14:26:31 +08001023static void dw_hdmi_phy_gen2_txpwron(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001024{
1025 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
1026 HDMI_PHY_CONF0_GEN2_TXPWRON_OFFSET,
1027 HDMI_PHY_CONF0_GEN2_TXPWRON_MASK);
1028}
1029
Andy Yanb21f4b62014-12-05 14:26:31 +08001030static void dw_hdmi_phy_sel_data_en_pol(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001031{
1032 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
1033 HDMI_PHY_CONF0_SELDATAENPOL_OFFSET,
1034 HDMI_PHY_CONF0_SELDATAENPOL_MASK);
1035}
1036
Andy Yanb21f4b62014-12-05 14:26:31 +08001037static void dw_hdmi_phy_sel_interface_control(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001038{
1039 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
1040 HDMI_PHY_CONF0_SELDIPIF_OFFSET,
1041 HDMI_PHY_CONF0_SELDIPIF_MASK);
1042}
1043
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02001044static void dw_hdmi_phy_power_off(struct dw_hdmi *hdmi)
1045{
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001046 const struct dw_hdmi_phy_data *phy = hdmi->phy.data;
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02001047 unsigned int i;
1048 u16 val;
1049
1050 if (phy->gen == 1) {
1051 dw_hdmi_phy_enable_tmds(hdmi, 0);
1052 dw_hdmi_phy_enable_powerdown(hdmi, true);
1053 return;
1054 }
1055
1056 dw_hdmi_phy_gen2_txpwron(hdmi, 0);
1057
1058 /*
1059 * Wait for TX_PHY_LOCK to be deasserted to indicate that the PHY went
1060 * to low power mode.
1061 */
1062 for (i = 0; i < 5; ++i) {
1063 val = hdmi_readb(hdmi, HDMI_PHY_STAT0);
1064 if (!(val & HDMI_PHY_TX_PHY_LOCK))
1065 break;
1066
1067 usleep_range(1000, 2000);
1068 }
1069
1070 if (val & HDMI_PHY_TX_PHY_LOCK)
1071 dev_warn(hdmi->dev, "PHY failed to power down\n");
1072 else
1073 dev_dbg(hdmi->dev, "PHY powered down in %u iterations\n", i);
1074
1075 dw_hdmi_phy_gen2_pddq(hdmi, 1);
1076}
1077
Laurent Pinchart181e0ef2017-03-06 01:35:57 +02001078static int dw_hdmi_phy_power_on(struct dw_hdmi *hdmi)
1079{
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001080 const struct dw_hdmi_phy_data *phy = hdmi->phy.data;
Laurent Pinchart181e0ef2017-03-06 01:35:57 +02001081 unsigned int i;
1082 u8 val;
1083
1084 if (phy->gen == 1) {
1085 dw_hdmi_phy_enable_powerdown(hdmi, false);
1086
1087 /* Toggle TMDS enable. */
1088 dw_hdmi_phy_enable_tmds(hdmi, 0);
1089 dw_hdmi_phy_enable_tmds(hdmi, 1);
1090 return 0;
1091 }
1092
1093 dw_hdmi_phy_gen2_txpwron(hdmi, 1);
1094 dw_hdmi_phy_gen2_pddq(hdmi, 0);
1095
1096 /* Wait for PHY PLL lock */
1097 for (i = 0; i < 5; ++i) {
1098 val = hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_TX_PHY_LOCK;
1099 if (val)
1100 break;
1101
1102 usleep_range(1000, 2000);
1103 }
1104
1105 if (!val) {
1106 dev_err(hdmi->dev, "PHY PLL failed to lock\n");
1107 return -ETIMEDOUT;
1108 }
1109
1110 dev_dbg(hdmi->dev, "PHY PLL locked %u iterations\n", i);
1111 return 0;
1112}
1113
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001114/*
1115 * PHY configuration function for the DWC HDMI 3D TX PHY. Based on the available
1116 * information the DWC MHL PHY has the same register layout and is thus also
1117 * supported by this function.
1118 */
1119static int hdmi_phy_configure_dwc_hdmi_3d_tx(struct dw_hdmi *hdmi,
1120 const struct dw_hdmi_plat_data *pdata,
1121 unsigned long mpixelclock)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001122{
Russell King39cc1532015-03-31 18:34:11 +01001123 const struct dw_hdmi_mpll_config *mpll_config = pdata->mpll_cfg;
1124 const struct dw_hdmi_curr_ctrl *curr_ctrl = pdata->cur_ctr;
1125 const struct dw_hdmi_phy_config *phy_config = pdata->phy_config;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001126
Russell King39cc1532015-03-31 18:34:11 +01001127 /* PLL/MPLL Cfg - always match on final entry */
1128 for (; mpll_config->mpixelclock != ~0UL; mpll_config++)
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001129 if (mpixelclock <= mpll_config->mpixelclock)
Russell King39cc1532015-03-31 18:34:11 +01001130 break;
1131
1132 for (; curr_ctrl->mpixelclock != ~0UL; curr_ctrl++)
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001133 if (mpixelclock <= curr_ctrl->mpixelclock)
Russell King39cc1532015-03-31 18:34:11 +01001134 break;
1135
1136 for (; phy_config->mpixelclock != ~0UL; phy_config++)
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001137 if (mpixelclock <= phy_config->mpixelclock)
Russell King39cc1532015-03-31 18:34:11 +01001138 break;
1139
1140 if (mpll_config->mpixelclock == ~0UL ||
1141 curr_ctrl->mpixelclock == ~0UL ||
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001142 phy_config->mpixelclock == ~0UL)
Russell King39cc1532015-03-31 18:34:11 +01001143 return -EINVAL;
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001144
1145 dw_hdmi_phy_i2c_write(hdmi, mpll_config->res[0].cpce,
1146 HDMI_3D_TX_PHY_CPCE_CTRL);
1147 dw_hdmi_phy_i2c_write(hdmi, mpll_config->res[0].gmp,
1148 HDMI_3D_TX_PHY_GMPCTRL);
1149 dw_hdmi_phy_i2c_write(hdmi, curr_ctrl->curr[0],
1150 HDMI_3D_TX_PHY_CURRCTRL);
1151
1152 dw_hdmi_phy_i2c_write(hdmi, 0, HDMI_3D_TX_PHY_PLLPHBYCTRL);
1153 dw_hdmi_phy_i2c_write(hdmi, HDMI_3D_TX_PHY_MSM_CTRL_CKO_SEL_FB_CLK,
1154 HDMI_3D_TX_PHY_MSM_CTRL);
1155
1156 dw_hdmi_phy_i2c_write(hdmi, phy_config->term, HDMI_3D_TX_PHY_TXTERM);
1157 dw_hdmi_phy_i2c_write(hdmi, phy_config->sym_ctr,
1158 HDMI_3D_TX_PHY_CKSYMTXCTRL);
1159 dw_hdmi_phy_i2c_write(hdmi, phy_config->vlev_ctr,
1160 HDMI_3D_TX_PHY_VLEVCTRL);
1161
1162 /* Override and disable clock termination. */
1163 dw_hdmi_phy_i2c_write(hdmi, HDMI_3D_TX_PHY_CKCALCTRL_OVERRIDE,
1164 HDMI_3D_TX_PHY_CKCALCTRL);
1165
1166 return 0;
1167}
1168
1169static int hdmi_phy_configure(struct dw_hdmi *hdmi)
1170{
1171 const struct dw_hdmi_phy_data *phy = hdmi->phy.data;
1172 const struct dw_hdmi_plat_data *pdata = hdmi->plat_data;
1173 unsigned long mpixelclock = hdmi->hdmi_data.video_mode.mpixelclock;
1174 int ret;
Russell King39cc1532015-03-31 18:34:11 +01001175
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02001176 dw_hdmi_phy_power_off(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001177
Laurent Pinchart2668db32017-01-17 10:29:09 +02001178 /* Leave low power consumption mode by asserting SVSRET. */
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001179 if (phy->has_svsret)
Laurent Pinchart2668db32017-01-17 10:29:09 +02001180 dw_hdmi_phy_enable_svsret(hdmi, 1);
1181
Laurent Pinchart54d72732017-01-17 10:29:08 +02001182 /* PHY reset. The reset signal is active high on Gen2 PHYs. */
1183 hdmi_writeb(hdmi, HDMI_MC_PHYRSTZ_PHYRSTZ, HDMI_MC_PHYRSTZ);
1184 hdmi_writeb(hdmi, 0, HDMI_MC_PHYRSTZ);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001185
1186 hdmi_writeb(hdmi, HDMI_MC_HEACPHY_RST_ASSERT, HDMI_MC_HEACPHY_RST);
1187
1188 hdmi_phy_test_clear(hdmi, 1);
1189 hdmi_writeb(hdmi, HDMI_PHY_I2CM_SLAVE_ADDR_PHY_GEN2,
Andy Yanb5878332014-12-05 14:23:52 +08001190 HDMI_PHY_I2CM_SLAVE_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001191 hdmi_phy_test_clear(hdmi, 0);
1192
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001193 /* Write to the PHY as configured by the platform */
1194 if (pdata->configure_phy)
1195 ret = pdata->configure_phy(hdmi, pdata, mpixelclock);
1196 else
1197 ret = phy->configure(hdmi, pdata, mpixelclock);
1198 if (ret) {
1199 dev_err(hdmi->dev, "PHY configuration failed (clock %lu)\n",
1200 mpixelclock);
1201 return ret;
1202 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001203
Laurent Pinchart181e0ef2017-03-06 01:35:57 +02001204 return dw_hdmi_phy_power_on(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001205}
1206
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001207static int dw_hdmi_phy_init(struct dw_hdmi *hdmi, void *data,
1208 struct drm_display_mode *mode)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001209{
1210 int i, ret;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001211
1212 /* HDMI Phy spec says to do the phy initialization sequence twice */
1213 for (i = 0; i < 2; i++) {
Andy Yanb21f4b62014-12-05 14:26:31 +08001214 dw_hdmi_phy_sel_data_en_pol(hdmi, 1);
1215 dw_hdmi_phy_sel_interface_control(hdmi, 0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001216
Laurent Pinchart8b9e1c02017-03-03 19:19:59 +02001217 ret = hdmi_phy_configure(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001218 if (ret)
1219 return ret;
1220 }
1221
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001222 return 0;
1223}
1224
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001225static void dw_hdmi_phy_disable(struct dw_hdmi *hdmi, void *data)
1226{
1227 dw_hdmi_phy_power_off(hdmi);
1228}
1229
1230static enum drm_connector_status dw_hdmi_phy_read_hpd(struct dw_hdmi *hdmi,
1231 void *data)
1232{
1233 return hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_HPD ?
1234 connector_status_connected : connector_status_disconnected;
1235}
1236
Neil Armstrong386d3292017-04-04 14:31:59 +02001237static void dw_hdmi_phy_update_hpd(struct dw_hdmi *hdmi, void *data,
1238 bool force, bool disabled, bool rxsense)
1239{
1240 u8 old_mask = hdmi->phy_mask;
1241
1242 if (force || disabled || !rxsense)
1243 hdmi->phy_mask |= HDMI_PHY_RX_SENSE;
1244 else
1245 hdmi->phy_mask &= ~HDMI_PHY_RX_SENSE;
1246
1247 if (old_mask != hdmi->phy_mask)
1248 hdmi_writeb(hdmi, hdmi->phy_mask, HDMI_PHY_MASK0);
1249}
1250
1251static void dw_hdmi_phy_setup_hpd(struct dw_hdmi *hdmi, void *data)
1252{
1253 /*
1254 * Configure the PHY RX SENSE and HPD interrupts polarities and clear
1255 * any pending interrupt.
1256 */
1257 hdmi_writeb(hdmi, HDMI_PHY_HPD | HDMI_PHY_RX_SENSE, HDMI_PHY_POL0);
1258 hdmi_writeb(hdmi, HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE,
1259 HDMI_IH_PHY_STAT0);
1260
1261 /* Enable cable hot plug irq. */
1262 hdmi_writeb(hdmi, hdmi->phy_mask, HDMI_PHY_MASK0);
1263
1264 /* Clear and unmute interrupts. */
1265 hdmi_writeb(hdmi, HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE,
1266 HDMI_IH_PHY_STAT0);
1267 hdmi_writeb(hdmi, ~(HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE),
1268 HDMI_IH_MUTE_PHY_STAT0);
1269}
1270
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001271static const struct dw_hdmi_phy_ops dw_hdmi_synopsys_phy_ops = {
1272 .init = dw_hdmi_phy_init,
1273 .disable = dw_hdmi_phy_disable,
1274 .read_hpd = dw_hdmi_phy_read_hpd,
Neil Armstrong386d3292017-04-04 14:31:59 +02001275 .update_hpd = dw_hdmi_phy_update_hpd,
1276 .setup_hpd = dw_hdmi_phy_setup_hpd,
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001277};
1278
1279/* -----------------------------------------------------------------------------
1280 * HDMI TX Setup
1281 */
1282
Andy Yanb21f4b62014-12-05 14:26:31 +08001283static void hdmi_tx_hdcp_config(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001284{
Russell King812bc612013-11-04 12:42:02 +00001285 u8 de;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001286
1287 if (hdmi->hdmi_data.video_mode.mdataenablepolarity)
1288 de = HDMI_A_VIDPOLCFG_DATAENPOL_ACTIVE_HIGH;
1289 else
1290 de = HDMI_A_VIDPOLCFG_DATAENPOL_ACTIVE_LOW;
1291
1292 /* disable rx detect */
Russell King812bc612013-11-04 12:42:02 +00001293 hdmi_modb(hdmi, HDMI_A_HDCPCFG0_RXDETECT_DISABLE,
1294 HDMI_A_HDCPCFG0_RXDETECT_MASK, HDMI_A_HDCPCFG0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001295
Russell King812bc612013-11-04 12:42:02 +00001296 hdmi_modb(hdmi, de, HDMI_A_VIDPOLCFG_DATAENPOL_MASK, HDMI_A_VIDPOLCFG);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001297
Russell King812bc612013-11-04 12:42:02 +00001298 hdmi_modb(hdmi, HDMI_A_HDCPCFG1_ENCRYPTIONDISABLE_DISABLE,
1299 HDMI_A_HDCPCFG1_ENCRYPTIONDISABLE_MASK, HDMI_A_HDCPCFG1);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001300}
1301
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001302static void hdmi_config_AVI(struct dw_hdmi *hdmi, struct drm_display_mode *mode)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001303{
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001304 struct hdmi_avi_infoframe frame;
1305 u8 val;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001306
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001307 /* Initialise info frame from DRM mode */
1308 drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001309
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001310 if (hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format))
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001311 frame.colorspace = HDMI_COLORSPACE_YUV444;
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001312 else if (hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format))
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001313 frame.colorspace = HDMI_COLORSPACE_YUV422;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001314 else
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001315 frame.colorspace = HDMI_COLORSPACE_RGB;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001316
1317 /* Set up colorimetry */
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001318 switch (hdmi->hdmi_data.enc_out_encoding) {
1319 case V4L2_YCBCR_ENC_601:
1320 if (hdmi->hdmi_data.enc_in_encoding == V4L2_YCBCR_ENC_XV601)
1321 frame.colorimetry = HDMI_COLORIMETRY_EXTENDED;
1322 else
1323 frame.colorimetry = HDMI_COLORIMETRY_ITU_601;
1324 frame.extended_colorimetry =
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001325 HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
Dan Carpenterf40d6562017-04-06 08:21:32 +03001326 break;
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001327 case V4L2_YCBCR_ENC_709:
1328 if (hdmi->hdmi_data.enc_in_encoding == V4L2_YCBCR_ENC_XV709)
1329 frame.colorimetry = HDMI_COLORIMETRY_EXTENDED;
1330 else
1331 frame.colorimetry = HDMI_COLORIMETRY_ITU_709;
1332 frame.extended_colorimetry =
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001333 HDMI_EXTENDED_COLORIMETRY_XV_YCC_709;
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001334 break;
1335 default: /* Carries no data */
1336 frame.colorimetry = HDMI_COLORIMETRY_ITU_601;
1337 frame.extended_colorimetry =
1338 HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
1339 break;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001340 }
1341
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001342 frame.scan_mode = HDMI_SCAN_MODE_NONE;
1343
1344 /*
1345 * The Designware IP uses a different byte format from standard
1346 * AVI info frames, though generally the bits are in the correct
1347 * bytes.
1348 */
1349
1350 /*
Jose Abreub0118e72016-08-29 10:30:51 +01001351 * AVI data byte 1 differences: Colorspace in bits 0,1 rather than 5,6,
1352 * scan info in bits 4,5 rather than 0,1 and active aspect present in
1353 * bit 6 rather than 4.
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001354 */
Jose Abreub0118e72016-08-29 10:30:51 +01001355 val = (frame.scan_mode & 3) << 4 | (frame.colorspace & 3);
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001356 if (frame.active_aspect & 15)
1357 val |= HDMI_FC_AVICONF0_ACTIVE_FMT_INFO_PRESENT;
1358 if (frame.top_bar || frame.bottom_bar)
1359 val |= HDMI_FC_AVICONF0_BAR_DATA_HORIZ_BAR;
1360 if (frame.left_bar || frame.right_bar)
1361 val |= HDMI_FC_AVICONF0_BAR_DATA_VERT_BAR;
1362 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF0);
1363
1364 /* AVI data byte 2 differences: none */
1365 val = ((frame.colorimetry & 0x3) << 6) |
1366 ((frame.picture_aspect & 0x3) << 4) |
1367 (frame.active_aspect & 0xf);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001368 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF1);
1369
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001370 /* AVI data byte 3 differences: none */
1371 val = ((frame.extended_colorimetry & 0x7) << 4) |
1372 ((frame.quantization_range & 0x3) << 2) |
1373 (frame.nups & 0x3);
1374 if (frame.itc)
1375 val |= HDMI_FC_AVICONF2_IT_CONTENT_VALID;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001376 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF2);
1377
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001378 /* AVI data byte 4 differences: none */
1379 val = frame.video_code & 0x7f;
1380 hdmi_writeb(hdmi, val, HDMI_FC_AVIVID);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001381
1382 /* AVI Data Byte 5- set up input and output pixel repetition */
1383 val = (((hdmi->hdmi_data.video_mode.mpixelrepetitioninput + 1) <<
1384 HDMI_FC_PRCONF_INCOMING_PR_FACTOR_OFFSET) &
1385 HDMI_FC_PRCONF_INCOMING_PR_FACTOR_MASK) |
1386 ((hdmi->hdmi_data.video_mode.mpixelrepetitionoutput <<
1387 HDMI_FC_PRCONF_OUTPUT_PR_FACTOR_OFFSET) &
1388 HDMI_FC_PRCONF_OUTPUT_PR_FACTOR_MASK);
1389 hdmi_writeb(hdmi, val, HDMI_FC_PRCONF);
1390
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001391 /*
1392 * AVI data byte 5 differences: content type in 0,1 rather than 4,5,
1393 * ycc range in bits 2,3 rather than 6,7
1394 */
1395 val = ((frame.ycc_quantization_range & 0x3) << 2) |
1396 (frame.content_type & 0x3);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001397 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF3);
1398
1399 /* AVI Data Bytes 6-13 */
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001400 hdmi_writeb(hdmi, frame.top_bar & 0xff, HDMI_FC_AVIETB0);
1401 hdmi_writeb(hdmi, (frame.top_bar >> 8) & 0xff, HDMI_FC_AVIETB1);
1402 hdmi_writeb(hdmi, frame.bottom_bar & 0xff, HDMI_FC_AVISBB0);
1403 hdmi_writeb(hdmi, (frame.bottom_bar >> 8) & 0xff, HDMI_FC_AVISBB1);
1404 hdmi_writeb(hdmi, frame.left_bar & 0xff, HDMI_FC_AVIELB0);
1405 hdmi_writeb(hdmi, (frame.left_bar >> 8) & 0xff, HDMI_FC_AVIELB1);
1406 hdmi_writeb(hdmi, frame.right_bar & 0xff, HDMI_FC_AVISRB0);
1407 hdmi_writeb(hdmi, (frame.right_bar >> 8) & 0xff, HDMI_FC_AVISRB1);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001408}
1409
Nickey Yang9aa1eca2017-03-21 15:36:17 +08001410static void hdmi_config_vendor_specific_infoframe(struct dw_hdmi *hdmi,
1411 struct drm_display_mode *mode)
1412{
1413 struct hdmi_vendor_infoframe frame;
1414 u8 buffer[10];
1415 ssize_t err;
1416
1417 err = drm_hdmi_vendor_infoframe_from_display_mode(&frame, mode);
1418 if (err < 0)
1419 /*
1420 * Going into that statement does not means vendor infoframe
1421 * fails. It just informed us that vendor infoframe is not
1422 * needed for the selected mode. Only 4k or stereoscopic 3D
1423 * mode requires vendor infoframe. So just simply return.
1424 */
1425 return;
1426
1427 err = hdmi_vendor_infoframe_pack(&frame, buffer, sizeof(buffer));
1428 if (err < 0) {
1429 dev_err(hdmi->dev, "Failed to pack vendor infoframe: %zd\n",
1430 err);
1431 return;
1432 }
1433 hdmi_mask_writeb(hdmi, 0, HDMI_FC_DATAUTO0, HDMI_FC_DATAUTO0_VSD_OFFSET,
1434 HDMI_FC_DATAUTO0_VSD_MASK);
1435
1436 /* Set the length of HDMI vendor specific InfoFrame payload */
1437 hdmi_writeb(hdmi, buffer[2], HDMI_FC_VSDSIZE);
1438
1439 /* Set 24bit IEEE Registration Identifier */
1440 hdmi_writeb(hdmi, buffer[4], HDMI_FC_VSDIEEEID0);
1441 hdmi_writeb(hdmi, buffer[5], HDMI_FC_VSDIEEEID1);
1442 hdmi_writeb(hdmi, buffer[6], HDMI_FC_VSDIEEEID2);
1443
1444 /* Set HDMI_Video_Format and HDMI_VIC/3D_Structure */
1445 hdmi_writeb(hdmi, buffer[7], HDMI_FC_VSDPAYLOAD0);
1446 hdmi_writeb(hdmi, buffer[8], HDMI_FC_VSDPAYLOAD1);
1447
1448 if (frame.s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF)
1449 hdmi_writeb(hdmi, buffer[9], HDMI_FC_VSDPAYLOAD2);
1450
1451 /* Packet frame interpolation */
1452 hdmi_writeb(hdmi, 1, HDMI_FC_DATAUTO1);
1453
1454 /* Auto packets per frame and line spacing */
1455 hdmi_writeb(hdmi, 0x11, HDMI_FC_DATAUTO2);
1456
1457 /* Configures the Frame Composer On RDRB mode */
1458 hdmi_mask_writeb(hdmi, 1, HDMI_FC_DATAUTO0, HDMI_FC_DATAUTO0_VSD_OFFSET,
1459 HDMI_FC_DATAUTO0_VSD_MASK);
1460}
1461
Andy Yanb21f4b62014-12-05 14:26:31 +08001462static void hdmi_av_composer(struct dw_hdmi *hdmi,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001463 const struct drm_display_mode *mode)
1464{
1465 u8 inv_val;
1466 struct hdmi_vmode *vmode = &hdmi->hdmi_data.video_mode;
1467 int hblank, vblank, h_de_hs, v_de_vs, hsync_len, vsync_len;
Russell Kinge80b9f42015-07-21 11:08:25 +01001468 unsigned int vdisplay;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001469
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001470 vmode->mpixelclock = mode->clock * 1000;
1471
1472 dev_dbg(hdmi->dev, "final pixclk = %d\n", vmode->mpixelclock);
1473
1474 /* Set up HDMI_FC_INVIDCONF */
1475 inv_val = (hdmi->hdmi_data.hdcp_enable ?
1476 HDMI_FC_INVIDCONF_HDCP_KEEPOUT_ACTIVE :
1477 HDMI_FC_INVIDCONF_HDCP_KEEPOUT_INACTIVE);
1478
Russell Kingb91eee82015-03-27 23:27:17 +00001479 inv_val |= mode->flags & DRM_MODE_FLAG_PVSYNC ?
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001480 HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_HIGH :
Russell Kingb91eee82015-03-27 23:27:17 +00001481 HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_LOW;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001482
Russell Kingb91eee82015-03-27 23:27:17 +00001483 inv_val |= mode->flags & DRM_MODE_FLAG_PHSYNC ?
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001484 HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_HIGH :
Russell Kingb91eee82015-03-27 23:27:17 +00001485 HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_LOW;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001486
1487 inv_val |= (vmode->mdataenablepolarity ?
1488 HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_HIGH :
1489 HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_LOW);
1490
1491 if (hdmi->vic == 39)
1492 inv_val |= HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_HIGH;
1493 else
Russell Kingb91eee82015-03-27 23:27:17 +00001494 inv_val |= mode->flags & DRM_MODE_FLAG_INTERLACE ?
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001495 HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_HIGH :
Russell Kingb91eee82015-03-27 23:27:17 +00001496 HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_LOW;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001497
Russell Kingb91eee82015-03-27 23:27:17 +00001498 inv_val |= mode->flags & DRM_MODE_FLAG_INTERLACE ?
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001499 HDMI_FC_INVIDCONF_IN_I_P_INTERLACED :
Russell Kingb91eee82015-03-27 23:27:17 +00001500 HDMI_FC_INVIDCONF_IN_I_P_PROGRESSIVE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001501
Russell King05b13422015-07-21 15:35:52 +01001502 inv_val |= hdmi->sink_is_hdmi ?
1503 HDMI_FC_INVIDCONF_DVI_MODEZ_HDMI_MODE :
1504 HDMI_FC_INVIDCONF_DVI_MODEZ_DVI_MODE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001505
1506 hdmi_writeb(hdmi, inv_val, HDMI_FC_INVIDCONF);
1507
Russell Kinge80b9f42015-07-21 11:08:25 +01001508 vdisplay = mode->vdisplay;
1509 vblank = mode->vtotal - mode->vdisplay;
1510 v_de_vs = mode->vsync_start - mode->vdisplay;
1511 vsync_len = mode->vsync_end - mode->vsync_start;
1512
1513 /*
1514 * When we're setting an interlaced mode, we need
1515 * to adjust the vertical timing to suit.
1516 */
1517 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
1518 vdisplay /= 2;
1519 vblank /= 2;
1520 v_de_vs /= 2;
1521 vsync_len /= 2;
1522 }
1523
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001524 /* Set up horizontal active pixel width */
1525 hdmi_writeb(hdmi, mode->hdisplay >> 8, HDMI_FC_INHACTV1);
1526 hdmi_writeb(hdmi, mode->hdisplay, HDMI_FC_INHACTV0);
1527
1528 /* Set up vertical active lines */
Russell Kinge80b9f42015-07-21 11:08:25 +01001529 hdmi_writeb(hdmi, vdisplay >> 8, HDMI_FC_INVACTV1);
1530 hdmi_writeb(hdmi, vdisplay, HDMI_FC_INVACTV0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001531
1532 /* Set up horizontal blanking pixel region width */
1533 hblank = mode->htotal - mode->hdisplay;
1534 hdmi_writeb(hdmi, hblank >> 8, HDMI_FC_INHBLANK1);
1535 hdmi_writeb(hdmi, hblank, HDMI_FC_INHBLANK0);
1536
1537 /* Set up vertical blanking pixel region width */
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001538 hdmi_writeb(hdmi, vblank, HDMI_FC_INVBLANK);
1539
1540 /* Set up HSYNC active edge delay width (in pixel clks) */
1541 h_de_hs = mode->hsync_start - mode->hdisplay;
1542 hdmi_writeb(hdmi, h_de_hs >> 8, HDMI_FC_HSYNCINDELAY1);
1543 hdmi_writeb(hdmi, h_de_hs, HDMI_FC_HSYNCINDELAY0);
1544
1545 /* Set up VSYNC active edge delay (in lines) */
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001546 hdmi_writeb(hdmi, v_de_vs, HDMI_FC_VSYNCINDELAY);
1547
1548 /* Set up HSYNC active pulse width (in pixel clks) */
1549 hsync_len = mode->hsync_end - mode->hsync_start;
1550 hdmi_writeb(hdmi, hsync_len >> 8, HDMI_FC_HSYNCINWIDTH1);
1551 hdmi_writeb(hdmi, hsync_len, HDMI_FC_HSYNCINWIDTH0);
1552
1553 /* Set up VSYNC active edge delay (in lines) */
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001554 hdmi_writeb(hdmi, vsync_len, HDMI_FC_VSYNCINWIDTH);
1555}
1556
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001557/* HDMI Initialization Step B.4 */
Andy Yanb21f4b62014-12-05 14:26:31 +08001558static void dw_hdmi_enable_video_path(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001559{
1560 u8 clkdis;
1561
1562 /* control period minimum duration */
1563 hdmi_writeb(hdmi, 12, HDMI_FC_CTRLDUR);
1564 hdmi_writeb(hdmi, 32, HDMI_FC_EXCTRLDUR);
1565 hdmi_writeb(hdmi, 1, HDMI_FC_EXCTRLSPAC);
1566
1567 /* Set to fill TMDS data channels */
1568 hdmi_writeb(hdmi, 0x0B, HDMI_FC_CH0PREAM);
1569 hdmi_writeb(hdmi, 0x16, HDMI_FC_CH1PREAM);
1570 hdmi_writeb(hdmi, 0x21, HDMI_FC_CH2PREAM);
1571
1572 /* Enable pixel clock and tmds data path */
1573 clkdis = 0x7F;
1574 clkdis &= ~HDMI_MC_CLKDIS_PIXELCLK_DISABLE;
1575 hdmi_writeb(hdmi, clkdis, HDMI_MC_CLKDIS);
1576
1577 clkdis &= ~HDMI_MC_CLKDIS_TMDSCLK_DISABLE;
1578 hdmi_writeb(hdmi, clkdis, HDMI_MC_CLKDIS);
1579
1580 /* Enable csc path */
1581 if (is_color_space_conversion(hdmi)) {
1582 clkdis &= ~HDMI_MC_CLKDIS_CSCCLK_DISABLE;
1583 hdmi_writeb(hdmi, clkdis, HDMI_MC_CLKDIS);
1584 }
Laurent Pinchart8b9e1c02017-03-03 19:19:59 +02001585
Neil Armstrong14247d72017-03-03 19:20:00 +02001586 /* Enable color space conversion if needed */
1587 if (is_color_space_conversion(hdmi))
Laurent Pinchart8b9e1c02017-03-03 19:19:59 +02001588 hdmi_writeb(hdmi, HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_IN_PATH,
1589 HDMI_MC_FLOWCTRL);
1590 else
1591 hdmi_writeb(hdmi, HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_BYPASS,
1592 HDMI_MC_FLOWCTRL);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001593}
1594
Andy Yanb21f4b62014-12-05 14:26:31 +08001595static void hdmi_enable_audio_clk(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001596{
Russell King812bc612013-11-04 12:42:02 +00001597 hdmi_modb(hdmi, 0, HDMI_MC_CLKDIS_AUDCLK_DISABLE, HDMI_MC_CLKDIS);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001598}
1599
1600/* Workaround to clear the overflow condition */
Andy Yanb21f4b62014-12-05 14:26:31 +08001601static void dw_hdmi_clear_overflow(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001602{
Laurent Pinchartbe41fc52017-01-17 10:29:05 +02001603 unsigned int count;
1604 unsigned int i;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001605 u8 val;
1606
Laurent Pinchartbe41fc52017-01-17 10:29:05 +02001607 /*
1608 * Under some circumstances the Frame Composer arithmetic unit can miss
1609 * an FC register write due to being busy processing the previous one.
1610 * The issue can be worked around by issuing a TMDS software reset and
1611 * then write one of the FC registers several times.
1612 *
1613 * The number of iterations matters and depends on the HDMI TX revision
1614 * (and possibly on the platform). So far only i.MX6Q (v1.30a) and
1615 * i.MX6DL (v1.31a) have been identified as needing the workaround, with
1616 * 4 and 1 iterations respectively.
1617 */
1618
1619 switch (hdmi->version) {
1620 case 0x130a:
1621 count = 4;
1622 break;
1623 case 0x131a:
1624 count = 1;
1625 break;
1626 default:
1627 return;
1628 }
1629
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001630 /* TMDS software reset */
1631 hdmi_writeb(hdmi, (u8)~HDMI_MC_SWRSTZ_TMDSSWRST_REQ, HDMI_MC_SWRSTZ);
1632
1633 val = hdmi_readb(hdmi, HDMI_FC_INVIDCONF);
Laurent Pinchartbe41fc52017-01-17 10:29:05 +02001634 for (i = 0; i < count; i++)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001635 hdmi_writeb(hdmi, val, HDMI_FC_INVIDCONF);
1636}
1637
Andy Yanb21f4b62014-12-05 14:26:31 +08001638static void hdmi_enable_overflow_interrupts(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001639{
1640 hdmi_writeb(hdmi, 0, HDMI_FC_MASK2);
1641 hdmi_writeb(hdmi, 0, HDMI_IH_MUTE_FC_STAT2);
1642}
1643
Andy Yanb21f4b62014-12-05 14:26:31 +08001644static void hdmi_disable_overflow_interrupts(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001645{
1646 hdmi_writeb(hdmi, HDMI_IH_MUTE_FC_STAT2_OVERFLOW_MASK,
1647 HDMI_IH_MUTE_FC_STAT2);
1648}
1649
Andy Yanb21f4b62014-12-05 14:26:31 +08001650static int dw_hdmi_setup(struct dw_hdmi *hdmi, struct drm_display_mode *mode)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001651{
1652 int ret;
1653
1654 hdmi_disable_overflow_interrupts(hdmi);
1655
1656 hdmi->vic = drm_match_cea_mode(mode);
1657
1658 if (!hdmi->vic) {
1659 dev_dbg(hdmi->dev, "Non-CEA mode used in HDMI\n");
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001660 } else {
1661 dev_dbg(hdmi->dev, "CEA mode used vic=%d\n", hdmi->vic);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001662 }
1663
1664 if ((hdmi->vic == 6) || (hdmi->vic == 7) ||
Andy Yanb5878332014-12-05 14:23:52 +08001665 (hdmi->vic == 21) || (hdmi->vic == 22) ||
1666 (hdmi->vic == 2) || (hdmi->vic == 3) ||
1667 (hdmi->vic == 17) || (hdmi->vic == 18))
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001668 hdmi->hdmi_data.enc_out_encoding = V4L2_YCBCR_ENC_601;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001669 else
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001670 hdmi->hdmi_data.enc_out_encoding = V4L2_YCBCR_ENC_709;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001671
Russell Kingd10ca822015-07-21 11:25:00 +01001672 hdmi->hdmi_data.video_mode.mpixelrepetitionoutput = 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001673 hdmi->hdmi_data.video_mode.mpixelrepetitioninput = 0;
1674
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001675 /* TOFIX: Get input format from plat data or fallback to RGB888 */
Neil Armstronge20c29a2017-04-06 11:34:04 +02001676 if (hdmi->plat_data->input_bus_format)
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001677 hdmi->hdmi_data.enc_in_bus_format =
1678 hdmi->plat_data->input_bus_format;
1679 else
1680 hdmi->hdmi_data.enc_in_bus_format = MEDIA_BUS_FMT_RGB888_1X24;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001681
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001682 /* TOFIX: Get input encoding from plat data or fallback to none */
Neil Armstronge20c29a2017-04-06 11:34:04 +02001683 if (hdmi->plat_data->input_bus_encoding)
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001684 hdmi->hdmi_data.enc_in_encoding =
1685 hdmi->plat_data->input_bus_encoding;
1686 else
1687 hdmi->hdmi_data.enc_in_encoding = V4L2_YCBCR_ENC_DEFAULT;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001688
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001689 /* TOFIX: Default to RGB888 output format */
1690 hdmi->hdmi_data.enc_out_bus_format = MEDIA_BUS_FMT_RGB888_1X24;
1691
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001692 hdmi->hdmi_data.pix_repet_factor = 0;
1693 hdmi->hdmi_data.hdcp_enable = 0;
1694 hdmi->hdmi_data.video_mode.mdataenablepolarity = true;
1695
1696 /* HDMI Initialization Step B.1 */
1697 hdmi_av_composer(hdmi, mode);
1698
1699 /* HDMI Initializateion Step B.2 */
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001700 ret = hdmi->phy.ops->init(hdmi, hdmi->phy.data, &hdmi->previous_mode);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001701 if (ret)
1702 return ret;
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001703 hdmi->phy.enabled = true;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001704
1705 /* HDMI Initialization Step B.3 */
Andy Yanb21f4b62014-12-05 14:26:31 +08001706 dw_hdmi_enable_video_path(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001707
Russell Kingf709ec02015-07-21 16:09:39 +01001708 if (hdmi->sink_has_audio) {
1709 dev_dbg(hdmi->dev, "sink has audio support\n");
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001710
1711 /* HDMI Initialization Step E - Configure audio */
1712 hdmi_clk_regenerator_update_pixel_clock(hdmi);
1713 hdmi_enable_audio_clk(hdmi);
Russell Kingf709ec02015-07-21 16:09:39 +01001714 }
1715
1716 /* not for DVI mode */
1717 if (hdmi->sink_is_hdmi) {
1718 dev_dbg(hdmi->dev, "%s HDMI mode\n", __func__);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001719
1720 /* HDMI Initialization Step F - Configure AVI InfoFrame */
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001721 hdmi_config_AVI(hdmi, mode);
Nickey Yang9aa1eca2017-03-21 15:36:17 +08001722 hdmi_config_vendor_specific_infoframe(hdmi, mode);
Russell King05b13422015-07-21 15:35:52 +01001723 } else {
1724 dev_dbg(hdmi->dev, "%s DVI mode\n", __func__);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001725 }
1726
1727 hdmi_video_packetize(hdmi);
1728 hdmi_video_csc(hdmi);
1729 hdmi_video_sample(hdmi);
1730 hdmi_tx_hdcp_config(hdmi);
1731
Andy Yanb21f4b62014-12-05 14:26:31 +08001732 dw_hdmi_clear_overflow(hdmi);
Russell King05b13422015-07-21 15:35:52 +01001733 if (hdmi->cable_plugin && hdmi->sink_is_hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001734 hdmi_enable_overflow_interrupts(hdmi);
1735
1736 return 0;
1737}
1738
Laurent Pincharta23d6262017-04-04 14:31:56 +02001739static void dw_hdmi_setup_i2c(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001740{
1741 hdmi_writeb(hdmi, HDMI_PHY_I2CM_INT_ADDR_DONE_POL,
1742 HDMI_PHY_I2CM_INT_ADDR);
1743
1744 hdmi_writeb(hdmi, HDMI_PHY_I2CM_CTLINT_ADDR_NAC_POL |
1745 HDMI_PHY_I2CM_CTLINT_ADDR_ARBITRATION_POL,
1746 HDMI_PHY_I2CM_CTLINT_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001747}
1748
Andy Yanb21f4b62014-12-05 14:26:31 +08001749static void initialize_hdmi_ih_mutes(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001750{
1751 u8 ih_mute;
1752
1753 /*
1754 * Boot up defaults are:
1755 * HDMI_IH_MUTE = 0x03 (disabled)
1756 * HDMI_IH_MUTE_* = 0x00 (enabled)
1757 *
1758 * Disable top level interrupt bits in HDMI block
1759 */
1760 ih_mute = hdmi_readb(hdmi, HDMI_IH_MUTE) |
1761 HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |
1762 HDMI_IH_MUTE_MUTE_ALL_INTERRUPT;
1763
1764 hdmi_writeb(hdmi, ih_mute, HDMI_IH_MUTE);
1765
1766 /* by default mask all interrupts */
1767 hdmi_writeb(hdmi, 0xff, HDMI_VP_MASK);
1768 hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK0);
1769 hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK1);
1770 hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK2);
1771 hdmi_writeb(hdmi, 0xff, HDMI_PHY_MASK0);
1772 hdmi_writeb(hdmi, 0xff, HDMI_PHY_I2CM_INT_ADDR);
1773 hdmi_writeb(hdmi, 0xff, HDMI_PHY_I2CM_CTLINT_ADDR);
1774 hdmi_writeb(hdmi, 0xff, HDMI_AUD_INT);
1775 hdmi_writeb(hdmi, 0xff, HDMI_AUD_SPDIFINT);
1776 hdmi_writeb(hdmi, 0xff, HDMI_AUD_HBR_MASK);
1777 hdmi_writeb(hdmi, 0xff, HDMI_GP_MASK);
1778 hdmi_writeb(hdmi, 0xff, HDMI_A_APIINTMSK);
1779 hdmi_writeb(hdmi, 0xff, HDMI_CEC_MASK);
1780 hdmi_writeb(hdmi, 0xff, HDMI_I2CM_INT);
1781 hdmi_writeb(hdmi, 0xff, HDMI_I2CM_CTLINT);
1782
1783 /* Disable interrupts in the IH_MUTE_* registers */
1784 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT0);
1785 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT1);
1786 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT2);
1787 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_AS_STAT0);
1788 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_PHY_STAT0);
1789 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_I2CM_STAT0);
1790 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_CEC_STAT0);
1791 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_VP_STAT0);
1792 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_I2CMPHY_STAT0);
1793 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_AHBDMAAUD_STAT0);
1794
1795 /* Enable top level interrupt bits in HDMI block */
1796 ih_mute &= ~(HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |
1797 HDMI_IH_MUTE_MUTE_ALL_INTERRUPT);
1798 hdmi_writeb(hdmi, ih_mute, HDMI_IH_MUTE);
1799}
1800
Andy Yanb21f4b62014-12-05 14:26:31 +08001801static void dw_hdmi_poweron(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001802{
Russell King381f05a2015-06-05 15:25:08 +01001803 hdmi->bridge_is_on = true;
Andy Yanb21f4b62014-12-05 14:26:31 +08001804 dw_hdmi_setup(hdmi, &hdmi->previous_mode);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001805}
1806
Andy Yanb21f4b62014-12-05 14:26:31 +08001807static void dw_hdmi_poweroff(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001808{
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001809 if (hdmi->phy.enabled) {
1810 hdmi->phy.ops->disable(hdmi, hdmi->phy.data);
1811 hdmi->phy.enabled = false;
1812 }
1813
Russell King381f05a2015-06-05 15:25:08 +01001814 hdmi->bridge_is_on = false;
1815}
1816
1817static void dw_hdmi_update_power(struct dw_hdmi *hdmi)
1818{
1819 int force = hdmi->force;
1820
1821 if (hdmi->disabled) {
1822 force = DRM_FORCE_OFF;
1823 } else if (force == DRM_FORCE_UNSPECIFIED) {
Russell Kingaeac23b2015-06-05 13:46:22 +01001824 if (hdmi->rxsense)
Russell King381f05a2015-06-05 15:25:08 +01001825 force = DRM_FORCE_ON;
1826 else
1827 force = DRM_FORCE_OFF;
1828 }
1829
1830 if (force == DRM_FORCE_OFF) {
1831 if (hdmi->bridge_is_on)
1832 dw_hdmi_poweroff(hdmi);
1833 } else {
1834 if (!hdmi->bridge_is_on)
1835 dw_hdmi_poweron(hdmi);
1836 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001837}
1838
Russell Kingaeac23b2015-06-05 13:46:22 +01001839/*
1840 * Adjust the detection of RXSENSE according to whether we have a forced
1841 * connection mode enabled, or whether we have been disabled. There is
1842 * no point processing RXSENSE interrupts if we have a forced connection
1843 * state, or DRM has us disabled.
1844 *
1845 * We also disable rxsense interrupts when we think we're disconnected
1846 * to avoid floating TDMS signals giving false rxsense interrupts.
1847 *
1848 * Note: we still need to listen for HPD interrupts even when DRM has us
1849 * disabled so that we can detect a connect event.
1850 */
1851static void dw_hdmi_update_phy_mask(struct dw_hdmi *hdmi)
1852{
Neil Armstrong386d3292017-04-04 14:31:59 +02001853 if (hdmi->phy.ops->update_hpd)
1854 hdmi->phy.ops->update_hpd(hdmi, hdmi->phy.data,
1855 hdmi->force, hdmi->disabled,
1856 hdmi->rxsense);
Laurent Pincharta23d6262017-04-04 14:31:56 +02001857}
1858
Andy Yanb21f4b62014-12-05 14:26:31 +08001859static enum drm_connector_status
1860dw_hdmi_connector_detect(struct drm_connector *connector, bool force)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001861{
Andy Yanb21f4b62014-12-05 14:26:31 +08001862 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
Russell Kingd94905e2013-11-03 22:23:24 +00001863 connector);
Russell King98dbead2014-04-18 10:46:45 +01001864
Russell King381f05a2015-06-05 15:25:08 +01001865 mutex_lock(&hdmi->mutex);
1866 hdmi->force = DRM_FORCE_UNSPECIFIED;
1867 dw_hdmi_update_power(hdmi);
Russell Kingaeac23b2015-06-05 13:46:22 +01001868 dw_hdmi_update_phy_mask(hdmi);
Russell King381f05a2015-06-05 15:25:08 +01001869 mutex_unlock(&hdmi->mutex);
1870
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001871 return hdmi->phy.ops->read_hpd(hdmi, hdmi->phy.data);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001872}
1873
Andy Yanb21f4b62014-12-05 14:26:31 +08001874static int dw_hdmi_connector_get_modes(struct drm_connector *connector)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001875{
Andy Yanb21f4b62014-12-05 14:26:31 +08001876 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001877 connector);
1878 struct edid *edid;
Doug Anderson6c7e66e2015-06-04 11:04:36 -07001879 int ret = 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001880
1881 if (!hdmi->ddc)
1882 return 0;
1883
1884 edid = drm_get_edid(connector, hdmi->ddc);
1885 if (edid) {
1886 dev_dbg(hdmi->dev, "got edid: width[%d] x height[%d]\n",
1887 edid->width_cm, edid->height_cm);
1888
Russell King05b13422015-07-21 15:35:52 +01001889 hdmi->sink_is_hdmi = drm_detect_hdmi_monitor(edid);
Russell Kingf709ec02015-07-21 16:09:39 +01001890 hdmi->sink_has_audio = drm_detect_monitor_audio(edid);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001891 drm_mode_connector_update_edid_property(connector, edid);
1892 ret = drm_add_edid_modes(connector, edid);
Russell Kingf5ce4052013-11-07 16:06:01 +00001893 /* Store the ELD */
1894 drm_edid_to_eld(connector, edid);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001895 kfree(edid);
1896 } else {
1897 dev_dbg(hdmi->dev, "failed to get edid\n");
1898 }
1899
Doug Anderson6c7e66e2015-06-04 11:04:36 -07001900 return ret;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001901}
1902
Andy Yan632d0352014-12-05 14:30:21 +08001903static enum drm_mode_status
1904dw_hdmi_connector_mode_valid(struct drm_connector *connector,
1905 struct drm_display_mode *mode)
1906{
1907 struct dw_hdmi *hdmi = container_of(connector,
1908 struct dw_hdmi, connector);
1909 enum drm_mode_status mode_status = MODE_OK;
1910
Russell King8add4192015-07-22 11:14:00 +01001911 /* We don't support double-clocked modes */
1912 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
1913 return MODE_BAD;
1914
Andy Yan632d0352014-12-05 14:30:21 +08001915 if (hdmi->plat_data->mode_valid)
1916 mode_status = hdmi->plat_data->mode_valid(connector, mode);
1917
1918 return mode_status;
1919}
1920
Russell King381f05a2015-06-05 15:25:08 +01001921static void dw_hdmi_connector_force(struct drm_connector *connector)
1922{
1923 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
1924 connector);
1925
1926 mutex_lock(&hdmi->mutex);
1927 hdmi->force = connector->force;
1928 dw_hdmi_update_power(hdmi);
Russell Kingaeac23b2015-06-05 13:46:22 +01001929 dw_hdmi_update_phy_mask(hdmi);
Russell King381f05a2015-06-05 15:25:08 +01001930 mutex_unlock(&hdmi->mutex);
1931}
1932
Ville Syrjälädae91e42015-12-15 12:21:02 +01001933static const struct drm_connector_funcs dw_hdmi_connector_funcs = {
Mark Yao2c5b2cc2015-11-30 18:33:40 +08001934 .dpms = drm_atomic_helper_connector_dpms,
1935 .fill_modes = drm_helper_probe_single_connector_modes,
1936 .detect = dw_hdmi_connector_detect,
Marek Vasutfdd83262016-10-05 16:31:33 +02001937 .destroy = drm_connector_cleanup,
Mark Yao2c5b2cc2015-11-30 18:33:40 +08001938 .force = dw_hdmi_connector_force,
1939 .reset = drm_atomic_helper_connector_reset,
1940 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1941 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1942};
1943
Ville Syrjälädae91e42015-12-15 12:21:02 +01001944static const struct drm_connector_helper_funcs dw_hdmi_connector_helper_funcs = {
Andy Yanb21f4b62014-12-05 14:26:31 +08001945 .get_modes = dw_hdmi_connector_get_modes,
Andy Yan632d0352014-12-05 14:30:21 +08001946 .mode_valid = dw_hdmi_connector_mode_valid,
Boris Brezillonc2a441f2016-06-07 13:48:15 +02001947 .best_encoder = drm_atomic_helper_best_encoder,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001948};
1949
Laurent Pinchartd2ae94a2017-01-17 10:28:59 +02001950static int dw_hdmi_bridge_attach(struct drm_bridge *bridge)
1951{
1952 struct dw_hdmi *hdmi = bridge->driver_private;
1953 struct drm_encoder *encoder = bridge->encoder;
1954 struct drm_connector *connector = &hdmi->connector;
1955
1956 connector->interlace_allowed = 1;
1957 connector->polled = DRM_CONNECTOR_POLL_HPD;
1958
1959 drm_connector_helper_add(connector, &dw_hdmi_connector_helper_funcs);
1960
1961 drm_connector_init(bridge->dev, connector, &dw_hdmi_connector_funcs,
1962 DRM_MODE_CONNECTOR_HDMIA);
1963
1964 drm_mode_connector_attach_encoder(connector, encoder);
1965
1966 return 0;
1967}
1968
Laurent Pinchartfd30b382017-01-17 10:28:58 +02001969static void dw_hdmi_bridge_mode_set(struct drm_bridge *bridge,
1970 struct drm_display_mode *orig_mode,
1971 struct drm_display_mode *mode)
1972{
1973 struct dw_hdmi *hdmi = bridge->driver_private;
1974
1975 mutex_lock(&hdmi->mutex);
1976
1977 /* Store the display mode for plugin/DKMS poweron events */
1978 memcpy(&hdmi->previous_mode, mode, sizeof(hdmi->previous_mode));
1979
1980 mutex_unlock(&hdmi->mutex);
1981}
1982
1983static void dw_hdmi_bridge_disable(struct drm_bridge *bridge)
1984{
1985 struct dw_hdmi *hdmi = bridge->driver_private;
1986
1987 mutex_lock(&hdmi->mutex);
1988 hdmi->disabled = true;
1989 dw_hdmi_update_power(hdmi);
1990 dw_hdmi_update_phy_mask(hdmi);
1991 mutex_unlock(&hdmi->mutex);
1992}
1993
1994static void dw_hdmi_bridge_enable(struct drm_bridge *bridge)
1995{
1996 struct dw_hdmi *hdmi = bridge->driver_private;
1997
1998 mutex_lock(&hdmi->mutex);
1999 hdmi->disabled = false;
2000 dw_hdmi_update_power(hdmi);
2001 dw_hdmi_update_phy_mask(hdmi);
2002 mutex_unlock(&hdmi->mutex);
2003}
2004
Ville Syrjälädae91e42015-12-15 12:21:02 +01002005static const struct drm_bridge_funcs dw_hdmi_bridge_funcs = {
Laurent Pinchartd2ae94a2017-01-17 10:28:59 +02002006 .attach = dw_hdmi_bridge_attach,
Andy Yanb21f4b62014-12-05 14:26:31 +08002007 .enable = dw_hdmi_bridge_enable,
2008 .disable = dw_hdmi_bridge_disable,
Andy Yanb21f4b62014-12-05 14:26:31 +08002009 .mode_set = dw_hdmi_bridge_mode_set,
Andy Yan3d1b35a2014-12-05 14:25:05 +08002010};
2011
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002012static irqreturn_t dw_hdmi_i2c_irq(struct dw_hdmi *hdmi)
2013{
2014 struct dw_hdmi_i2c *i2c = hdmi->i2c;
2015 unsigned int stat;
2016
2017 stat = hdmi_readb(hdmi, HDMI_IH_I2CM_STAT0);
2018 if (!stat)
2019 return IRQ_NONE;
2020
2021 hdmi_writeb(hdmi, stat, HDMI_IH_I2CM_STAT0);
2022
2023 i2c->stat = stat;
2024
2025 complete(&i2c->cmp);
2026
2027 return IRQ_HANDLED;
2028}
2029
Andy Yanb21f4b62014-12-05 14:26:31 +08002030static irqreturn_t dw_hdmi_hardirq(int irq, void *dev_id)
Russell Kingd94905e2013-11-03 22:23:24 +00002031{
Andy Yanb21f4b62014-12-05 14:26:31 +08002032 struct dw_hdmi *hdmi = dev_id;
Russell Kingd94905e2013-11-03 22:23:24 +00002033 u8 intr_stat;
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002034 irqreturn_t ret = IRQ_NONE;
2035
2036 if (hdmi->i2c)
2037 ret = dw_hdmi_i2c_irq(hdmi);
Russell Kingd94905e2013-11-03 22:23:24 +00002038
2039 intr_stat = hdmi_readb(hdmi, HDMI_IH_PHY_STAT0);
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002040 if (intr_stat) {
Russell Kingd94905e2013-11-03 22:23:24 +00002041 hdmi_writeb(hdmi, ~0, HDMI_IH_MUTE_PHY_STAT0);
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002042 return IRQ_WAKE_THREAD;
2043 }
Russell Kingd94905e2013-11-03 22:23:24 +00002044
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002045 return ret;
Russell Kingd94905e2013-11-03 22:23:24 +00002046}
2047
Neil Armstrong386d3292017-04-04 14:31:59 +02002048void __dw_hdmi_setup_rx_sense(struct dw_hdmi *hdmi, bool hpd, bool rx_sense)
2049{
2050 mutex_lock(&hdmi->mutex);
2051
2052 if (!hdmi->force) {
2053 /*
2054 * If the RX sense status indicates we're disconnected,
2055 * clear the software rxsense status.
2056 */
2057 if (!rx_sense)
2058 hdmi->rxsense = false;
2059
2060 /*
2061 * Only set the software rxsense status when both
2062 * rxsense and hpd indicates we're connected.
2063 * This avoids what seems to be bad behaviour in
2064 * at least iMX6S versions of the phy.
2065 */
2066 if (hpd)
2067 hdmi->rxsense = true;
2068
2069 dw_hdmi_update_power(hdmi);
2070 dw_hdmi_update_phy_mask(hdmi);
2071 }
2072 mutex_unlock(&hdmi->mutex);
2073}
2074
2075void dw_hdmi_setup_rx_sense(struct device *dev, bool hpd, bool rx_sense)
2076{
2077 struct dw_hdmi *hdmi = dev_get_drvdata(dev);
2078
2079 __dw_hdmi_setup_rx_sense(hdmi, hpd, rx_sense);
2080}
2081EXPORT_SYMBOL_GPL(dw_hdmi_setup_rx_sense);
2082
Andy Yanb21f4b62014-12-05 14:26:31 +08002083static irqreturn_t dw_hdmi_irq(int irq, void *dev_id)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002084{
Andy Yanb21f4b62014-12-05 14:26:31 +08002085 struct dw_hdmi *hdmi = dev_id;
Russell Kingaeac23b2015-06-05 13:46:22 +01002086 u8 intr_stat, phy_int_pol, phy_pol_mask, phy_stat;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002087
2088 intr_stat = hdmi_readb(hdmi, HDMI_IH_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002089 phy_int_pol = hdmi_readb(hdmi, HDMI_PHY_POL0);
Russell Kingaeac23b2015-06-05 13:46:22 +01002090 phy_stat = hdmi_readb(hdmi, HDMI_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002091
Russell Kingaeac23b2015-06-05 13:46:22 +01002092 phy_pol_mask = 0;
2093 if (intr_stat & HDMI_IH_PHY_STAT0_HPD)
2094 phy_pol_mask |= HDMI_PHY_HPD;
2095 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE0)
2096 phy_pol_mask |= HDMI_PHY_RX_SENSE0;
2097 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE1)
2098 phy_pol_mask |= HDMI_PHY_RX_SENSE1;
2099 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE2)
2100 phy_pol_mask |= HDMI_PHY_RX_SENSE2;
2101 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE3)
2102 phy_pol_mask |= HDMI_PHY_RX_SENSE3;
2103
2104 if (phy_pol_mask)
2105 hdmi_modb(hdmi, ~phy_int_pol, phy_pol_mask, HDMI_PHY_POL0);
2106
2107 /*
2108 * RX sense tells us whether the TDMS transmitters are detecting
2109 * load - in other words, there's something listening on the
2110 * other end of the link. Use this to decide whether we should
2111 * power on the phy as HPD may be toggled by the sink to merely
2112 * ask the source to re-read the EDID.
2113 */
2114 if (intr_stat &
Neil Armstrong386d3292017-04-04 14:31:59 +02002115 (HDMI_IH_PHY_STAT0_RX_SENSE | HDMI_IH_PHY_STAT0_HPD))
2116 __dw_hdmi_setup_rx_sense(hdmi,
2117 phy_stat & HDMI_PHY_HPD,
2118 phy_stat & HDMI_PHY_RX_SENSE);
Russell Kingaeac23b2015-06-05 13:46:22 +01002119
2120 if (intr_stat & HDMI_IH_PHY_STAT0_HPD) {
2121 dev_dbg(hdmi->dev, "EVENT=%s\n",
2122 phy_int_pol & HDMI_PHY_HPD ? "plugin" : "plugout");
Laurent Pinchartba5d7e62017-01-17 10:28:56 +02002123 if (hdmi->bridge.dev)
2124 drm_helper_hpd_irq_event(hdmi->bridge.dev);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002125 }
2126
2127 hdmi_writeb(hdmi, intr_stat, HDMI_IH_PHY_STAT0);
Russell Kingaeac23b2015-06-05 13:46:22 +01002128 hdmi_writeb(hdmi, ~(HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE),
2129 HDMI_IH_MUTE_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002130
2131 return IRQ_HANDLED;
2132}
2133
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002134static const struct dw_hdmi_phy_data dw_hdmi_phys[] = {
2135 {
2136 .type = DW_HDMI_PHY_DWC_HDMI_TX_PHY,
2137 .name = "DWC HDMI TX PHY",
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02002138 .gen = 1,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002139 }, {
2140 .type = DW_HDMI_PHY_DWC_MHL_PHY_HEAC,
2141 .name = "DWC MHL PHY + HEAC PHY",
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02002142 .gen = 2,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002143 .has_svsret = true,
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02002144 .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002145 }, {
2146 .type = DW_HDMI_PHY_DWC_MHL_PHY,
2147 .name = "DWC MHL PHY",
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02002148 .gen = 2,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002149 .has_svsret = true,
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02002150 .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002151 }, {
2152 .type = DW_HDMI_PHY_DWC_HDMI_3D_TX_PHY_HEAC,
2153 .name = "DWC HDMI 3D TX PHY + HEAC PHY",
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02002154 .gen = 2,
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02002155 .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002156 }, {
2157 .type = DW_HDMI_PHY_DWC_HDMI_3D_TX_PHY,
2158 .name = "DWC HDMI 3D TX PHY",
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02002159 .gen = 2,
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02002160 .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002161 }, {
2162 .type = DW_HDMI_PHY_DWC_HDMI20_TX_PHY,
2163 .name = "DWC HDMI 2.0 TX PHY",
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02002164 .gen = 2,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002165 .has_svsret = true,
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02002166 }, {
2167 .type = DW_HDMI_PHY_VENDOR_PHY,
2168 .name = "Vendor PHY",
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002169 }
2170};
2171
2172static int dw_hdmi_detect_phy(struct dw_hdmi *hdmi)
2173{
2174 unsigned int i;
2175 u8 phy_type;
2176
2177 phy_type = hdmi_readb(hdmi, HDMI_CONFIG2_ID);
2178
Laurent Pinchartf1585f62017-03-06 01:36:15 +02002179 if (phy_type == DW_HDMI_PHY_VENDOR_PHY) {
2180 /* Vendor PHYs require support from the glue layer. */
2181 if (!hdmi->plat_data->phy_ops || !hdmi->plat_data->phy_name) {
2182 dev_err(hdmi->dev,
2183 "Vendor HDMI PHY not supported by glue layer\n");
2184 return -ENODEV;
2185 }
2186
2187 hdmi->phy.ops = hdmi->plat_data->phy_ops;
2188 hdmi->phy.data = hdmi->plat_data->phy_data;
2189 hdmi->phy.name = hdmi->plat_data->phy_name;
2190 return 0;
2191 }
2192
2193 /* Synopsys PHYs are handled internally. */
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002194 for (i = 0; i < ARRAY_SIZE(dw_hdmi_phys); ++i) {
2195 if (dw_hdmi_phys[i].type == phy_type) {
Laurent Pinchartf1585f62017-03-06 01:36:15 +02002196 hdmi->phy.ops = &dw_hdmi_synopsys_phy_ops;
2197 hdmi->phy.name = dw_hdmi_phys[i].name;
2198 hdmi->phy.data = (void *)&dw_hdmi_phys[i];
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02002199
2200 if (!dw_hdmi_phys[i].configure &&
2201 !hdmi->plat_data->configure_phy) {
2202 dev_err(hdmi->dev, "%s requires platform support\n",
2203 hdmi->phy.name);
2204 return -ENODEV;
2205 }
2206
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002207 return 0;
2208 }
2209 }
2210
Laurent Pinchartf1585f62017-03-06 01:36:15 +02002211 dev_err(hdmi->dev, "Unsupported HDMI PHY type (%02x)\n", phy_type);
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002212 return -ENODEV;
2213}
2214
Neil Armstrong80e2f972017-03-03 19:20:06 +02002215static const struct regmap_config hdmi_regmap_8bit_config = {
2216 .reg_bits = 32,
2217 .val_bits = 8,
2218 .reg_stride = 1,
2219 .max_register = HDMI_I2CM_FS_SCL_LCNT_0_ADDR,
2220};
2221
2222static const struct regmap_config hdmi_regmap_32bit_config = {
2223 .reg_bits = 32,
2224 .val_bits = 32,
2225 .reg_stride = 4,
2226 .max_register = HDMI_I2CM_FS_SCL_LCNT_0_ADDR << 2,
2227};
2228
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002229static struct dw_hdmi *
2230__dw_hdmi_probe(struct platform_device *pdev,
2231 const struct dw_hdmi_plat_data *plat_data)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002232{
Laurent Pinchartc6081192017-01-17 10:28:57 +02002233 struct device *dev = &pdev->dev;
Russell King17b50012013-11-03 11:23:34 +00002234 struct device_node *np = dev->of_node;
Russell King7ed6c662013-11-07 16:01:45 +00002235 struct platform_device_info pdevinfo;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002236 struct device_node *ddc_node;
Andy Yanb21f4b62014-12-05 14:26:31 +08002237 struct dw_hdmi *hdmi;
Neil Armstrong80e2f972017-03-03 19:20:06 +02002238 struct resource *iores = NULL;
Laurent Pinchartc6081192017-01-17 10:28:57 +02002239 int irq;
Andy Yan3d1b35a2014-12-05 14:25:05 +08002240 int ret;
Andy Yan0cd9d142014-12-05 14:28:24 +08002241 u32 val = 1;
Laurent Pinchart0527e122017-01-17 10:29:03 +02002242 u8 prod_id0;
2243 u8 prod_id1;
Kuninori Morimoto2761ba62016-11-08 01:00:57 +00002244 u8 config0;
Laurent Pinchart0c674942017-01-17 10:29:04 +02002245 u8 config3;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002246
Russell King17b50012013-11-03 11:23:34 +00002247 hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002248 if (!hdmi)
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002249 return ERR_PTR(-ENOMEM);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002250
Andy Yan3d1b35a2014-12-05 14:25:05 +08002251 hdmi->plat_data = plat_data;
Russell King17b50012013-11-03 11:23:34 +00002252 hdmi->dev = dev;
Russell King40678382013-11-07 15:35:06 +00002253 hdmi->sample_rate = 48000;
Russell Kingb872a8e2015-06-05 12:22:46 +01002254 hdmi->disabled = true;
Russell Kingaeac23b2015-06-05 13:46:22 +01002255 hdmi->rxsense = true;
2256 hdmi->phy_mask = (u8)~(HDMI_PHY_HPD | HDMI_PHY_RX_SENSE);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002257
Russell Kingb872a8e2015-06-05 12:22:46 +01002258 mutex_init(&hdmi->mutex);
Russell King6bcf4952015-02-02 11:01:08 +00002259 mutex_init(&hdmi->audio_mutex);
Russell Kingb90120a2015-03-27 12:59:58 +00002260 spin_lock_init(&hdmi->audio_lock);
Russell King6bcf4952015-02-02 11:01:08 +00002261
Philipp Zabelb5d45902014-03-05 10:20:56 +01002262 ddc_node = of_parse_phandle(np, "ddc-i2c-bus", 0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002263 if (ddc_node) {
Vladimir Zapolskiy9f04a1f2016-08-16 23:26:43 +03002264 hdmi->ddc = of_get_i2c_adapter_by_node(ddc_node);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002265 of_node_put(ddc_node);
Andy Yanc2c38482014-12-05 14:24:28 +08002266 if (!hdmi->ddc) {
2267 dev_dbg(hdmi->dev, "failed to read ddc node\n");
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002268 return ERR_PTR(-EPROBE_DEFER);
Andy Yanc2c38482014-12-05 14:24:28 +08002269 }
2270
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002271 } else {
2272 dev_dbg(hdmi->dev, "no ddc property found\n");
2273 }
2274
Neil Armstrong80e2f972017-03-03 19:20:06 +02002275 if (!plat_data->regm) {
2276 const struct regmap_config *reg_config;
2277
2278 of_property_read_u32(np, "reg-io-width", &val);
2279 switch (val) {
2280 case 4:
2281 reg_config = &hdmi_regmap_32bit_config;
2282 hdmi->reg_shift = 2;
2283 break;
2284 case 1:
2285 reg_config = &hdmi_regmap_8bit_config;
2286 break;
2287 default:
2288 dev_err(dev, "reg-io-width must be 1 or 4\n");
2289 return ERR_PTR(-EINVAL);
2290 }
2291
2292 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2293 hdmi->regs = devm_ioremap_resource(dev, iores);
2294 if (IS_ERR(hdmi->regs)) {
2295 ret = PTR_ERR(hdmi->regs);
2296 goto err_res;
2297 }
2298
2299 hdmi->regm = devm_regmap_init_mmio(dev, hdmi->regs, reg_config);
2300 if (IS_ERR(hdmi->regm)) {
2301 dev_err(dev, "Failed to configure regmap\n");
2302 ret = PTR_ERR(hdmi->regm);
2303 goto err_res;
2304 }
2305 } else {
2306 hdmi->regm = plat_data->regm;
Vladimir Zapolskiy9f04a1f2016-08-16 23:26:43 +03002307 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002308
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002309 hdmi->isfr_clk = devm_clk_get(hdmi->dev, "isfr");
2310 if (IS_ERR(hdmi->isfr_clk)) {
2311 ret = PTR_ERR(hdmi->isfr_clk);
Andy Yanb5878332014-12-05 14:23:52 +08002312 dev_err(hdmi->dev, "Unable to get HDMI isfr clk: %d\n", ret);
Vladimir Zapolskiy9f04a1f2016-08-16 23:26:43 +03002313 goto err_res;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002314 }
2315
2316 ret = clk_prepare_enable(hdmi->isfr_clk);
2317 if (ret) {
Andy Yanb5878332014-12-05 14:23:52 +08002318 dev_err(hdmi->dev, "Cannot enable HDMI isfr clock: %d\n", ret);
Vladimir Zapolskiy9f04a1f2016-08-16 23:26:43 +03002319 goto err_res;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002320 }
2321
2322 hdmi->iahb_clk = devm_clk_get(hdmi->dev, "iahb");
2323 if (IS_ERR(hdmi->iahb_clk)) {
2324 ret = PTR_ERR(hdmi->iahb_clk);
Andy Yanb5878332014-12-05 14:23:52 +08002325 dev_err(hdmi->dev, "Unable to get HDMI iahb clk: %d\n", ret);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002326 goto err_isfr;
2327 }
2328
2329 ret = clk_prepare_enable(hdmi->iahb_clk);
2330 if (ret) {
Andy Yanb5878332014-12-05 14:23:52 +08002331 dev_err(hdmi->dev, "Cannot enable HDMI iahb clock: %d\n", ret);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002332 goto err_isfr;
2333 }
2334
2335 /* Product and revision IDs */
Laurent Pinchartbe41fc52017-01-17 10:29:05 +02002336 hdmi->version = (hdmi_readb(hdmi, HDMI_DESIGN_ID) << 8)
2337 | (hdmi_readb(hdmi, HDMI_REVISION_ID) << 0);
Laurent Pinchart0527e122017-01-17 10:29:03 +02002338 prod_id0 = hdmi_readb(hdmi, HDMI_PRODUCT_ID0);
2339 prod_id1 = hdmi_readb(hdmi, HDMI_PRODUCT_ID1);
2340
2341 if (prod_id0 != HDMI_PRODUCT_ID0_HDMI_TX ||
2342 (prod_id1 & ~HDMI_PRODUCT_ID1_HDCP) != HDMI_PRODUCT_ID1_HDMI_TX) {
2343 dev_err(dev, "Unsupported HDMI controller (%04x:%02x:%02x)\n",
Laurent Pinchartbe41fc52017-01-17 10:29:05 +02002344 hdmi->version, prod_id0, prod_id1);
Laurent Pinchart0527e122017-01-17 10:29:03 +02002345 ret = -ENODEV;
2346 goto err_iahb;
2347 }
2348
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002349 ret = dw_hdmi_detect_phy(hdmi);
2350 if (ret < 0)
2351 goto err_iahb;
2352
2353 dev_info(dev, "Detected HDMI TX controller v%x.%03x %s HDCP (%s)\n",
Laurent Pinchartbe41fc52017-01-17 10:29:05 +02002354 hdmi->version >> 12, hdmi->version & 0xfff,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002355 prod_id1 & HDMI_PRODUCT_ID1_HDCP ? "with" : "without",
Laurent Pinchartf1585f62017-03-06 01:36:15 +02002356 hdmi->phy.name);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002357
2358 initialize_hdmi_ih_mutes(hdmi);
2359
Laurent Pinchartc6081192017-01-17 10:28:57 +02002360 irq = platform_get_irq(pdev, 0);
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002361 if (irq < 0) {
2362 ret = irq;
Laurent Pinchartc6081192017-01-17 10:28:57 +02002363 goto err_iahb;
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002364 }
Laurent Pinchartc6081192017-01-17 10:28:57 +02002365
Philipp Zabel639a2022015-01-07 13:43:50 +01002366 ret = devm_request_threaded_irq(dev, irq, dw_hdmi_hardirq,
2367 dw_hdmi_irq, IRQF_SHARED,
2368 dev_name(dev), hdmi);
2369 if (ret)
Fabio Estevamb33ef612015-01-27 10:54:12 -02002370 goto err_iahb;
Philipp Zabel639a2022015-01-07 13:43:50 +01002371
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002372 /*
2373 * To prevent overflows in HDMI_IH_FC_STAT2, set the clk regenerator
2374 * N and cts values before enabling phy
2375 */
2376 hdmi_init_clk_regenerator(hdmi);
2377
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002378 /* If DDC bus is not specified, try to register HDMI I2C bus */
2379 if (!hdmi->ddc) {
2380 hdmi->ddc = dw_hdmi_i2c_adapter(hdmi);
2381 if (IS_ERR(hdmi->ddc))
2382 hdmi->ddc = NULL;
2383 }
2384
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002385 hdmi->bridge.driver_private = hdmi;
2386 hdmi->bridge.funcs = &dw_hdmi_bridge_funcs;
Arnd Bergmannd5ad7842017-01-23 13:20:38 +01002387#ifdef CONFIG_OF
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002388 hdmi->bridge.of_node = pdev->dev.of_node;
Arnd Bergmannd5ad7842017-01-23 13:20:38 +01002389#endif
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002390
Laurent Pincharta23d6262017-04-04 14:31:56 +02002391 dw_hdmi_setup_i2c(hdmi);
Neil Armstrong386d3292017-04-04 14:31:59 +02002392 if (hdmi->phy.ops->setup_hpd)
2393 hdmi->phy.ops->setup_hpd(hdmi, hdmi->phy.data);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002394
Russell King7ed6c662013-11-07 16:01:45 +00002395 memset(&pdevinfo, 0, sizeof(pdevinfo));
2396 pdevinfo.parent = dev;
2397 pdevinfo.id = PLATFORM_DEVID_AUTO;
2398
Kuninori Morimoto2761ba62016-11-08 01:00:57 +00002399 config0 = hdmi_readb(hdmi, HDMI_CONFIG0_ID);
Laurent Pinchart0c674942017-01-17 10:29:04 +02002400 config3 = hdmi_readb(hdmi, HDMI_CONFIG3_ID);
Kuninori Morimoto2761ba62016-11-08 01:00:57 +00002401
Neil Armstrong80e2f972017-03-03 19:20:06 +02002402 if (iores && config3 & HDMI_CONFIG3_AHBAUDDMA) {
Kuninori Morimoto2761ba62016-11-08 01:00:57 +00002403 struct dw_hdmi_audio_data audio;
2404
Russell King7ed6c662013-11-07 16:01:45 +00002405 audio.phys = iores->start;
2406 audio.base = hdmi->regs;
2407 audio.irq = irq;
2408 audio.hdmi = hdmi;
Russell Kingf5ce4052013-11-07 16:06:01 +00002409 audio.eld = hdmi->connector.eld;
Romain Periera7d555d2017-04-14 10:31:12 +02002410 hdmi->enable_audio = dw_hdmi_ahb_audio_enable;
2411 hdmi->disable_audio = dw_hdmi_ahb_audio_disable;
Russell King7ed6c662013-11-07 16:01:45 +00002412
2413 pdevinfo.name = "dw-hdmi-ahb-audio";
2414 pdevinfo.data = &audio;
2415 pdevinfo.size_data = sizeof(audio);
2416 pdevinfo.dma_mask = DMA_BIT_MASK(32);
2417 hdmi->audio = platform_device_register_full(&pdevinfo);
Kuninori Morimoto2761ba62016-11-08 01:00:57 +00002418 } else if (config0 & HDMI_CONFIG0_I2S) {
2419 struct dw_hdmi_i2s_audio_data audio;
2420
2421 audio.hdmi = hdmi;
2422 audio.write = hdmi_writeb;
2423 audio.read = hdmi_readb;
Romain Periera7d555d2017-04-14 10:31:12 +02002424 hdmi->enable_audio = dw_hdmi_i2s_audio_enable;
Kuninori Morimoto2761ba62016-11-08 01:00:57 +00002425
2426 pdevinfo.name = "dw-hdmi-i2s-audio";
2427 pdevinfo.data = &audio;
2428 pdevinfo.size_data = sizeof(audio);
2429 pdevinfo.dma_mask = DMA_BIT_MASK(32);
2430 hdmi->audio = platform_device_register_full(&pdevinfo);
Russell King7ed6c662013-11-07 16:01:45 +00002431 }
2432
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002433 /* Reset HDMI DDC I2C master controller and mute I2CM interrupts */
2434 if (hdmi->i2c)
2435 dw_hdmi_i2c_init(hdmi);
2436
Laurent Pinchartc6081192017-01-17 10:28:57 +02002437 platform_set_drvdata(pdev, hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002438
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002439 return hdmi;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002440
2441err_iahb:
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002442 if (hdmi->i2c) {
2443 i2c_del_adapter(&hdmi->i2c->adap);
2444 hdmi->ddc = NULL;
2445 }
2446
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002447 clk_disable_unprepare(hdmi->iahb_clk);
2448err_isfr:
2449 clk_disable_unprepare(hdmi->isfr_clk);
Vladimir Zapolskiy9f04a1f2016-08-16 23:26:43 +03002450err_res:
2451 i2c_put_adapter(hdmi->ddc);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002452
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002453 return ERR_PTR(ret);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002454}
2455
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002456static void __dw_hdmi_remove(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002457{
Russell King7ed6c662013-11-07 16:01:45 +00002458 if (hdmi->audio && !IS_ERR(hdmi->audio))
2459 platform_device_unregister(hdmi->audio);
2460
Russell Kingd94905e2013-11-03 22:23:24 +00002461 /* Disable all interrupts */
2462 hdmi_writeb(hdmi, ~0, HDMI_IH_MUTE_PHY_STAT0);
2463
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002464 clk_disable_unprepare(hdmi->iahb_clk);
2465 clk_disable_unprepare(hdmi->isfr_clk);
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002466
2467 if (hdmi->i2c)
2468 i2c_del_adapter(&hdmi->i2c->adap);
2469 else
2470 i2c_put_adapter(hdmi->ddc);
Russell King17b50012013-11-03 11:23:34 +00002471}
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002472
2473/* -----------------------------------------------------------------------------
2474 * Probe/remove API, used from platforms based on the DRM bridge API.
2475 */
2476int dw_hdmi_probe(struct platform_device *pdev,
2477 const struct dw_hdmi_plat_data *plat_data)
2478{
2479 struct dw_hdmi *hdmi;
2480 int ret;
2481
2482 hdmi = __dw_hdmi_probe(pdev, plat_data);
2483 if (IS_ERR(hdmi))
2484 return PTR_ERR(hdmi);
2485
2486 ret = drm_bridge_add(&hdmi->bridge);
2487 if (ret < 0) {
2488 __dw_hdmi_remove(hdmi);
2489 return ret;
2490 }
2491
2492 return 0;
2493}
2494EXPORT_SYMBOL_GPL(dw_hdmi_probe);
2495
2496void dw_hdmi_remove(struct platform_device *pdev)
2497{
2498 struct dw_hdmi *hdmi = platform_get_drvdata(pdev);
2499
2500 drm_bridge_remove(&hdmi->bridge);
2501
2502 __dw_hdmi_remove(hdmi);
2503}
2504EXPORT_SYMBOL_GPL(dw_hdmi_remove);
2505
2506/* -----------------------------------------------------------------------------
2507 * Bind/unbind API, used from platforms based on the component framework.
2508 */
2509int dw_hdmi_bind(struct platform_device *pdev, struct drm_encoder *encoder,
2510 const struct dw_hdmi_plat_data *plat_data)
2511{
2512 struct dw_hdmi *hdmi;
2513 int ret;
2514
2515 hdmi = __dw_hdmi_probe(pdev, plat_data);
2516 if (IS_ERR(hdmi))
2517 return PTR_ERR(hdmi);
2518
2519 ret = drm_bridge_attach(encoder, &hdmi->bridge, NULL);
2520 if (ret) {
2521 dw_hdmi_remove(pdev);
2522 DRM_ERROR("Failed to initialize bridge with drm\n");
2523 return ret;
2524 }
2525
2526 return 0;
2527}
2528EXPORT_SYMBOL_GPL(dw_hdmi_bind);
2529
2530void dw_hdmi_unbind(struct device *dev)
2531{
2532 struct dw_hdmi *hdmi = dev_get_drvdata(dev);
2533
2534 __dw_hdmi_remove(hdmi);
2535}
Andy Yanb21f4b62014-12-05 14:26:31 +08002536EXPORT_SYMBOL_GPL(dw_hdmi_unbind);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002537
2538MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
Andy Yan3d1b35a2014-12-05 14:25:05 +08002539MODULE_AUTHOR("Andy Yan <andy.yan@rock-chips.com>");
2540MODULE_AUTHOR("Yakir Yang <ykk@rock-chips.com>");
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002541MODULE_AUTHOR("Vladimir Zapolskiy <vladimir_zapolskiy@mentor.com>");
Andy Yanb21f4b62014-12-05 14:26:31 +08002542MODULE_DESCRIPTION("DW HDMI transmitter driver");
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002543MODULE_LICENSE("GPL");
Andy Yanb21f4b62014-12-05 14:26:31 +08002544MODULE_ALIAS("platform:dw-hdmi");