blob: 16d5fff3e69735e4585df29368539a7fc92f2885 [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;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200176};
177
Russell Kingaeac23b2015-06-05 13:46:22 +0100178#define HDMI_IH_PHY_STAT0_RX_SENSE \
179 (HDMI_IH_PHY_STAT0_RX_SENSE0 | HDMI_IH_PHY_STAT0_RX_SENSE1 | \
180 HDMI_IH_PHY_STAT0_RX_SENSE2 | HDMI_IH_PHY_STAT0_RX_SENSE3)
181
182#define HDMI_PHY_RX_SENSE \
183 (HDMI_PHY_RX_SENSE0 | HDMI_PHY_RX_SENSE1 | \
184 HDMI_PHY_RX_SENSE2 | HDMI_PHY_RX_SENSE3)
185
Andy Yan0cd9d142014-12-05 14:28:24 +0800186static inline void hdmi_writeb(struct dw_hdmi *hdmi, u8 val, int offset)
187{
Neil Armstrong80e2f972017-03-03 19:20:06 +0200188 regmap_write(hdmi->regm, offset << hdmi->reg_shift, val);
Andy Yan0cd9d142014-12-05 14:28:24 +0800189}
190
191static inline u8 hdmi_readb(struct dw_hdmi *hdmi, int offset)
192{
Neil Armstrong80e2f972017-03-03 19:20:06 +0200193 unsigned int val = 0;
194
195 regmap_read(hdmi->regm, offset << hdmi->reg_shift, &val);
196
197 return val;
Andy Yan0cd9d142014-12-05 14:28:24 +0800198}
199
Andy Yanb21f4b62014-12-05 14:26:31 +0800200static void hdmi_modb(struct dw_hdmi *hdmi, u8 data, u8 mask, unsigned reg)
Russell King812bc612013-11-04 12:42:02 +0000201{
Neil Armstrong80e2f972017-03-03 19:20:06 +0200202 regmap_update_bits(hdmi->regm, reg << hdmi->reg_shift, mask, data);
Russell King812bc612013-11-04 12:42:02 +0000203}
204
Andy Yanb21f4b62014-12-05 14:26:31 +0800205static void hdmi_mask_writeb(struct dw_hdmi *hdmi, u8 data, unsigned int reg,
Andy Yanb5878332014-12-05 14:23:52 +0800206 u8 shift, u8 mask)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200207{
Russell King812bc612013-11-04 12:42:02 +0000208 hdmi_modb(hdmi, data << shift, mask, reg);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200209}
210
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +0300211static void dw_hdmi_i2c_init(struct dw_hdmi *hdmi)
212{
213 /* Software reset */
214 hdmi_writeb(hdmi, 0x00, HDMI_I2CM_SOFTRSTZ);
215
216 /* Set Standard Mode speed (determined to be 100KHz on iMX6) */
217 hdmi_writeb(hdmi, 0x00, HDMI_I2CM_DIV);
218
219 /* Set done, not acknowledged and arbitration interrupt polarities */
220 hdmi_writeb(hdmi, HDMI_I2CM_INT_DONE_POL, HDMI_I2CM_INT);
221 hdmi_writeb(hdmi, HDMI_I2CM_CTLINT_NAC_POL | HDMI_I2CM_CTLINT_ARB_POL,
222 HDMI_I2CM_CTLINT);
223
224 /* Clear DONE and ERROR interrupts */
225 hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,
226 HDMI_IH_I2CM_STAT0);
227
228 /* Mute DONE and ERROR interrupts */
229 hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,
230 HDMI_IH_MUTE_I2CM_STAT0);
231}
232
233static int dw_hdmi_i2c_read(struct dw_hdmi *hdmi,
234 unsigned char *buf, unsigned int length)
235{
236 struct dw_hdmi_i2c *i2c = hdmi->i2c;
237 int stat;
238
239 if (!i2c->is_regaddr) {
240 dev_dbg(hdmi->dev, "set read register address to 0\n");
241 i2c->slave_reg = 0x00;
242 i2c->is_regaddr = true;
243 }
244
245 while (length--) {
246 reinit_completion(&i2c->cmp);
247
248 hdmi_writeb(hdmi, i2c->slave_reg++, HDMI_I2CM_ADDRESS);
Nickey Yang94bb4dc2017-03-20 10:57:31 +0800249 if (i2c->is_segment)
250 hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_READ_EXT,
251 HDMI_I2CM_OPERATION);
252 else
253 hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_READ,
254 HDMI_I2CM_OPERATION);
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +0300255
256 stat = wait_for_completion_timeout(&i2c->cmp, HZ / 10);
257 if (!stat)
258 return -EAGAIN;
259
260 /* Check for error condition on the bus */
261 if (i2c->stat & HDMI_IH_I2CM_STAT0_ERROR)
262 return -EIO;
263
264 *buf++ = hdmi_readb(hdmi, HDMI_I2CM_DATAI);
265 }
Nickey Yang94bb4dc2017-03-20 10:57:31 +0800266 i2c->is_segment = false;
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +0300267
268 return 0;
269}
270
271static int dw_hdmi_i2c_write(struct dw_hdmi *hdmi,
272 unsigned char *buf, unsigned int length)
273{
274 struct dw_hdmi_i2c *i2c = hdmi->i2c;
275 int stat;
276
277 if (!i2c->is_regaddr) {
278 /* Use the first write byte as register address */
279 i2c->slave_reg = buf[0];
280 length--;
281 buf++;
282 i2c->is_regaddr = true;
283 }
284
285 while (length--) {
286 reinit_completion(&i2c->cmp);
287
288 hdmi_writeb(hdmi, *buf++, HDMI_I2CM_DATAO);
289 hdmi_writeb(hdmi, i2c->slave_reg++, HDMI_I2CM_ADDRESS);
290 hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_WRITE,
291 HDMI_I2CM_OPERATION);
292
293 stat = wait_for_completion_timeout(&i2c->cmp, HZ / 10);
294 if (!stat)
295 return -EAGAIN;
296
297 /* Check for error condition on the bus */
298 if (i2c->stat & HDMI_IH_I2CM_STAT0_ERROR)
299 return -EIO;
300 }
301
302 return 0;
303}
304
305static int dw_hdmi_i2c_xfer(struct i2c_adapter *adap,
306 struct i2c_msg *msgs, int num)
307{
308 struct dw_hdmi *hdmi = i2c_get_adapdata(adap);
309 struct dw_hdmi_i2c *i2c = hdmi->i2c;
310 u8 addr = msgs[0].addr;
311 int i, ret = 0;
312
313 dev_dbg(hdmi->dev, "xfer: num: %d, addr: %#x\n", num, addr);
314
315 for (i = 0; i < num; i++) {
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +0300316 if (msgs[i].len == 0) {
317 dev_dbg(hdmi->dev,
318 "unsupported transfer %d/%d, no data\n",
319 i + 1, num);
320 return -EOPNOTSUPP;
321 }
322 }
323
324 mutex_lock(&i2c->lock);
325
326 /* Unmute DONE and ERROR interrupts */
327 hdmi_writeb(hdmi, 0x00, HDMI_IH_MUTE_I2CM_STAT0);
328
329 /* Set slave device address taken from the first I2C message */
330 hdmi_writeb(hdmi, addr, HDMI_I2CM_SLAVE);
331
332 /* Set slave device register address on transfer */
333 i2c->is_regaddr = false;
334
Nickey Yang94bb4dc2017-03-20 10:57:31 +0800335 /* Set segment pointer for I2C extended read mode operation */
336 i2c->is_segment = false;
337
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +0300338 for (i = 0; i < num; i++) {
339 dev_dbg(hdmi->dev, "xfer: num: %d/%d, len: %d, flags: %#x\n",
340 i + 1, num, msgs[i].len, msgs[i].flags);
Nickey Yang94bb4dc2017-03-20 10:57:31 +0800341 if (msgs[i].addr == DDC_SEGMENT_ADDR && msgs[i].len == 1) {
342 i2c->is_segment = true;
343 hdmi_writeb(hdmi, DDC_SEGMENT_ADDR, HDMI_I2CM_SEGADDR);
344 hdmi_writeb(hdmi, *msgs[i].buf, HDMI_I2CM_SEGPTR);
345 } else {
346 if (msgs[i].flags & I2C_M_RD)
347 ret = dw_hdmi_i2c_read(hdmi, msgs[i].buf,
348 msgs[i].len);
349 else
350 ret = dw_hdmi_i2c_write(hdmi, msgs[i].buf,
351 msgs[i].len);
352 }
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +0300353 if (ret < 0)
354 break;
355 }
356
357 if (!ret)
358 ret = num;
359
360 /* Mute DONE and ERROR interrupts */
361 hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,
362 HDMI_IH_MUTE_I2CM_STAT0);
363
364 mutex_unlock(&i2c->lock);
365
366 return ret;
367}
368
369static u32 dw_hdmi_i2c_func(struct i2c_adapter *adapter)
370{
371 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
372}
373
374static const struct i2c_algorithm dw_hdmi_algorithm = {
375 .master_xfer = dw_hdmi_i2c_xfer,
376 .functionality = dw_hdmi_i2c_func,
377};
378
379static struct i2c_adapter *dw_hdmi_i2c_adapter(struct dw_hdmi *hdmi)
380{
381 struct i2c_adapter *adap;
382 struct dw_hdmi_i2c *i2c;
383 int ret;
384
385 i2c = devm_kzalloc(hdmi->dev, sizeof(*i2c), GFP_KERNEL);
386 if (!i2c)
387 return ERR_PTR(-ENOMEM);
388
389 mutex_init(&i2c->lock);
390 init_completion(&i2c->cmp);
391
392 adap = &i2c->adap;
393 adap->class = I2C_CLASS_DDC;
394 adap->owner = THIS_MODULE;
395 adap->dev.parent = hdmi->dev;
396 adap->algo = &dw_hdmi_algorithm;
397 strlcpy(adap->name, "DesignWare HDMI", sizeof(adap->name));
398 i2c_set_adapdata(adap, hdmi);
399
400 ret = i2c_add_adapter(adap);
401 if (ret) {
402 dev_warn(hdmi->dev, "cannot add %s I2C adapter\n", adap->name);
403 devm_kfree(hdmi->dev, i2c);
404 return ERR_PTR(ret);
405 }
406
407 hdmi->i2c = i2c;
408
409 dev_info(hdmi->dev, "registered %s I2C bus driver\n", adap->name);
410
411 return adap;
412}
413
Russell King351e1352015-01-31 14:50:23 +0000414static void hdmi_set_cts_n(struct dw_hdmi *hdmi, unsigned int cts,
415 unsigned int n)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200416{
Russell King622494a2015-02-02 10:55:38 +0000417 /* Must be set/cleared first */
418 hdmi_modb(hdmi, 0, HDMI_AUD_CTS3_CTS_MANUAL, HDMI_AUD_CTS3);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200419
420 /* nshift factor = 0 */
Russell King812bc612013-11-04 12:42:02 +0000421 hdmi_modb(hdmi, 0, HDMI_AUD_CTS3_N_SHIFT_MASK, HDMI_AUD_CTS3);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200422
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200423 hdmi_writeb(hdmi, ((cts >> 16) & HDMI_AUD_CTS3_AUDCTS19_16_MASK) |
424 HDMI_AUD_CTS3_CTS_MANUAL, HDMI_AUD_CTS3);
Russell King622494a2015-02-02 10:55:38 +0000425 hdmi_writeb(hdmi, (cts >> 8) & 0xff, HDMI_AUD_CTS2);
426 hdmi_writeb(hdmi, cts & 0xff, HDMI_AUD_CTS1);
427
428 hdmi_writeb(hdmi, (n >> 16) & 0x0f, HDMI_AUD_N3);
429 hdmi_writeb(hdmi, (n >> 8) & 0xff, HDMI_AUD_N2);
430 hdmi_writeb(hdmi, n & 0xff, HDMI_AUD_N1);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200431}
432
Russell Kingb195fbd2015-07-22 11:28:16 +0100433static unsigned int hdmi_compute_n(unsigned int freq, unsigned long pixel_clk)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200434{
435 unsigned int n = (128 * freq) / 1000;
Russell Kingd0c96d12015-07-22 10:35:41 +0100436 unsigned int mult = 1;
437
438 while (freq > 48000) {
439 mult *= 2;
440 freq /= 2;
441 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200442
443 switch (freq) {
444 case 32000:
Russell King426701d2015-07-22 10:39:27 +0100445 if (pixel_clk == 25175000)
Russell Kingb195fbd2015-07-22 11:28:16 +0100446 n = 4576;
Russell King426701d2015-07-22 10:39:27 +0100447 else if (pixel_clk == 27027000)
Russell Kingb195fbd2015-07-22 11:28:16 +0100448 n = 4096;
Russell King426701d2015-07-22 10:39:27 +0100449 else if (pixel_clk == 74176000 || pixel_clk == 148352000)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200450 n = 11648;
451 else
452 n = 4096;
Russell Kingd0c96d12015-07-22 10:35:41 +0100453 n *= mult;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200454 break;
455
456 case 44100:
Russell King426701d2015-07-22 10:39:27 +0100457 if (pixel_clk == 25175000)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200458 n = 7007;
Russell King426701d2015-07-22 10:39:27 +0100459 else if (pixel_clk == 74176000)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200460 n = 17836;
Russell King426701d2015-07-22 10:39:27 +0100461 else if (pixel_clk == 148352000)
Russell Kingb195fbd2015-07-22 11:28:16 +0100462 n = 8918;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200463 else
464 n = 6272;
Russell Kingd0c96d12015-07-22 10:35:41 +0100465 n *= mult;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200466 break;
467
468 case 48000:
Russell King426701d2015-07-22 10:39:27 +0100469 if (pixel_clk == 25175000)
Russell Kingb195fbd2015-07-22 11:28:16 +0100470 n = 6864;
Russell King426701d2015-07-22 10:39:27 +0100471 else if (pixel_clk == 27027000)
Russell Kingb195fbd2015-07-22 11:28:16 +0100472 n = 6144;
Russell King426701d2015-07-22 10:39:27 +0100473 else if (pixel_clk == 74176000)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200474 n = 11648;
Russell King426701d2015-07-22 10:39:27 +0100475 else if (pixel_clk == 148352000)
Russell Kingb195fbd2015-07-22 11:28:16 +0100476 n = 5824;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200477 else
478 n = 6144;
Russell Kingd0c96d12015-07-22 10:35:41 +0100479 n *= mult;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200480 break;
481
482 default:
483 break;
484 }
485
486 return n;
487}
488
Andy Yanb21f4b62014-12-05 14:26:31 +0800489static void hdmi_set_clk_regenerator(struct dw_hdmi *hdmi,
Russell Kingb195fbd2015-07-22 11:28:16 +0100490 unsigned long pixel_clk, unsigned int sample_rate)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200491{
Russell Kingdfbdaf52015-07-22 16:54:37 +0100492 unsigned long ftdms = pixel_clk;
Russell Kingf879b382015-03-27 12:53:29 +0000493 unsigned int n, cts;
Russell Kingdfbdaf52015-07-22 16:54:37 +0100494 u64 tmp;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200495
Russell Kingb195fbd2015-07-22 11:28:16 +0100496 n = hdmi_compute_n(sample_rate, pixel_clk);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200497
Russell Kingdfbdaf52015-07-22 16:54:37 +0100498 /*
499 * Compute the CTS value from the N value. Note that CTS and N
500 * can be up to 20 bits in total, so we need 64-bit math. Also
501 * note that our TDMS clock is not fully accurate; it is accurate
502 * to kHz. This can introduce an unnecessary remainder in the
503 * calculation below, so we don't try to warn about that.
504 */
505 tmp = (u64)ftdms * n;
506 do_div(tmp, 128 * sample_rate);
507 cts = tmp;
508
509 dev_dbg(hdmi->dev, "%s: fs=%uHz ftdms=%lu.%03luMHz N=%d cts=%d\n",
510 __func__, sample_rate, ftdms / 1000000, (ftdms / 1000) % 1000,
511 n, cts);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200512
Russell Kingb90120a2015-03-27 12:59:58 +0000513 spin_lock_irq(&hdmi->audio_lock);
514 hdmi->audio_n = n;
515 hdmi->audio_cts = cts;
516 hdmi_set_cts_n(hdmi, cts, hdmi->audio_enable ? n : 0);
517 spin_unlock_irq(&hdmi->audio_lock);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200518}
519
Andy Yanb21f4b62014-12-05 14:26:31 +0800520static void hdmi_init_clk_regenerator(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200521{
Russell King6bcf4952015-02-02 11:01:08 +0000522 mutex_lock(&hdmi->audio_mutex);
Russell Kingb195fbd2015-07-22 11:28:16 +0100523 hdmi_set_clk_regenerator(hdmi, 74250000, hdmi->sample_rate);
Russell King6bcf4952015-02-02 11:01:08 +0000524 mutex_unlock(&hdmi->audio_mutex);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200525}
526
Andy Yanb21f4b62014-12-05 14:26:31 +0800527static void hdmi_clk_regenerator_update_pixel_clock(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200528{
Russell King6bcf4952015-02-02 11:01:08 +0000529 mutex_lock(&hdmi->audio_mutex);
Russell Kingf879b382015-03-27 12:53:29 +0000530 hdmi_set_clk_regenerator(hdmi, hdmi->hdmi_data.video_mode.mpixelclock,
Russell Kingb195fbd2015-07-22 11:28:16 +0100531 hdmi->sample_rate);
Russell King6bcf4952015-02-02 11:01:08 +0000532 mutex_unlock(&hdmi->audio_mutex);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200533}
534
Russell Kingb5814ff2015-03-27 12:50:58 +0000535void dw_hdmi_set_sample_rate(struct dw_hdmi *hdmi, unsigned int rate)
536{
537 mutex_lock(&hdmi->audio_mutex);
538 hdmi->sample_rate = rate;
539 hdmi_set_clk_regenerator(hdmi, hdmi->hdmi_data.video_mode.mpixelclock,
Russell Kingb195fbd2015-07-22 11:28:16 +0100540 hdmi->sample_rate);
Russell Kingb5814ff2015-03-27 12:50:58 +0000541 mutex_unlock(&hdmi->audio_mutex);
542}
543EXPORT_SYMBOL_GPL(dw_hdmi_set_sample_rate);
544
Russell Kingb90120a2015-03-27 12:59:58 +0000545void dw_hdmi_audio_enable(struct dw_hdmi *hdmi)
546{
547 unsigned long flags;
548
549 spin_lock_irqsave(&hdmi->audio_lock, flags);
550 hdmi->audio_enable = true;
551 hdmi_set_cts_n(hdmi, hdmi->audio_cts, hdmi->audio_n);
552 spin_unlock_irqrestore(&hdmi->audio_lock, flags);
553}
554EXPORT_SYMBOL_GPL(dw_hdmi_audio_enable);
555
556void dw_hdmi_audio_disable(struct dw_hdmi *hdmi)
557{
558 unsigned long flags;
559
560 spin_lock_irqsave(&hdmi->audio_lock, flags);
561 hdmi->audio_enable = false;
562 hdmi_set_cts_n(hdmi, hdmi->audio_cts, 0);
563 spin_unlock_irqrestore(&hdmi->audio_lock, flags);
564}
565EXPORT_SYMBOL_GPL(dw_hdmi_audio_disable);
566
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200567static bool hdmi_bus_fmt_is_rgb(unsigned int bus_format)
568{
569 switch (bus_format) {
570 case MEDIA_BUS_FMT_RGB888_1X24:
571 case MEDIA_BUS_FMT_RGB101010_1X30:
572 case MEDIA_BUS_FMT_RGB121212_1X36:
573 case MEDIA_BUS_FMT_RGB161616_1X48:
574 return true;
575
576 default:
577 return false;
578 }
579}
580
581static bool hdmi_bus_fmt_is_yuv444(unsigned int bus_format)
582{
583 switch (bus_format) {
584 case MEDIA_BUS_FMT_YUV8_1X24:
585 case MEDIA_BUS_FMT_YUV10_1X30:
586 case MEDIA_BUS_FMT_YUV12_1X36:
587 case MEDIA_BUS_FMT_YUV16_1X48:
588 return true;
589
590 default:
591 return false;
592 }
593}
594
595static bool hdmi_bus_fmt_is_yuv422(unsigned int bus_format)
596{
597 switch (bus_format) {
598 case MEDIA_BUS_FMT_UYVY8_1X16:
599 case MEDIA_BUS_FMT_UYVY10_1X20:
600 case MEDIA_BUS_FMT_UYVY12_1X24:
601 return true;
602
603 default:
604 return false;
605 }
606}
607
608static bool hdmi_bus_fmt_is_yuv420(unsigned int bus_format)
609{
610 switch (bus_format) {
611 case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
612 case MEDIA_BUS_FMT_UYYVYY10_0_5X30:
613 case MEDIA_BUS_FMT_UYYVYY12_0_5X36:
614 case MEDIA_BUS_FMT_UYYVYY16_0_5X48:
615 return true;
616
617 default:
618 return false;
619 }
620}
621
622static int hdmi_bus_fmt_color_depth(unsigned int bus_format)
623{
624 switch (bus_format) {
625 case MEDIA_BUS_FMT_RGB888_1X24:
626 case MEDIA_BUS_FMT_YUV8_1X24:
627 case MEDIA_BUS_FMT_UYVY8_1X16:
628 case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
629 return 8;
630
631 case MEDIA_BUS_FMT_RGB101010_1X30:
632 case MEDIA_BUS_FMT_YUV10_1X30:
633 case MEDIA_BUS_FMT_UYVY10_1X20:
634 case MEDIA_BUS_FMT_UYYVYY10_0_5X30:
635 return 10;
636
637 case MEDIA_BUS_FMT_RGB121212_1X36:
638 case MEDIA_BUS_FMT_YUV12_1X36:
639 case MEDIA_BUS_FMT_UYVY12_1X24:
640 case MEDIA_BUS_FMT_UYYVYY12_0_5X36:
641 return 12;
642
643 case MEDIA_BUS_FMT_RGB161616_1X48:
644 case MEDIA_BUS_FMT_YUV16_1X48:
645 case MEDIA_BUS_FMT_UYYVYY16_0_5X48:
646 return 16;
647
648 default:
649 return 0;
650 }
651}
652
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200653/*
654 * this submodule is responsible for the video data synchronization.
655 * for example, for RGB 4:4:4 input, the data map is defined as
656 * pin{47~40} <==> R[7:0]
657 * pin{31~24} <==> G[7:0]
658 * pin{15~8} <==> B[7:0]
659 */
Andy Yanb21f4b62014-12-05 14:26:31 +0800660static void hdmi_video_sample(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200661{
662 int color_format = 0;
663 u8 val;
664
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200665 switch (hdmi->hdmi_data.enc_in_bus_format) {
666 case MEDIA_BUS_FMT_RGB888_1X24:
667 color_format = 0x01;
668 break;
669 case MEDIA_BUS_FMT_RGB101010_1X30:
670 color_format = 0x03;
671 break;
672 case MEDIA_BUS_FMT_RGB121212_1X36:
673 color_format = 0x05;
674 break;
675 case MEDIA_BUS_FMT_RGB161616_1X48:
676 color_format = 0x07;
677 break;
678
679 case MEDIA_BUS_FMT_YUV8_1X24:
680 case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
681 color_format = 0x09;
682 break;
683 case MEDIA_BUS_FMT_YUV10_1X30:
684 case MEDIA_BUS_FMT_UYYVYY10_0_5X30:
685 color_format = 0x0B;
686 break;
687 case MEDIA_BUS_FMT_YUV12_1X36:
688 case MEDIA_BUS_FMT_UYYVYY12_0_5X36:
689 color_format = 0x0D;
690 break;
691 case MEDIA_BUS_FMT_YUV16_1X48:
692 case MEDIA_BUS_FMT_UYYVYY16_0_5X48:
693 color_format = 0x0F;
694 break;
695
696 case MEDIA_BUS_FMT_UYVY8_1X16:
697 color_format = 0x16;
698 break;
699 case MEDIA_BUS_FMT_UYVY10_1X20:
700 color_format = 0x14;
701 break;
702 case MEDIA_BUS_FMT_UYVY12_1X24:
703 color_format = 0x12;
704 break;
705
706 default:
707 return;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200708 }
709
710 val = HDMI_TX_INVID0_INTERNAL_DE_GENERATOR_DISABLE |
711 ((color_format << HDMI_TX_INVID0_VIDEO_MAPPING_OFFSET) &
712 HDMI_TX_INVID0_VIDEO_MAPPING_MASK);
713 hdmi_writeb(hdmi, val, HDMI_TX_INVID0);
714
715 /* Enable TX stuffing: When DE is inactive, fix the output data to 0 */
716 val = HDMI_TX_INSTUFFING_BDBDATA_STUFFING_ENABLE |
717 HDMI_TX_INSTUFFING_RCRDATA_STUFFING_ENABLE |
718 HDMI_TX_INSTUFFING_GYDATA_STUFFING_ENABLE;
719 hdmi_writeb(hdmi, val, HDMI_TX_INSTUFFING);
720 hdmi_writeb(hdmi, 0x0, HDMI_TX_GYDATA0);
721 hdmi_writeb(hdmi, 0x0, HDMI_TX_GYDATA1);
722 hdmi_writeb(hdmi, 0x0, HDMI_TX_RCRDATA0);
723 hdmi_writeb(hdmi, 0x0, HDMI_TX_RCRDATA1);
724 hdmi_writeb(hdmi, 0x0, HDMI_TX_BCBDATA0);
725 hdmi_writeb(hdmi, 0x0, HDMI_TX_BCBDATA1);
726}
727
Andy Yanb21f4b62014-12-05 14:26:31 +0800728static int is_color_space_conversion(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200729{
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200730 return hdmi->hdmi_data.enc_in_bus_format != hdmi->hdmi_data.enc_out_bus_format;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200731}
732
Andy Yanb21f4b62014-12-05 14:26:31 +0800733static int is_color_space_decimation(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200734{
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200735 if (!hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format))
Fabio Estevamba92b222014-02-06 10:12:03 -0200736 return 0;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200737
738 if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_in_bus_format) ||
739 hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_in_bus_format))
Fabio Estevamba92b222014-02-06 10:12:03 -0200740 return 1;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200741
Fabio Estevamba92b222014-02-06 10:12:03 -0200742 return 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200743}
744
Andy Yanb21f4b62014-12-05 14:26:31 +0800745static int is_color_space_interpolation(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200746{
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200747 if (!hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_in_bus_format))
Fabio Estevamba92b222014-02-06 10:12:03 -0200748 return 0;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200749
750 if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format) ||
751 hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format))
Fabio Estevamba92b222014-02-06 10:12:03 -0200752 return 1;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200753
Fabio Estevamba92b222014-02-06 10:12:03 -0200754 return 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200755}
756
Andy Yanb21f4b62014-12-05 14:26:31 +0800757static void dw_hdmi_update_csc_coeffs(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200758{
759 const u16 (*csc_coeff)[3][4] = &csc_coeff_default;
Russell Kingc082f9d2013-11-04 12:10:40 +0000760 unsigned i;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200761 u32 csc_scale = 1;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200762
763 if (is_color_space_conversion(hdmi)) {
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200764 if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format)) {
765 if (hdmi->hdmi_data.enc_out_encoding ==
766 V4L2_YCBCR_ENC_601)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200767 csc_coeff = &csc_coeff_rgb_out_eitu601;
768 else
769 csc_coeff = &csc_coeff_rgb_out_eitu709;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200770 } else if (hdmi_bus_fmt_is_rgb(
771 hdmi->hdmi_data.enc_in_bus_format)) {
772 if (hdmi->hdmi_data.enc_out_encoding ==
773 V4L2_YCBCR_ENC_601)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200774 csc_coeff = &csc_coeff_rgb_in_eitu601;
775 else
776 csc_coeff = &csc_coeff_rgb_in_eitu709;
777 csc_scale = 0;
778 }
779 }
780
Russell Kingc082f9d2013-11-04 12:10:40 +0000781 /* The CSC registers are sequential, alternating MSB then LSB */
782 for (i = 0; i < ARRAY_SIZE(csc_coeff_default[0]); i++) {
783 u16 coeff_a = (*csc_coeff)[0][i];
784 u16 coeff_b = (*csc_coeff)[1][i];
785 u16 coeff_c = (*csc_coeff)[2][i];
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200786
Andy Yanb5878332014-12-05 14:23:52 +0800787 hdmi_writeb(hdmi, coeff_a & 0xff, HDMI_CSC_COEF_A1_LSB + i * 2);
Russell Kingc082f9d2013-11-04 12:10:40 +0000788 hdmi_writeb(hdmi, coeff_a >> 8, HDMI_CSC_COEF_A1_MSB + i * 2);
789 hdmi_writeb(hdmi, coeff_b & 0xff, HDMI_CSC_COEF_B1_LSB + i * 2);
790 hdmi_writeb(hdmi, coeff_b >> 8, HDMI_CSC_COEF_B1_MSB + i * 2);
Andy Yanb5878332014-12-05 14:23:52 +0800791 hdmi_writeb(hdmi, coeff_c & 0xff, HDMI_CSC_COEF_C1_LSB + i * 2);
Russell Kingc082f9d2013-11-04 12:10:40 +0000792 hdmi_writeb(hdmi, coeff_c >> 8, HDMI_CSC_COEF_C1_MSB + i * 2);
793 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200794
Russell King812bc612013-11-04 12:42:02 +0000795 hdmi_modb(hdmi, csc_scale, HDMI_CSC_SCALE_CSCSCALE_MASK,
796 HDMI_CSC_SCALE);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200797}
798
Andy Yanb21f4b62014-12-05 14:26:31 +0800799static void hdmi_video_csc(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200800{
801 int color_depth = 0;
802 int interpolation = HDMI_CSC_CFG_INTMODE_DISABLE;
803 int decimation = 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200804
805 /* YCC422 interpolation to 444 mode */
806 if (is_color_space_interpolation(hdmi))
807 interpolation = HDMI_CSC_CFG_INTMODE_CHROMA_INT_FORMULA1;
808 else if (is_color_space_decimation(hdmi))
809 decimation = HDMI_CSC_CFG_DECMODE_CHROMA_INT_FORMULA3;
810
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200811 switch (hdmi_bus_fmt_color_depth(hdmi->hdmi_data.enc_out_bus_format)) {
812 case 8:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200813 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_24BPP;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200814 break;
815 case 10:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200816 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_30BPP;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200817 break;
818 case 12:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200819 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_36BPP;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200820 break;
821 case 16:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200822 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_48BPP;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200823 break;
824
825 default:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200826 return;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200827 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200828
829 /* Configure the CSC registers */
830 hdmi_writeb(hdmi, interpolation | decimation, HDMI_CSC_CFG);
Russell King812bc612013-11-04 12:42:02 +0000831 hdmi_modb(hdmi, color_depth, HDMI_CSC_SCALE_CSC_COLORDE_PTH_MASK,
832 HDMI_CSC_SCALE);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200833
Andy Yanb21f4b62014-12-05 14:26:31 +0800834 dw_hdmi_update_csc_coeffs(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200835}
836
837/*
838 * HDMI video packetizer is used to packetize the data.
839 * for example, if input is YCC422 mode or repeater is used,
840 * data should be repacked this module can be bypassed.
841 */
Andy Yanb21f4b62014-12-05 14:26:31 +0800842static void hdmi_video_packetize(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200843{
844 unsigned int color_depth = 0;
845 unsigned int remap_size = HDMI_VP_REMAP_YCC422_16bit;
846 unsigned int output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_PP;
847 struct hdmi_data_info *hdmi_data = &hdmi->hdmi_data;
Russell Kingbebdf662013-11-04 12:55:30 +0000848 u8 val, vp_conf;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200849
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200850 if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format) ||
851 hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format)) {
852 switch (hdmi_bus_fmt_color_depth(
853 hdmi->hdmi_data.enc_out_bus_format)) {
854 case 8:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200855 color_depth = 4;
856 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200857 break;
858 case 10:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200859 color_depth = 5;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200860 break;
861 case 12:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200862 color_depth = 6;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200863 break;
864 case 16:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200865 color_depth = 7;
Neil Armstrongdef23aa2017-04-04 14:31:57 +0200866 break;
867 default:
868 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;
869 }
870 } else if (hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format)) {
871 switch (hdmi_bus_fmt_color_depth(
872 hdmi->hdmi_data.enc_out_bus_format)) {
873 case 0:
874 case 8:
875 remap_size = HDMI_VP_REMAP_YCC422_16bit;
876 break;
877 case 10:
878 remap_size = HDMI_VP_REMAP_YCC422_20bit;
879 break;
880 case 12:
881 remap_size = HDMI_VP_REMAP_YCC422_24bit;
882 break;
883
884 default:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200885 return;
Andy Yanb5878332014-12-05 14:23:52 +0800886 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200887 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_YCC422;
Andy Yanb5878332014-12-05 14:23:52 +0800888 } else {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200889 return;
Andy Yanb5878332014-12-05 14:23:52 +0800890 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200891
892 /* set the packetizer registers */
893 val = ((color_depth << HDMI_VP_PR_CD_COLOR_DEPTH_OFFSET) &
894 HDMI_VP_PR_CD_COLOR_DEPTH_MASK) |
895 ((hdmi_data->pix_repet_factor <<
896 HDMI_VP_PR_CD_DESIRED_PR_FACTOR_OFFSET) &
897 HDMI_VP_PR_CD_DESIRED_PR_FACTOR_MASK);
898 hdmi_writeb(hdmi, val, HDMI_VP_PR_CD);
899
Russell King812bc612013-11-04 12:42:02 +0000900 hdmi_modb(hdmi, HDMI_VP_STUFF_PR_STUFFING_STUFFING_MODE,
901 HDMI_VP_STUFF_PR_STUFFING_MASK, HDMI_VP_STUFF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200902
903 /* Data from pixel repeater block */
904 if (hdmi_data->pix_repet_factor > 1) {
Russell Kingbebdf662013-11-04 12:55:30 +0000905 vp_conf = HDMI_VP_CONF_PR_EN_ENABLE |
906 HDMI_VP_CONF_BYPASS_SELECT_PIX_REPEATER;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200907 } else { /* data from packetizer block */
Russell Kingbebdf662013-11-04 12:55:30 +0000908 vp_conf = HDMI_VP_CONF_PR_EN_DISABLE |
909 HDMI_VP_CONF_BYPASS_SELECT_VID_PACKETIZER;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200910 }
911
Russell Kingbebdf662013-11-04 12:55:30 +0000912 hdmi_modb(hdmi, vp_conf,
913 HDMI_VP_CONF_PR_EN_MASK |
914 HDMI_VP_CONF_BYPASS_SELECT_MASK, HDMI_VP_CONF);
915
Russell King812bc612013-11-04 12:42:02 +0000916 hdmi_modb(hdmi, 1 << HDMI_VP_STUFF_IDEFAULT_PHASE_OFFSET,
917 HDMI_VP_STUFF_IDEFAULT_PHASE_MASK, HDMI_VP_STUFF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200918
919 hdmi_writeb(hdmi, remap_size, HDMI_VP_REMAP);
920
921 if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_PP) {
Russell Kingbebdf662013-11-04 12:55:30 +0000922 vp_conf = HDMI_VP_CONF_BYPASS_EN_DISABLE |
923 HDMI_VP_CONF_PP_EN_ENABLE |
924 HDMI_VP_CONF_YCC422_EN_DISABLE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200925 } else if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_YCC422) {
Russell Kingbebdf662013-11-04 12:55:30 +0000926 vp_conf = HDMI_VP_CONF_BYPASS_EN_DISABLE |
927 HDMI_VP_CONF_PP_EN_DISABLE |
928 HDMI_VP_CONF_YCC422_EN_ENABLE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200929 } else if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS) {
Russell Kingbebdf662013-11-04 12:55:30 +0000930 vp_conf = HDMI_VP_CONF_BYPASS_EN_ENABLE |
931 HDMI_VP_CONF_PP_EN_DISABLE |
932 HDMI_VP_CONF_YCC422_EN_DISABLE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200933 } else {
934 return;
935 }
936
Russell Kingbebdf662013-11-04 12:55:30 +0000937 hdmi_modb(hdmi, vp_conf,
938 HDMI_VP_CONF_BYPASS_EN_MASK | HDMI_VP_CONF_PP_EN_ENMASK |
939 HDMI_VP_CONF_YCC422_EN_MASK, HDMI_VP_CONF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200940
Russell King812bc612013-11-04 12:42:02 +0000941 hdmi_modb(hdmi, HDMI_VP_STUFF_PP_STUFFING_STUFFING_MODE |
942 HDMI_VP_STUFF_YCC422_STUFFING_STUFFING_MODE,
943 HDMI_VP_STUFF_PP_STUFFING_MASK |
944 HDMI_VP_STUFF_YCC422_STUFFING_MASK, HDMI_VP_STUFF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200945
Russell King812bc612013-11-04 12:42:02 +0000946 hdmi_modb(hdmi, output_select, HDMI_VP_CONF_OUTPUT_SELECTOR_MASK,
947 HDMI_VP_CONF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200948}
949
Laurent Pinchartf1585f62017-03-06 01:36:15 +0200950/* -----------------------------------------------------------------------------
951 * Synopsys PHY Handling
952 */
953
Andy Yanb21f4b62014-12-05 14:26:31 +0800954static inline void hdmi_phy_test_clear(struct dw_hdmi *hdmi,
Andy Yanb5878332014-12-05 14:23:52 +0800955 unsigned char bit)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200956{
Russell King812bc612013-11-04 12:42:02 +0000957 hdmi_modb(hdmi, bit << HDMI_PHY_TST0_TSTCLR_OFFSET,
958 HDMI_PHY_TST0_TSTCLR_MASK, HDMI_PHY_TST0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200959}
960
Andy Yanb21f4b62014-12-05 14:26:31 +0800961static bool hdmi_phy_wait_i2c_done(struct dw_hdmi *hdmi, int msec)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200962{
Andy Yana4d3b8b2014-12-05 14:31:09 +0800963 u32 val;
964
965 while ((val = hdmi_readb(hdmi, HDMI_IH_I2CMPHY_STAT0) & 0x3) == 0) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200966 if (msec-- == 0)
967 return false;
Emil Renner Berthing0e6bcf32014-03-30 00:21:21 +0100968 udelay(1000);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200969 }
Andy Yana4d3b8b2014-12-05 14:31:09 +0800970 hdmi_writeb(hdmi, val, HDMI_IH_I2CMPHY_STAT0);
971
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200972 return true;
973}
974
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +0200975void dw_hdmi_phy_i2c_write(struct dw_hdmi *hdmi, unsigned short data,
976 unsigned char addr)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200977{
978 hdmi_writeb(hdmi, 0xFF, HDMI_IH_I2CMPHY_STAT0);
979 hdmi_writeb(hdmi, addr, HDMI_PHY_I2CM_ADDRESS_ADDR);
980 hdmi_writeb(hdmi, (unsigned char)(data >> 8),
Andy Yanb5878332014-12-05 14:23:52 +0800981 HDMI_PHY_I2CM_DATAO_1_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200982 hdmi_writeb(hdmi, (unsigned char)(data >> 0),
Andy Yanb5878332014-12-05 14:23:52 +0800983 HDMI_PHY_I2CM_DATAO_0_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200984 hdmi_writeb(hdmi, HDMI_PHY_I2CM_OPERATION_ADDR_WRITE,
Andy Yanb5878332014-12-05 14:23:52 +0800985 HDMI_PHY_I2CM_OPERATION_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200986 hdmi_phy_wait_i2c_done(hdmi, 1000);
987}
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +0200988EXPORT_SYMBOL_GPL(dw_hdmi_phy_i2c_write);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200989
Russell King2fada102015-07-28 12:21:34 +0100990static void dw_hdmi_phy_enable_powerdown(struct dw_hdmi *hdmi, bool enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200991{
Russell King2fada102015-07-28 12:21:34 +0100992 hdmi_mask_writeb(hdmi, !enable, HDMI_PHY_CONF0,
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200993 HDMI_PHY_CONF0_PDZ_OFFSET,
994 HDMI_PHY_CONF0_PDZ_MASK);
995}
996
Andy Yanb21f4b62014-12-05 14:26:31 +0800997static void dw_hdmi_phy_enable_tmds(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200998{
999 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
1000 HDMI_PHY_CONF0_ENTMDS_OFFSET,
1001 HDMI_PHY_CONF0_ENTMDS_MASK);
1002}
1003
Laurent Pinchartf4104e82017-01-17 10:29:02 +02001004static void dw_hdmi_phy_enable_svsret(struct dw_hdmi *hdmi, u8 enable)
Andy Yand346c142014-12-05 14:31:53 +08001005{
1006 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
Laurent Pinchartf4104e82017-01-17 10:29:02 +02001007 HDMI_PHY_CONF0_SVSRET_OFFSET,
1008 HDMI_PHY_CONF0_SVSRET_MASK);
Andy Yand346c142014-12-05 14:31:53 +08001009}
1010
Andy Yanb21f4b62014-12-05 14:26:31 +08001011static void dw_hdmi_phy_gen2_pddq(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001012{
1013 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
1014 HDMI_PHY_CONF0_GEN2_PDDQ_OFFSET,
1015 HDMI_PHY_CONF0_GEN2_PDDQ_MASK);
1016}
1017
Andy Yanb21f4b62014-12-05 14:26:31 +08001018static void dw_hdmi_phy_gen2_txpwron(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001019{
1020 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
1021 HDMI_PHY_CONF0_GEN2_TXPWRON_OFFSET,
1022 HDMI_PHY_CONF0_GEN2_TXPWRON_MASK);
1023}
1024
Andy Yanb21f4b62014-12-05 14:26:31 +08001025static void dw_hdmi_phy_sel_data_en_pol(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001026{
1027 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
1028 HDMI_PHY_CONF0_SELDATAENPOL_OFFSET,
1029 HDMI_PHY_CONF0_SELDATAENPOL_MASK);
1030}
1031
Andy Yanb21f4b62014-12-05 14:26:31 +08001032static void dw_hdmi_phy_sel_interface_control(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001033{
1034 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
1035 HDMI_PHY_CONF0_SELDIPIF_OFFSET,
1036 HDMI_PHY_CONF0_SELDIPIF_MASK);
1037}
1038
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02001039static void dw_hdmi_phy_power_off(struct dw_hdmi *hdmi)
1040{
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001041 const struct dw_hdmi_phy_data *phy = hdmi->phy.data;
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02001042 unsigned int i;
1043 u16 val;
1044
1045 if (phy->gen == 1) {
1046 dw_hdmi_phy_enable_tmds(hdmi, 0);
1047 dw_hdmi_phy_enable_powerdown(hdmi, true);
1048 return;
1049 }
1050
1051 dw_hdmi_phy_gen2_txpwron(hdmi, 0);
1052
1053 /*
1054 * Wait for TX_PHY_LOCK to be deasserted to indicate that the PHY went
1055 * to low power mode.
1056 */
1057 for (i = 0; i < 5; ++i) {
1058 val = hdmi_readb(hdmi, HDMI_PHY_STAT0);
1059 if (!(val & HDMI_PHY_TX_PHY_LOCK))
1060 break;
1061
1062 usleep_range(1000, 2000);
1063 }
1064
1065 if (val & HDMI_PHY_TX_PHY_LOCK)
1066 dev_warn(hdmi->dev, "PHY failed to power down\n");
1067 else
1068 dev_dbg(hdmi->dev, "PHY powered down in %u iterations\n", i);
1069
1070 dw_hdmi_phy_gen2_pddq(hdmi, 1);
1071}
1072
Laurent Pinchart181e0ef2017-03-06 01:35:57 +02001073static int dw_hdmi_phy_power_on(struct dw_hdmi *hdmi)
1074{
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001075 const struct dw_hdmi_phy_data *phy = hdmi->phy.data;
Laurent Pinchart181e0ef2017-03-06 01:35:57 +02001076 unsigned int i;
1077 u8 val;
1078
1079 if (phy->gen == 1) {
1080 dw_hdmi_phy_enable_powerdown(hdmi, false);
1081
1082 /* Toggle TMDS enable. */
1083 dw_hdmi_phy_enable_tmds(hdmi, 0);
1084 dw_hdmi_phy_enable_tmds(hdmi, 1);
1085 return 0;
1086 }
1087
1088 dw_hdmi_phy_gen2_txpwron(hdmi, 1);
1089 dw_hdmi_phy_gen2_pddq(hdmi, 0);
1090
1091 /* Wait for PHY PLL lock */
1092 for (i = 0; i < 5; ++i) {
1093 val = hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_TX_PHY_LOCK;
1094 if (val)
1095 break;
1096
1097 usleep_range(1000, 2000);
1098 }
1099
1100 if (!val) {
1101 dev_err(hdmi->dev, "PHY PLL failed to lock\n");
1102 return -ETIMEDOUT;
1103 }
1104
1105 dev_dbg(hdmi->dev, "PHY PLL locked %u iterations\n", i);
1106 return 0;
1107}
1108
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001109/*
1110 * PHY configuration function for the DWC HDMI 3D TX PHY. Based on the available
1111 * information the DWC MHL PHY has the same register layout and is thus also
1112 * supported by this function.
1113 */
1114static int hdmi_phy_configure_dwc_hdmi_3d_tx(struct dw_hdmi *hdmi,
1115 const struct dw_hdmi_plat_data *pdata,
1116 unsigned long mpixelclock)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001117{
Russell King39cc1532015-03-31 18:34:11 +01001118 const struct dw_hdmi_mpll_config *mpll_config = pdata->mpll_cfg;
1119 const struct dw_hdmi_curr_ctrl *curr_ctrl = pdata->cur_ctr;
1120 const struct dw_hdmi_phy_config *phy_config = pdata->phy_config;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001121
Russell King39cc1532015-03-31 18:34:11 +01001122 /* PLL/MPLL Cfg - always match on final entry */
1123 for (; mpll_config->mpixelclock != ~0UL; mpll_config++)
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001124 if (mpixelclock <= mpll_config->mpixelclock)
Russell King39cc1532015-03-31 18:34:11 +01001125 break;
1126
1127 for (; curr_ctrl->mpixelclock != ~0UL; curr_ctrl++)
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001128 if (mpixelclock <= curr_ctrl->mpixelclock)
Russell King39cc1532015-03-31 18:34:11 +01001129 break;
1130
1131 for (; phy_config->mpixelclock != ~0UL; phy_config++)
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001132 if (mpixelclock <= phy_config->mpixelclock)
Russell King39cc1532015-03-31 18:34:11 +01001133 break;
1134
1135 if (mpll_config->mpixelclock == ~0UL ||
1136 curr_ctrl->mpixelclock == ~0UL ||
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001137 phy_config->mpixelclock == ~0UL)
Russell King39cc1532015-03-31 18:34:11 +01001138 return -EINVAL;
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001139
1140 dw_hdmi_phy_i2c_write(hdmi, mpll_config->res[0].cpce,
1141 HDMI_3D_TX_PHY_CPCE_CTRL);
1142 dw_hdmi_phy_i2c_write(hdmi, mpll_config->res[0].gmp,
1143 HDMI_3D_TX_PHY_GMPCTRL);
1144 dw_hdmi_phy_i2c_write(hdmi, curr_ctrl->curr[0],
1145 HDMI_3D_TX_PHY_CURRCTRL);
1146
1147 dw_hdmi_phy_i2c_write(hdmi, 0, HDMI_3D_TX_PHY_PLLPHBYCTRL);
1148 dw_hdmi_phy_i2c_write(hdmi, HDMI_3D_TX_PHY_MSM_CTRL_CKO_SEL_FB_CLK,
1149 HDMI_3D_TX_PHY_MSM_CTRL);
1150
1151 dw_hdmi_phy_i2c_write(hdmi, phy_config->term, HDMI_3D_TX_PHY_TXTERM);
1152 dw_hdmi_phy_i2c_write(hdmi, phy_config->sym_ctr,
1153 HDMI_3D_TX_PHY_CKSYMTXCTRL);
1154 dw_hdmi_phy_i2c_write(hdmi, phy_config->vlev_ctr,
1155 HDMI_3D_TX_PHY_VLEVCTRL);
1156
1157 /* Override and disable clock termination. */
1158 dw_hdmi_phy_i2c_write(hdmi, HDMI_3D_TX_PHY_CKCALCTRL_OVERRIDE,
1159 HDMI_3D_TX_PHY_CKCALCTRL);
1160
1161 return 0;
1162}
1163
1164static int hdmi_phy_configure(struct dw_hdmi *hdmi)
1165{
1166 const struct dw_hdmi_phy_data *phy = hdmi->phy.data;
1167 const struct dw_hdmi_plat_data *pdata = hdmi->plat_data;
1168 unsigned long mpixelclock = hdmi->hdmi_data.video_mode.mpixelclock;
1169 int ret;
Russell King39cc1532015-03-31 18:34:11 +01001170
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02001171 dw_hdmi_phy_power_off(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001172
Laurent Pinchart2668db32017-01-17 10:29:09 +02001173 /* Leave low power consumption mode by asserting SVSRET. */
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001174 if (phy->has_svsret)
Laurent Pinchart2668db32017-01-17 10:29:09 +02001175 dw_hdmi_phy_enable_svsret(hdmi, 1);
1176
Laurent Pinchart54d72732017-01-17 10:29:08 +02001177 /* PHY reset. The reset signal is active high on Gen2 PHYs. */
1178 hdmi_writeb(hdmi, HDMI_MC_PHYRSTZ_PHYRSTZ, HDMI_MC_PHYRSTZ);
1179 hdmi_writeb(hdmi, 0, HDMI_MC_PHYRSTZ);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001180
1181 hdmi_writeb(hdmi, HDMI_MC_HEACPHY_RST_ASSERT, HDMI_MC_HEACPHY_RST);
1182
1183 hdmi_phy_test_clear(hdmi, 1);
1184 hdmi_writeb(hdmi, HDMI_PHY_I2CM_SLAVE_ADDR_PHY_GEN2,
Andy Yanb5878332014-12-05 14:23:52 +08001185 HDMI_PHY_I2CM_SLAVE_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001186 hdmi_phy_test_clear(hdmi, 0);
1187
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02001188 /* Write to the PHY as configured by the platform */
1189 if (pdata->configure_phy)
1190 ret = pdata->configure_phy(hdmi, pdata, mpixelclock);
1191 else
1192 ret = phy->configure(hdmi, pdata, mpixelclock);
1193 if (ret) {
1194 dev_err(hdmi->dev, "PHY configuration failed (clock %lu)\n",
1195 mpixelclock);
1196 return ret;
1197 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001198
Laurent Pinchart181e0ef2017-03-06 01:35:57 +02001199 return dw_hdmi_phy_power_on(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001200}
1201
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001202static int dw_hdmi_phy_init(struct dw_hdmi *hdmi, void *data,
1203 struct drm_display_mode *mode)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001204{
1205 int i, ret;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001206
1207 /* HDMI Phy spec says to do the phy initialization sequence twice */
1208 for (i = 0; i < 2; i++) {
Andy Yanb21f4b62014-12-05 14:26:31 +08001209 dw_hdmi_phy_sel_data_en_pol(hdmi, 1);
1210 dw_hdmi_phy_sel_interface_control(hdmi, 0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001211
Laurent Pinchart8b9e1c02017-03-03 19:19:59 +02001212 ret = hdmi_phy_configure(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001213 if (ret)
1214 return ret;
1215 }
1216
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001217 return 0;
1218}
1219
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001220static void dw_hdmi_phy_disable(struct dw_hdmi *hdmi, void *data)
1221{
1222 dw_hdmi_phy_power_off(hdmi);
1223}
1224
1225static enum drm_connector_status dw_hdmi_phy_read_hpd(struct dw_hdmi *hdmi,
1226 void *data)
1227{
1228 return hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_HPD ?
1229 connector_status_connected : connector_status_disconnected;
1230}
1231
1232static const struct dw_hdmi_phy_ops dw_hdmi_synopsys_phy_ops = {
1233 .init = dw_hdmi_phy_init,
1234 .disable = dw_hdmi_phy_disable,
1235 .read_hpd = dw_hdmi_phy_read_hpd,
1236};
1237
1238/* -----------------------------------------------------------------------------
1239 * HDMI TX Setup
1240 */
1241
Andy Yanb21f4b62014-12-05 14:26:31 +08001242static void hdmi_tx_hdcp_config(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001243{
Russell King812bc612013-11-04 12:42:02 +00001244 u8 de;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001245
1246 if (hdmi->hdmi_data.video_mode.mdataenablepolarity)
1247 de = HDMI_A_VIDPOLCFG_DATAENPOL_ACTIVE_HIGH;
1248 else
1249 de = HDMI_A_VIDPOLCFG_DATAENPOL_ACTIVE_LOW;
1250
1251 /* disable rx detect */
Russell King812bc612013-11-04 12:42:02 +00001252 hdmi_modb(hdmi, HDMI_A_HDCPCFG0_RXDETECT_DISABLE,
1253 HDMI_A_HDCPCFG0_RXDETECT_MASK, HDMI_A_HDCPCFG0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001254
Russell King812bc612013-11-04 12:42:02 +00001255 hdmi_modb(hdmi, de, HDMI_A_VIDPOLCFG_DATAENPOL_MASK, HDMI_A_VIDPOLCFG);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001256
Russell King812bc612013-11-04 12:42:02 +00001257 hdmi_modb(hdmi, HDMI_A_HDCPCFG1_ENCRYPTIONDISABLE_DISABLE,
1258 HDMI_A_HDCPCFG1_ENCRYPTIONDISABLE_MASK, HDMI_A_HDCPCFG1);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001259}
1260
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001261static void hdmi_config_AVI(struct dw_hdmi *hdmi, struct drm_display_mode *mode)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001262{
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001263 struct hdmi_avi_infoframe frame;
1264 u8 val;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001265
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001266 /* Initialise info frame from DRM mode */
1267 drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001268
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001269 if (hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format))
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001270 frame.colorspace = HDMI_COLORSPACE_YUV444;
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001271 else if (hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format))
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001272 frame.colorspace = HDMI_COLORSPACE_YUV422;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001273 else
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001274 frame.colorspace = HDMI_COLORSPACE_RGB;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001275
1276 /* Set up colorimetry */
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001277 switch (hdmi->hdmi_data.enc_out_encoding) {
1278 case V4L2_YCBCR_ENC_601:
1279 if (hdmi->hdmi_data.enc_in_encoding == V4L2_YCBCR_ENC_XV601)
1280 frame.colorimetry = HDMI_COLORIMETRY_EXTENDED;
1281 else
1282 frame.colorimetry = HDMI_COLORIMETRY_ITU_601;
1283 frame.extended_colorimetry =
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001284 HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001285 case V4L2_YCBCR_ENC_709:
1286 if (hdmi->hdmi_data.enc_in_encoding == V4L2_YCBCR_ENC_XV709)
1287 frame.colorimetry = HDMI_COLORIMETRY_EXTENDED;
1288 else
1289 frame.colorimetry = HDMI_COLORIMETRY_ITU_709;
1290 frame.extended_colorimetry =
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001291 HDMI_EXTENDED_COLORIMETRY_XV_YCC_709;
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001292 break;
1293 default: /* Carries no data */
1294 frame.colorimetry = HDMI_COLORIMETRY_ITU_601;
1295 frame.extended_colorimetry =
1296 HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
1297 break;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001298 }
1299
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001300 frame.scan_mode = HDMI_SCAN_MODE_NONE;
1301
1302 /*
1303 * The Designware IP uses a different byte format from standard
1304 * AVI info frames, though generally the bits are in the correct
1305 * bytes.
1306 */
1307
1308 /*
Jose Abreub0118e72016-08-29 10:30:51 +01001309 * AVI data byte 1 differences: Colorspace in bits 0,1 rather than 5,6,
1310 * scan info in bits 4,5 rather than 0,1 and active aspect present in
1311 * bit 6 rather than 4.
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001312 */
Jose Abreub0118e72016-08-29 10:30:51 +01001313 val = (frame.scan_mode & 3) << 4 | (frame.colorspace & 3);
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001314 if (frame.active_aspect & 15)
1315 val |= HDMI_FC_AVICONF0_ACTIVE_FMT_INFO_PRESENT;
1316 if (frame.top_bar || frame.bottom_bar)
1317 val |= HDMI_FC_AVICONF0_BAR_DATA_HORIZ_BAR;
1318 if (frame.left_bar || frame.right_bar)
1319 val |= HDMI_FC_AVICONF0_BAR_DATA_VERT_BAR;
1320 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF0);
1321
1322 /* AVI data byte 2 differences: none */
1323 val = ((frame.colorimetry & 0x3) << 6) |
1324 ((frame.picture_aspect & 0x3) << 4) |
1325 (frame.active_aspect & 0xf);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001326 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF1);
1327
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001328 /* AVI data byte 3 differences: none */
1329 val = ((frame.extended_colorimetry & 0x7) << 4) |
1330 ((frame.quantization_range & 0x3) << 2) |
1331 (frame.nups & 0x3);
1332 if (frame.itc)
1333 val |= HDMI_FC_AVICONF2_IT_CONTENT_VALID;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001334 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF2);
1335
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001336 /* AVI data byte 4 differences: none */
1337 val = frame.video_code & 0x7f;
1338 hdmi_writeb(hdmi, val, HDMI_FC_AVIVID);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001339
1340 /* AVI Data Byte 5- set up input and output pixel repetition */
1341 val = (((hdmi->hdmi_data.video_mode.mpixelrepetitioninput + 1) <<
1342 HDMI_FC_PRCONF_INCOMING_PR_FACTOR_OFFSET) &
1343 HDMI_FC_PRCONF_INCOMING_PR_FACTOR_MASK) |
1344 ((hdmi->hdmi_data.video_mode.mpixelrepetitionoutput <<
1345 HDMI_FC_PRCONF_OUTPUT_PR_FACTOR_OFFSET) &
1346 HDMI_FC_PRCONF_OUTPUT_PR_FACTOR_MASK);
1347 hdmi_writeb(hdmi, val, HDMI_FC_PRCONF);
1348
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001349 /*
1350 * AVI data byte 5 differences: content type in 0,1 rather than 4,5,
1351 * ycc range in bits 2,3 rather than 6,7
1352 */
1353 val = ((frame.ycc_quantization_range & 0x3) << 2) |
1354 (frame.content_type & 0x3);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001355 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF3);
1356
1357 /* AVI Data Bytes 6-13 */
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001358 hdmi_writeb(hdmi, frame.top_bar & 0xff, HDMI_FC_AVIETB0);
1359 hdmi_writeb(hdmi, (frame.top_bar >> 8) & 0xff, HDMI_FC_AVIETB1);
1360 hdmi_writeb(hdmi, frame.bottom_bar & 0xff, HDMI_FC_AVISBB0);
1361 hdmi_writeb(hdmi, (frame.bottom_bar >> 8) & 0xff, HDMI_FC_AVISBB1);
1362 hdmi_writeb(hdmi, frame.left_bar & 0xff, HDMI_FC_AVIELB0);
1363 hdmi_writeb(hdmi, (frame.left_bar >> 8) & 0xff, HDMI_FC_AVIELB1);
1364 hdmi_writeb(hdmi, frame.right_bar & 0xff, HDMI_FC_AVISRB0);
1365 hdmi_writeb(hdmi, (frame.right_bar >> 8) & 0xff, HDMI_FC_AVISRB1);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001366}
1367
Nickey Yang9aa1eca2017-03-21 15:36:17 +08001368static void hdmi_config_vendor_specific_infoframe(struct dw_hdmi *hdmi,
1369 struct drm_display_mode *mode)
1370{
1371 struct hdmi_vendor_infoframe frame;
1372 u8 buffer[10];
1373 ssize_t err;
1374
1375 err = drm_hdmi_vendor_infoframe_from_display_mode(&frame, mode);
1376 if (err < 0)
1377 /*
1378 * Going into that statement does not means vendor infoframe
1379 * fails. It just informed us that vendor infoframe is not
1380 * needed for the selected mode. Only 4k or stereoscopic 3D
1381 * mode requires vendor infoframe. So just simply return.
1382 */
1383 return;
1384
1385 err = hdmi_vendor_infoframe_pack(&frame, buffer, sizeof(buffer));
1386 if (err < 0) {
1387 dev_err(hdmi->dev, "Failed to pack vendor infoframe: %zd\n",
1388 err);
1389 return;
1390 }
1391 hdmi_mask_writeb(hdmi, 0, HDMI_FC_DATAUTO0, HDMI_FC_DATAUTO0_VSD_OFFSET,
1392 HDMI_FC_DATAUTO0_VSD_MASK);
1393
1394 /* Set the length of HDMI vendor specific InfoFrame payload */
1395 hdmi_writeb(hdmi, buffer[2], HDMI_FC_VSDSIZE);
1396
1397 /* Set 24bit IEEE Registration Identifier */
1398 hdmi_writeb(hdmi, buffer[4], HDMI_FC_VSDIEEEID0);
1399 hdmi_writeb(hdmi, buffer[5], HDMI_FC_VSDIEEEID1);
1400 hdmi_writeb(hdmi, buffer[6], HDMI_FC_VSDIEEEID2);
1401
1402 /* Set HDMI_Video_Format and HDMI_VIC/3D_Structure */
1403 hdmi_writeb(hdmi, buffer[7], HDMI_FC_VSDPAYLOAD0);
1404 hdmi_writeb(hdmi, buffer[8], HDMI_FC_VSDPAYLOAD1);
1405
1406 if (frame.s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF)
1407 hdmi_writeb(hdmi, buffer[9], HDMI_FC_VSDPAYLOAD2);
1408
1409 /* Packet frame interpolation */
1410 hdmi_writeb(hdmi, 1, HDMI_FC_DATAUTO1);
1411
1412 /* Auto packets per frame and line spacing */
1413 hdmi_writeb(hdmi, 0x11, HDMI_FC_DATAUTO2);
1414
1415 /* Configures the Frame Composer On RDRB mode */
1416 hdmi_mask_writeb(hdmi, 1, HDMI_FC_DATAUTO0, HDMI_FC_DATAUTO0_VSD_OFFSET,
1417 HDMI_FC_DATAUTO0_VSD_MASK);
1418}
1419
Andy Yanb21f4b62014-12-05 14:26:31 +08001420static void hdmi_av_composer(struct dw_hdmi *hdmi,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001421 const struct drm_display_mode *mode)
1422{
1423 u8 inv_val;
1424 struct hdmi_vmode *vmode = &hdmi->hdmi_data.video_mode;
1425 int hblank, vblank, h_de_hs, v_de_vs, hsync_len, vsync_len;
Russell Kinge80b9f42015-07-21 11:08:25 +01001426 unsigned int vdisplay;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001427
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001428 vmode->mpixelclock = mode->clock * 1000;
1429
1430 dev_dbg(hdmi->dev, "final pixclk = %d\n", vmode->mpixelclock);
1431
1432 /* Set up HDMI_FC_INVIDCONF */
1433 inv_val = (hdmi->hdmi_data.hdcp_enable ?
1434 HDMI_FC_INVIDCONF_HDCP_KEEPOUT_ACTIVE :
1435 HDMI_FC_INVIDCONF_HDCP_KEEPOUT_INACTIVE);
1436
Russell Kingb91eee82015-03-27 23:27:17 +00001437 inv_val |= mode->flags & DRM_MODE_FLAG_PVSYNC ?
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001438 HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_HIGH :
Russell Kingb91eee82015-03-27 23:27:17 +00001439 HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_LOW;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001440
Russell Kingb91eee82015-03-27 23:27:17 +00001441 inv_val |= mode->flags & DRM_MODE_FLAG_PHSYNC ?
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001442 HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_HIGH :
Russell Kingb91eee82015-03-27 23:27:17 +00001443 HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_LOW;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001444
1445 inv_val |= (vmode->mdataenablepolarity ?
1446 HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_HIGH :
1447 HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_LOW);
1448
1449 if (hdmi->vic == 39)
1450 inv_val |= HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_HIGH;
1451 else
Russell Kingb91eee82015-03-27 23:27:17 +00001452 inv_val |= mode->flags & DRM_MODE_FLAG_INTERLACE ?
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001453 HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_HIGH :
Russell Kingb91eee82015-03-27 23:27:17 +00001454 HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_LOW;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001455
Russell Kingb91eee82015-03-27 23:27:17 +00001456 inv_val |= mode->flags & DRM_MODE_FLAG_INTERLACE ?
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001457 HDMI_FC_INVIDCONF_IN_I_P_INTERLACED :
Russell Kingb91eee82015-03-27 23:27:17 +00001458 HDMI_FC_INVIDCONF_IN_I_P_PROGRESSIVE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001459
Russell King05b13422015-07-21 15:35:52 +01001460 inv_val |= hdmi->sink_is_hdmi ?
1461 HDMI_FC_INVIDCONF_DVI_MODEZ_HDMI_MODE :
1462 HDMI_FC_INVIDCONF_DVI_MODEZ_DVI_MODE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001463
1464 hdmi_writeb(hdmi, inv_val, HDMI_FC_INVIDCONF);
1465
Russell Kinge80b9f42015-07-21 11:08:25 +01001466 vdisplay = mode->vdisplay;
1467 vblank = mode->vtotal - mode->vdisplay;
1468 v_de_vs = mode->vsync_start - mode->vdisplay;
1469 vsync_len = mode->vsync_end - mode->vsync_start;
1470
1471 /*
1472 * When we're setting an interlaced mode, we need
1473 * to adjust the vertical timing to suit.
1474 */
1475 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
1476 vdisplay /= 2;
1477 vblank /= 2;
1478 v_de_vs /= 2;
1479 vsync_len /= 2;
1480 }
1481
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001482 /* Set up horizontal active pixel width */
1483 hdmi_writeb(hdmi, mode->hdisplay >> 8, HDMI_FC_INHACTV1);
1484 hdmi_writeb(hdmi, mode->hdisplay, HDMI_FC_INHACTV0);
1485
1486 /* Set up vertical active lines */
Russell Kinge80b9f42015-07-21 11:08:25 +01001487 hdmi_writeb(hdmi, vdisplay >> 8, HDMI_FC_INVACTV1);
1488 hdmi_writeb(hdmi, vdisplay, HDMI_FC_INVACTV0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001489
1490 /* Set up horizontal blanking pixel region width */
1491 hblank = mode->htotal - mode->hdisplay;
1492 hdmi_writeb(hdmi, hblank >> 8, HDMI_FC_INHBLANK1);
1493 hdmi_writeb(hdmi, hblank, HDMI_FC_INHBLANK0);
1494
1495 /* Set up vertical blanking pixel region width */
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001496 hdmi_writeb(hdmi, vblank, HDMI_FC_INVBLANK);
1497
1498 /* Set up HSYNC active edge delay width (in pixel clks) */
1499 h_de_hs = mode->hsync_start - mode->hdisplay;
1500 hdmi_writeb(hdmi, h_de_hs >> 8, HDMI_FC_HSYNCINDELAY1);
1501 hdmi_writeb(hdmi, h_de_hs, HDMI_FC_HSYNCINDELAY0);
1502
1503 /* Set up VSYNC active edge delay (in lines) */
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001504 hdmi_writeb(hdmi, v_de_vs, HDMI_FC_VSYNCINDELAY);
1505
1506 /* Set up HSYNC active pulse width (in pixel clks) */
1507 hsync_len = mode->hsync_end - mode->hsync_start;
1508 hdmi_writeb(hdmi, hsync_len >> 8, HDMI_FC_HSYNCINWIDTH1);
1509 hdmi_writeb(hdmi, hsync_len, HDMI_FC_HSYNCINWIDTH0);
1510
1511 /* Set up VSYNC active edge delay (in lines) */
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001512 hdmi_writeb(hdmi, vsync_len, HDMI_FC_VSYNCINWIDTH);
1513}
1514
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001515/* HDMI Initialization Step B.4 */
Andy Yanb21f4b62014-12-05 14:26:31 +08001516static void dw_hdmi_enable_video_path(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001517{
1518 u8 clkdis;
1519
1520 /* control period minimum duration */
1521 hdmi_writeb(hdmi, 12, HDMI_FC_CTRLDUR);
1522 hdmi_writeb(hdmi, 32, HDMI_FC_EXCTRLDUR);
1523 hdmi_writeb(hdmi, 1, HDMI_FC_EXCTRLSPAC);
1524
1525 /* Set to fill TMDS data channels */
1526 hdmi_writeb(hdmi, 0x0B, HDMI_FC_CH0PREAM);
1527 hdmi_writeb(hdmi, 0x16, HDMI_FC_CH1PREAM);
1528 hdmi_writeb(hdmi, 0x21, HDMI_FC_CH2PREAM);
1529
1530 /* Enable pixel clock and tmds data path */
1531 clkdis = 0x7F;
1532 clkdis &= ~HDMI_MC_CLKDIS_PIXELCLK_DISABLE;
1533 hdmi_writeb(hdmi, clkdis, HDMI_MC_CLKDIS);
1534
1535 clkdis &= ~HDMI_MC_CLKDIS_TMDSCLK_DISABLE;
1536 hdmi_writeb(hdmi, clkdis, HDMI_MC_CLKDIS);
1537
1538 /* Enable csc path */
1539 if (is_color_space_conversion(hdmi)) {
1540 clkdis &= ~HDMI_MC_CLKDIS_CSCCLK_DISABLE;
1541 hdmi_writeb(hdmi, clkdis, HDMI_MC_CLKDIS);
1542 }
Laurent Pinchart8b9e1c02017-03-03 19:19:59 +02001543
Neil Armstrong14247d72017-03-03 19:20:00 +02001544 /* Enable color space conversion if needed */
1545 if (is_color_space_conversion(hdmi))
Laurent Pinchart8b9e1c02017-03-03 19:19:59 +02001546 hdmi_writeb(hdmi, HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_IN_PATH,
1547 HDMI_MC_FLOWCTRL);
1548 else
1549 hdmi_writeb(hdmi, HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_BYPASS,
1550 HDMI_MC_FLOWCTRL);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001551}
1552
Andy Yanb21f4b62014-12-05 14:26:31 +08001553static void hdmi_enable_audio_clk(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001554{
Russell King812bc612013-11-04 12:42:02 +00001555 hdmi_modb(hdmi, 0, HDMI_MC_CLKDIS_AUDCLK_DISABLE, HDMI_MC_CLKDIS);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001556}
1557
1558/* Workaround to clear the overflow condition */
Andy Yanb21f4b62014-12-05 14:26:31 +08001559static void dw_hdmi_clear_overflow(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001560{
Laurent Pinchartbe41fc52017-01-17 10:29:05 +02001561 unsigned int count;
1562 unsigned int i;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001563 u8 val;
1564
Laurent Pinchartbe41fc52017-01-17 10:29:05 +02001565 /*
1566 * Under some circumstances the Frame Composer arithmetic unit can miss
1567 * an FC register write due to being busy processing the previous one.
1568 * The issue can be worked around by issuing a TMDS software reset and
1569 * then write one of the FC registers several times.
1570 *
1571 * The number of iterations matters and depends on the HDMI TX revision
1572 * (and possibly on the platform). So far only i.MX6Q (v1.30a) and
1573 * i.MX6DL (v1.31a) have been identified as needing the workaround, with
1574 * 4 and 1 iterations respectively.
1575 */
1576
1577 switch (hdmi->version) {
1578 case 0x130a:
1579 count = 4;
1580 break;
1581 case 0x131a:
1582 count = 1;
1583 break;
1584 default:
1585 return;
1586 }
1587
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001588 /* TMDS software reset */
1589 hdmi_writeb(hdmi, (u8)~HDMI_MC_SWRSTZ_TMDSSWRST_REQ, HDMI_MC_SWRSTZ);
1590
1591 val = hdmi_readb(hdmi, HDMI_FC_INVIDCONF);
Laurent Pinchartbe41fc52017-01-17 10:29:05 +02001592 for (i = 0; i < count; i++)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001593 hdmi_writeb(hdmi, val, HDMI_FC_INVIDCONF);
1594}
1595
Andy Yanb21f4b62014-12-05 14:26:31 +08001596static void hdmi_enable_overflow_interrupts(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001597{
1598 hdmi_writeb(hdmi, 0, HDMI_FC_MASK2);
1599 hdmi_writeb(hdmi, 0, HDMI_IH_MUTE_FC_STAT2);
1600}
1601
Andy Yanb21f4b62014-12-05 14:26:31 +08001602static void hdmi_disable_overflow_interrupts(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001603{
1604 hdmi_writeb(hdmi, HDMI_IH_MUTE_FC_STAT2_OVERFLOW_MASK,
1605 HDMI_IH_MUTE_FC_STAT2);
1606}
1607
Andy Yanb21f4b62014-12-05 14:26:31 +08001608static int dw_hdmi_setup(struct dw_hdmi *hdmi, struct drm_display_mode *mode)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001609{
1610 int ret;
1611
1612 hdmi_disable_overflow_interrupts(hdmi);
1613
1614 hdmi->vic = drm_match_cea_mode(mode);
1615
1616 if (!hdmi->vic) {
1617 dev_dbg(hdmi->dev, "Non-CEA mode used in HDMI\n");
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001618 } else {
1619 dev_dbg(hdmi->dev, "CEA mode used vic=%d\n", hdmi->vic);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001620 }
1621
1622 if ((hdmi->vic == 6) || (hdmi->vic == 7) ||
Andy Yanb5878332014-12-05 14:23:52 +08001623 (hdmi->vic == 21) || (hdmi->vic == 22) ||
1624 (hdmi->vic == 2) || (hdmi->vic == 3) ||
1625 (hdmi->vic == 17) || (hdmi->vic == 18))
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001626 hdmi->hdmi_data.enc_out_encoding = V4L2_YCBCR_ENC_601;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001627 else
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001628 hdmi->hdmi_data.enc_out_encoding = V4L2_YCBCR_ENC_709;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001629
Russell Kingd10ca822015-07-21 11:25:00 +01001630 hdmi->hdmi_data.video_mode.mpixelrepetitionoutput = 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001631 hdmi->hdmi_data.video_mode.mpixelrepetitioninput = 0;
1632
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001633 /* TOFIX: Get input format from plat data or fallback to RGB888 */
1634 if (hdmi->plat_data->input_bus_format >= 0)
1635 hdmi->hdmi_data.enc_in_bus_format =
1636 hdmi->plat_data->input_bus_format;
1637 else
1638 hdmi->hdmi_data.enc_in_bus_format = MEDIA_BUS_FMT_RGB888_1X24;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001639
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001640 /* TOFIX: Get input encoding from plat data or fallback to none */
1641 if (hdmi->plat_data->input_bus_encoding >= 0)
1642 hdmi->hdmi_data.enc_in_encoding =
1643 hdmi->plat_data->input_bus_encoding;
1644 else
1645 hdmi->hdmi_data.enc_in_encoding = V4L2_YCBCR_ENC_DEFAULT;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001646
Neil Armstrongdef23aa2017-04-04 14:31:57 +02001647 /* TOFIX: Default to RGB888 output format */
1648 hdmi->hdmi_data.enc_out_bus_format = MEDIA_BUS_FMT_RGB888_1X24;
1649
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001650 hdmi->hdmi_data.pix_repet_factor = 0;
1651 hdmi->hdmi_data.hdcp_enable = 0;
1652 hdmi->hdmi_data.video_mode.mdataenablepolarity = true;
1653
1654 /* HDMI Initialization Step B.1 */
1655 hdmi_av_composer(hdmi, mode);
1656
1657 /* HDMI Initializateion Step B.2 */
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001658 ret = hdmi->phy.ops->init(hdmi, hdmi->phy.data, &hdmi->previous_mode);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001659 if (ret)
1660 return ret;
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001661 hdmi->phy.enabled = true;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001662
1663 /* HDMI Initialization Step B.3 */
Andy Yanb21f4b62014-12-05 14:26:31 +08001664 dw_hdmi_enable_video_path(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001665
Russell Kingf709ec02015-07-21 16:09:39 +01001666 if (hdmi->sink_has_audio) {
1667 dev_dbg(hdmi->dev, "sink has audio support\n");
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001668
1669 /* HDMI Initialization Step E - Configure audio */
1670 hdmi_clk_regenerator_update_pixel_clock(hdmi);
1671 hdmi_enable_audio_clk(hdmi);
Russell Kingf709ec02015-07-21 16:09:39 +01001672 }
1673
1674 /* not for DVI mode */
1675 if (hdmi->sink_is_hdmi) {
1676 dev_dbg(hdmi->dev, "%s HDMI mode\n", __func__);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001677
1678 /* HDMI Initialization Step F - Configure AVI InfoFrame */
Russell Kingd4ac4cb2015-03-27 20:06:50 +00001679 hdmi_config_AVI(hdmi, mode);
Nickey Yang9aa1eca2017-03-21 15:36:17 +08001680 hdmi_config_vendor_specific_infoframe(hdmi, mode);
Russell King05b13422015-07-21 15:35:52 +01001681 } else {
1682 dev_dbg(hdmi->dev, "%s DVI mode\n", __func__);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001683 }
1684
1685 hdmi_video_packetize(hdmi);
1686 hdmi_video_csc(hdmi);
1687 hdmi_video_sample(hdmi);
1688 hdmi_tx_hdcp_config(hdmi);
1689
Andy Yanb21f4b62014-12-05 14:26:31 +08001690 dw_hdmi_clear_overflow(hdmi);
Russell King05b13422015-07-21 15:35:52 +01001691 if (hdmi->cable_plugin && hdmi->sink_is_hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001692 hdmi_enable_overflow_interrupts(hdmi);
1693
1694 return 0;
1695}
1696
Laurent Pincharta23d6262017-04-04 14:31:56 +02001697static void dw_hdmi_setup_i2c(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001698{
1699 hdmi_writeb(hdmi, HDMI_PHY_I2CM_INT_ADDR_DONE_POL,
1700 HDMI_PHY_I2CM_INT_ADDR);
1701
1702 hdmi_writeb(hdmi, HDMI_PHY_I2CM_CTLINT_ADDR_NAC_POL |
1703 HDMI_PHY_I2CM_CTLINT_ADDR_ARBITRATION_POL,
1704 HDMI_PHY_I2CM_CTLINT_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001705}
1706
Andy Yanb21f4b62014-12-05 14:26:31 +08001707static void initialize_hdmi_ih_mutes(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001708{
1709 u8 ih_mute;
1710
1711 /*
1712 * Boot up defaults are:
1713 * HDMI_IH_MUTE = 0x03 (disabled)
1714 * HDMI_IH_MUTE_* = 0x00 (enabled)
1715 *
1716 * Disable top level interrupt bits in HDMI block
1717 */
1718 ih_mute = hdmi_readb(hdmi, HDMI_IH_MUTE) |
1719 HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |
1720 HDMI_IH_MUTE_MUTE_ALL_INTERRUPT;
1721
1722 hdmi_writeb(hdmi, ih_mute, HDMI_IH_MUTE);
1723
1724 /* by default mask all interrupts */
1725 hdmi_writeb(hdmi, 0xff, HDMI_VP_MASK);
1726 hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK0);
1727 hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK1);
1728 hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK2);
1729 hdmi_writeb(hdmi, 0xff, HDMI_PHY_MASK0);
1730 hdmi_writeb(hdmi, 0xff, HDMI_PHY_I2CM_INT_ADDR);
1731 hdmi_writeb(hdmi, 0xff, HDMI_PHY_I2CM_CTLINT_ADDR);
1732 hdmi_writeb(hdmi, 0xff, HDMI_AUD_INT);
1733 hdmi_writeb(hdmi, 0xff, HDMI_AUD_SPDIFINT);
1734 hdmi_writeb(hdmi, 0xff, HDMI_AUD_HBR_MASK);
1735 hdmi_writeb(hdmi, 0xff, HDMI_GP_MASK);
1736 hdmi_writeb(hdmi, 0xff, HDMI_A_APIINTMSK);
1737 hdmi_writeb(hdmi, 0xff, HDMI_CEC_MASK);
1738 hdmi_writeb(hdmi, 0xff, HDMI_I2CM_INT);
1739 hdmi_writeb(hdmi, 0xff, HDMI_I2CM_CTLINT);
1740
1741 /* Disable interrupts in the IH_MUTE_* registers */
1742 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT0);
1743 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT1);
1744 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT2);
1745 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_AS_STAT0);
1746 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_PHY_STAT0);
1747 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_I2CM_STAT0);
1748 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_CEC_STAT0);
1749 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_VP_STAT0);
1750 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_I2CMPHY_STAT0);
1751 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_AHBDMAAUD_STAT0);
1752
1753 /* Enable top level interrupt bits in HDMI block */
1754 ih_mute &= ~(HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |
1755 HDMI_IH_MUTE_MUTE_ALL_INTERRUPT);
1756 hdmi_writeb(hdmi, ih_mute, HDMI_IH_MUTE);
1757}
1758
Andy Yanb21f4b62014-12-05 14:26:31 +08001759static void dw_hdmi_poweron(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001760{
Russell King381f05a2015-06-05 15:25:08 +01001761 hdmi->bridge_is_on = true;
Andy Yanb21f4b62014-12-05 14:26:31 +08001762 dw_hdmi_setup(hdmi, &hdmi->previous_mode);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001763}
1764
Andy Yanb21f4b62014-12-05 14:26:31 +08001765static void dw_hdmi_poweroff(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001766{
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001767 if (hdmi->phy.enabled) {
1768 hdmi->phy.ops->disable(hdmi, hdmi->phy.data);
1769 hdmi->phy.enabled = false;
1770 }
1771
Russell King381f05a2015-06-05 15:25:08 +01001772 hdmi->bridge_is_on = false;
1773}
1774
1775static void dw_hdmi_update_power(struct dw_hdmi *hdmi)
1776{
1777 int force = hdmi->force;
1778
1779 if (hdmi->disabled) {
1780 force = DRM_FORCE_OFF;
1781 } else if (force == DRM_FORCE_UNSPECIFIED) {
Russell Kingaeac23b2015-06-05 13:46:22 +01001782 if (hdmi->rxsense)
Russell King381f05a2015-06-05 15:25:08 +01001783 force = DRM_FORCE_ON;
1784 else
1785 force = DRM_FORCE_OFF;
1786 }
1787
1788 if (force == DRM_FORCE_OFF) {
1789 if (hdmi->bridge_is_on)
1790 dw_hdmi_poweroff(hdmi);
1791 } else {
1792 if (!hdmi->bridge_is_on)
1793 dw_hdmi_poweron(hdmi);
1794 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001795}
1796
Russell Kingaeac23b2015-06-05 13:46:22 +01001797/*
1798 * Adjust the detection of RXSENSE according to whether we have a forced
1799 * connection mode enabled, or whether we have been disabled. There is
1800 * no point processing RXSENSE interrupts if we have a forced connection
1801 * state, or DRM has us disabled.
1802 *
1803 * We also disable rxsense interrupts when we think we're disconnected
1804 * to avoid floating TDMS signals giving false rxsense interrupts.
1805 *
1806 * Note: we still need to listen for HPD interrupts even when DRM has us
1807 * disabled so that we can detect a connect event.
1808 */
1809static void dw_hdmi_update_phy_mask(struct dw_hdmi *hdmi)
1810{
1811 u8 old_mask = hdmi->phy_mask;
1812
1813 if (hdmi->force || hdmi->disabled || !hdmi->rxsense)
1814 hdmi->phy_mask |= HDMI_PHY_RX_SENSE;
1815 else
1816 hdmi->phy_mask &= ~HDMI_PHY_RX_SENSE;
1817
1818 if (old_mask != hdmi->phy_mask)
1819 hdmi_writeb(hdmi, hdmi->phy_mask, HDMI_PHY_MASK0);
1820}
1821
Laurent Pincharta23d6262017-04-04 14:31:56 +02001822static void dw_hdmi_phy_setup_hpd(struct dw_hdmi *hdmi)
1823{
1824 /*
1825 * Configure the PHY RX SENSE and HPD interrupts polarities and clear
1826 * any pending interrupt.
1827 */
1828 hdmi_writeb(hdmi, HDMI_PHY_HPD | HDMI_PHY_RX_SENSE, HDMI_PHY_POL0);
1829 hdmi_writeb(hdmi, HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE,
1830 HDMI_IH_PHY_STAT0);
1831
1832 /* Enable cable hot plug irq. */
1833 hdmi_writeb(hdmi, hdmi->phy_mask, HDMI_PHY_MASK0);
1834
1835 /* Clear and unmute interrupts. */
1836 hdmi_writeb(hdmi, HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE,
1837 HDMI_IH_PHY_STAT0);
1838 hdmi_writeb(hdmi, ~(HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE),
1839 HDMI_IH_MUTE_PHY_STAT0);
1840}
1841
Andy Yanb21f4b62014-12-05 14:26:31 +08001842static enum drm_connector_status
1843dw_hdmi_connector_detect(struct drm_connector *connector, bool force)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001844{
Andy Yanb21f4b62014-12-05 14:26:31 +08001845 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
Russell Kingd94905e2013-11-03 22:23:24 +00001846 connector);
Russell King98dbead2014-04-18 10:46:45 +01001847
Russell King381f05a2015-06-05 15:25:08 +01001848 mutex_lock(&hdmi->mutex);
1849 hdmi->force = DRM_FORCE_UNSPECIFIED;
1850 dw_hdmi_update_power(hdmi);
Russell Kingaeac23b2015-06-05 13:46:22 +01001851 dw_hdmi_update_phy_mask(hdmi);
Russell King381f05a2015-06-05 15:25:08 +01001852 mutex_unlock(&hdmi->mutex);
1853
Laurent Pinchartf1585f62017-03-06 01:36:15 +02001854 return hdmi->phy.ops->read_hpd(hdmi, hdmi->phy.data);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001855}
1856
Andy Yanb21f4b62014-12-05 14:26:31 +08001857static int dw_hdmi_connector_get_modes(struct drm_connector *connector)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001858{
Andy Yanb21f4b62014-12-05 14:26:31 +08001859 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001860 connector);
1861 struct edid *edid;
Doug Anderson6c7e66e2015-06-04 11:04:36 -07001862 int ret = 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001863
1864 if (!hdmi->ddc)
1865 return 0;
1866
1867 edid = drm_get_edid(connector, hdmi->ddc);
1868 if (edid) {
1869 dev_dbg(hdmi->dev, "got edid: width[%d] x height[%d]\n",
1870 edid->width_cm, edid->height_cm);
1871
Russell King05b13422015-07-21 15:35:52 +01001872 hdmi->sink_is_hdmi = drm_detect_hdmi_monitor(edid);
Russell Kingf709ec02015-07-21 16:09:39 +01001873 hdmi->sink_has_audio = drm_detect_monitor_audio(edid);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001874 drm_mode_connector_update_edid_property(connector, edid);
1875 ret = drm_add_edid_modes(connector, edid);
Russell Kingf5ce4052013-11-07 16:06:01 +00001876 /* Store the ELD */
1877 drm_edid_to_eld(connector, edid);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001878 kfree(edid);
1879 } else {
1880 dev_dbg(hdmi->dev, "failed to get edid\n");
1881 }
1882
Doug Anderson6c7e66e2015-06-04 11:04:36 -07001883 return ret;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001884}
1885
Andy Yan632d0352014-12-05 14:30:21 +08001886static enum drm_mode_status
1887dw_hdmi_connector_mode_valid(struct drm_connector *connector,
1888 struct drm_display_mode *mode)
1889{
1890 struct dw_hdmi *hdmi = container_of(connector,
1891 struct dw_hdmi, connector);
1892 enum drm_mode_status mode_status = MODE_OK;
1893
Russell King8add4192015-07-22 11:14:00 +01001894 /* We don't support double-clocked modes */
1895 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
1896 return MODE_BAD;
1897
Andy Yan632d0352014-12-05 14:30:21 +08001898 if (hdmi->plat_data->mode_valid)
1899 mode_status = hdmi->plat_data->mode_valid(connector, mode);
1900
1901 return mode_status;
1902}
1903
Russell King381f05a2015-06-05 15:25:08 +01001904static void dw_hdmi_connector_force(struct drm_connector *connector)
1905{
1906 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
1907 connector);
1908
1909 mutex_lock(&hdmi->mutex);
1910 hdmi->force = connector->force;
1911 dw_hdmi_update_power(hdmi);
Russell Kingaeac23b2015-06-05 13:46:22 +01001912 dw_hdmi_update_phy_mask(hdmi);
Russell King381f05a2015-06-05 15:25:08 +01001913 mutex_unlock(&hdmi->mutex);
1914}
1915
Ville Syrjälädae91e42015-12-15 12:21:02 +01001916static const struct drm_connector_funcs dw_hdmi_connector_funcs = {
Mark Yao2c5b2cc2015-11-30 18:33:40 +08001917 .dpms = drm_atomic_helper_connector_dpms,
1918 .fill_modes = drm_helper_probe_single_connector_modes,
1919 .detect = dw_hdmi_connector_detect,
Marek Vasutfdd83262016-10-05 16:31:33 +02001920 .destroy = drm_connector_cleanup,
Mark Yao2c5b2cc2015-11-30 18:33:40 +08001921 .force = dw_hdmi_connector_force,
1922 .reset = drm_atomic_helper_connector_reset,
1923 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1924 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1925};
1926
Ville Syrjälädae91e42015-12-15 12:21:02 +01001927static const struct drm_connector_helper_funcs dw_hdmi_connector_helper_funcs = {
Andy Yanb21f4b62014-12-05 14:26:31 +08001928 .get_modes = dw_hdmi_connector_get_modes,
Andy Yan632d0352014-12-05 14:30:21 +08001929 .mode_valid = dw_hdmi_connector_mode_valid,
Boris Brezillonc2a441f2016-06-07 13:48:15 +02001930 .best_encoder = drm_atomic_helper_best_encoder,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001931};
1932
Laurent Pinchartd2ae94a2017-01-17 10:28:59 +02001933static int dw_hdmi_bridge_attach(struct drm_bridge *bridge)
1934{
1935 struct dw_hdmi *hdmi = bridge->driver_private;
1936 struct drm_encoder *encoder = bridge->encoder;
1937 struct drm_connector *connector = &hdmi->connector;
1938
1939 connector->interlace_allowed = 1;
1940 connector->polled = DRM_CONNECTOR_POLL_HPD;
1941
1942 drm_connector_helper_add(connector, &dw_hdmi_connector_helper_funcs);
1943
1944 drm_connector_init(bridge->dev, connector, &dw_hdmi_connector_funcs,
1945 DRM_MODE_CONNECTOR_HDMIA);
1946
1947 drm_mode_connector_attach_encoder(connector, encoder);
1948
1949 return 0;
1950}
1951
Laurent Pinchartfd30b382017-01-17 10:28:58 +02001952static void dw_hdmi_bridge_mode_set(struct drm_bridge *bridge,
1953 struct drm_display_mode *orig_mode,
1954 struct drm_display_mode *mode)
1955{
1956 struct dw_hdmi *hdmi = bridge->driver_private;
1957
1958 mutex_lock(&hdmi->mutex);
1959
1960 /* Store the display mode for plugin/DKMS poweron events */
1961 memcpy(&hdmi->previous_mode, mode, sizeof(hdmi->previous_mode));
1962
1963 mutex_unlock(&hdmi->mutex);
1964}
1965
1966static void dw_hdmi_bridge_disable(struct drm_bridge *bridge)
1967{
1968 struct dw_hdmi *hdmi = bridge->driver_private;
1969
1970 mutex_lock(&hdmi->mutex);
1971 hdmi->disabled = true;
1972 dw_hdmi_update_power(hdmi);
1973 dw_hdmi_update_phy_mask(hdmi);
1974 mutex_unlock(&hdmi->mutex);
1975}
1976
1977static void dw_hdmi_bridge_enable(struct drm_bridge *bridge)
1978{
1979 struct dw_hdmi *hdmi = bridge->driver_private;
1980
1981 mutex_lock(&hdmi->mutex);
1982 hdmi->disabled = false;
1983 dw_hdmi_update_power(hdmi);
1984 dw_hdmi_update_phy_mask(hdmi);
1985 mutex_unlock(&hdmi->mutex);
1986}
1987
Ville Syrjälädae91e42015-12-15 12:21:02 +01001988static const struct drm_bridge_funcs dw_hdmi_bridge_funcs = {
Laurent Pinchartd2ae94a2017-01-17 10:28:59 +02001989 .attach = dw_hdmi_bridge_attach,
Andy Yanb21f4b62014-12-05 14:26:31 +08001990 .enable = dw_hdmi_bridge_enable,
1991 .disable = dw_hdmi_bridge_disable,
Andy Yanb21f4b62014-12-05 14:26:31 +08001992 .mode_set = dw_hdmi_bridge_mode_set,
Andy Yan3d1b35a2014-12-05 14:25:05 +08001993};
1994
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03001995static irqreturn_t dw_hdmi_i2c_irq(struct dw_hdmi *hdmi)
1996{
1997 struct dw_hdmi_i2c *i2c = hdmi->i2c;
1998 unsigned int stat;
1999
2000 stat = hdmi_readb(hdmi, HDMI_IH_I2CM_STAT0);
2001 if (!stat)
2002 return IRQ_NONE;
2003
2004 hdmi_writeb(hdmi, stat, HDMI_IH_I2CM_STAT0);
2005
2006 i2c->stat = stat;
2007
2008 complete(&i2c->cmp);
2009
2010 return IRQ_HANDLED;
2011}
2012
Andy Yanb21f4b62014-12-05 14:26:31 +08002013static irqreturn_t dw_hdmi_hardirq(int irq, void *dev_id)
Russell Kingd94905e2013-11-03 22:23:24 +00002014{
Andy Yanb21f4b62014-12-05 14:26:31 +08002015 struct dw_hdmi *hdmi = dev_id;
Russell Kingd94905e2013-11-03 22:23:24 +00002016 u8 intr_stat;
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002017 irqreturn_t ret = IRQ_NONE;
2018
2019 if (hdmi->i2c)
2020 ret = dw_hdmi_i2c_irq(hdmi);
Russell Kingd94905e2013-11-03 22:23:24 +00002021
2022 intr_stat = hdmi_readb(hdmi, HDMI_IH_PHY_STAT0);
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002023 if (intr_stat) {
Russell Kingd94905e2013-11-03 22:23:24 +00002024 hdmi_writeb(hdmi, ~0, HDMI_IH_MUTE_PHY_STAT0);
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002025 return IRQ_WAKE_THREAD;
2026 }
Russell Kingd94905e2013-11-03 22:23:24 +00002027
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002028 return ret;
Russell Kingd94905e2013-11-03 22:23:24 +00002029}
2030
Andy Yanb21f4b62014-12-05 14:26:31 +08002031static irqreturn_t dw_hdmi_irq(int irq, void *dev_id)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002032{
Andy Yanb21f4b62014-12-05 14:26:31 +08002033 struct dw_hdmi *hdmi = dev_id;
Russell Kingaeac23b2015-06-05 13:46:22 +01002034 u8 intr_stat, phy_int_pol, phy_pol_mask, phy_stat;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002035
2036 intr_stat = hdmi_readb(hdmi, HDMI_IH_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002037 phy_int_pol = hdmi_readb(hdmi, HDMI_PHY_POL0);
Russell Kingaeac23b2015-06-05 13:46:22 +01002038 phy_stat = hdmi_readb(hdmi, HDMI_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002039
Russell Kingaeac23b2015-06-05 13:46:22 +01002040 phy_pol_mask = 0;
2041 if (intr_stat & HDMI_IH_PHY_STAT0_HPD)
2042 phy_pol_mask |= HDMI_PHY_HPD;
2043 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE0)
2044 phy_pol_mask |= HDMI_PHY_RX_SENSE0;
2045 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE1)
2046 phy_pol_mask |= HDMI_PHY_RX_SENSE1;
2047 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE2)
2048 phy_pol_mask |= HDMI_PHY_RX_SENSE2;
2049 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE3)
2050 phy_pol_mask |= HDMI_PHY_RX_SENSE3;
2051
2052 if (phy_pol_mask)
2053 hdmi_modb(hdmi, ~phy_int_pol, phy_pol_mask, HDMI_PHY_POL0);
2054
2055 /*
2056 * RX sense tells us whether the TDMS transmitters are detecting
2057 * load - in other words, there's something listening on the
2058 * other end of the link. Use this to decide whether we should
2059 * power on the phy as HPD may be toggled by the sink to merely
2060 * ask the source to re-read the EDID.
2061 */
2062 if (intr_stat &
2063 (HDMI_IH_PHY_STAT0_RX_SENSE | HDMI_IH_PHY_STAT0_HPD)) {
Russell Kingb872a8e2015-06-05 12:22:46 +01002064 mutex_lock(&hdmi->mutex);
Romain Perier187697a2017-03-27 11:45:07 +05302065 if (!hdmi->force) {
Russell Kingaeac23b2015-06-05 13:46:22 +01002066 /*
2067 * If the RX sense status indicates we're disconnected,
2068 * clear the software rxsense status.
2069 */
2070 if (!(phy_stat & HDMI_PHY_RX_SENSE))
2071 hdmi->rxsense = false;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002072
Russell Kingaeac23b2015-06-05 13:46:22 +01002073 /*
2074 * Only set the software rxsense status when both
2075 * rxsense and hpd indicates we're connected.
2076 * This avoids what seems to be bad behaviour in
2077 * at least iMX6S versions of the phy.
2078 */
2079 if (phy_stat & HDMI_PHY_HPD)
2080 hdmi->rxsense = true;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002081
Russell Kingaeac23b2015-06-05 13:46:22 +01002082 dw_hdmi_update_power(hdmi);
2083 dw_hdmi_update_phy_mask(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002084 }
Russell Kingb872a8e2015-06-05 12:22:46 +01002085 mutex_unlock(&hdmi->mutex);
Russell Kingaeac23b2015-06-05 13:46:22 +01002086 }
2087
2088 if (intr_stat & HDMI_IH_PHY_STAT0_HPD) {
2089 dev_dbg(hdmi->dev, "EVENT=%s\n",
2090 phy_int_pol & HDMI_PHY_HPD ? "plugin" : "plugout");
Laurent Pinchartba5d7e62017-01-17 10:28:56 +02002091 if (hdmi->bridge.dev)
2092 drm_helper_hpd_irq_event(hdmi->bridge.dev);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002093 }
2094
2095 hdmi_writeb(hdmi, intr_stat, HDMI_IH_PHY_STAT0);
Russell Kingaeac23b2015-06-05 13:46:22 +01002096 hdmi_writeb(hdmi, ~(HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE),
2097 HDMI_IH_MUTE_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002098
2099 return IRQ_HANDLED;
2100}
2101
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002102static const struct dw_hdmi_phy_data dw_hdmi_phys[] = {
2103 {
2104 .type = DW_HDMI_PHY_DWC_HDMI_TX_PHY,
2105 .name = "DWC HDMI TX PHY",
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02002106 .gen = 1,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002107 }, {
2108 .type = DW_HDMI_PHY_DWC_MHL_PHY_HEAC,
2109 .name = "DWC MHL PHY + HEAC PHY",
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02002110 .gen = 2,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002111 .has_svsret = true,
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02002112 .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002113 }, {
2114 .type = DW_HDMI_PHY_DWC_MHL_PHY,
2115 .name = "DWC MHL PHY",
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02002116 .gen = 2,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002117 .has_svsret = true,
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02002118 .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002119 }, {
2120 .type = DW_HDMI_PHY_DWC_HDMI_3D_TX_PHY_HEAC,
2121 .name = "DWC HDMI 3D TX PHY + HEAC PHY",
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02002122 .gen = 2,
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02002123 .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002124 }, {
2125 .type = DW_HDMI_PHY_DWC_HDMI_3D_TX_PHY,
2126 .name = "DWC HDMI 3D TX PHY",
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02002127 .gen = 2,
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02002128 .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002129 }, {
2130 .type = DW_HDMI_PHY_DWC_HDMI20_TX_PHY,
2131 .name = "DWC HDMI 2.0 TX PHY",
Laurent Pinchartb0e583e2017-03-06 01:35:39 +02002132 .gen = 2,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002133 .has_svsret = true,
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02002134 }, {
2135 .type = DW_HDMI_PHY_VENDOR_PHY,
2136 .name = "Vendor PHY",
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002137 }
2138};
2139
2140static int dw_hdmi_detect_phy(struct dw_hdmi *hdmi)
2141{
2142 unsigned int i;
2143 u8 phy_type;
2144
2145 phy_type = hdmi_readb(hdmi, HDMI_CONFIG2_ID);
2146
Laurent Pinchartf1585f62017-03-06 01:36:15 +02002147 if (phy_type == DW_HDMI_PHY_VENDOR_PHY) {
2148 /* Vendor PHYs require support from the glue layer. */
2149 if (!hdmi->plat_data->phy_ops || !hdmi->plat_data->phy_name) {
2150 dev_err(hdmi->dev,
2151 "Vendor HDMI PHY not supported by glue layer\n");
2152 return -ENODEV;
2153 }
2154
2155 hdmi->phy.ops = hdmi->plat_data->phy_ops;
2156 hdmi->phy.data = hdmi->plat_data->phy_data;
2157 hdmi->phy.name = hdmi->plat_data->phy_name;
2158 return 0;
2159 }
2160
2161 /* Synopsys PHYs are handled internally. */
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002162 for (i = 0; i < ARRAY_SIZE(dw_hdmi_phys); ++i) {
2163 if (dw_hdmi_phys[i].type == phy_type) {
Laurent Pinchartf1585f62017-03-06 01:36:15 +02002164 hdmi->phy.ops = &dw_hdmi_synopsys_phy_ops;
2165 hdmi->phy.name = dw_hdmi_phys[i].name;
2166 hdmi->phy.data = (void *)&dw_hdmi_phys[i];
Kieran Bingham2ef9dfe2017-03-03 19:20:04 +02002167
2168 if (!dw_hdmi_phys[i].configure &&
2169 !hdmi->plat_data->configure_phy) {
2170 dev_err(hdmi->dev, "%s requires platform support\n",
2171 hdmi->phy.name);
2172 return -ENODEV;
2173 }
2174
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002175 return 0;
2176 }
2177 }
2178
Laurent Pinchartf1585f62017-03-06 01:36:15 +02002179 dev_err(hdmi->dev, "Unsupported HDMI PHY type (%02x)\n", phy_type);
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002180 return -ENODEV;
2181}
2182
Neil Armstrong80e2f972017-03-03 19:20:06 +02002183static const struct regmap_config hdmi_regmap_8bit_config = {
2184 .reg_bits = 32,
2185 .val_bits = 8,
2186 .reg_stride = 1,
2187 .max_register = HDMI_I2CM_FS_SCL_LCNT_0_ADDR,
2188};
2189
2190static const struct regmap_config hdmi_regmap_32bit_config = {
2191 .reg_bits = 32,
2192 .val_bits = 32,
2193 .reg_stride = 4,
2194 .max_register = HDMI_I2CM_FS_SCL_LCNT_0_ADDR << 2,
2195};
2196
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002197static struct dw_hdmi *
2198__dw_hdmi_probe(struct platform_device *pdev,
2199 const struct dw_hdmi_plat_data *plat_data)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002200{
Laurent Pinchartc6081192017-01-17 10:28:57 +02002201 struct device *dev = &pdev->dev;
Russell King17b50012013-11-03 11:23:34 +00002202 struct device_node *np = dev->of_node;
Russell King7ed6c662013-11-07 16:01:45 +00002203 struct platform_device_info pdevinfo;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002204 struct device_node *ddc_node;
Andy Yanb21f4b62014-12-05 14:26:31 +08002205 struct dw_hdmi *hdmi;
Neil Armstrong80e2f972017-03-03 19:20:06 +02002206 struct resource *iores = NULL;
Laurent Pinchartc6081192017-01-17 10:28:57 +02002207 int irq;
Andy Yan3d1b35a2014-12-05 14:25:05 +08002208 int ret;
Andy Yan0cd9d142014-12-05 14:28:24 +08002209 u32 val = 1;
Laurent Pinchart0527e122017-01-17 10:29:03 +02002210 u8 prod_id0;
2211 u8 prod_id1;
Kuninori Morimoto2761ba62016-11-08 01:00:57 +00002212 u8 config0;
Laurent Pinchart0c674942017-01-17 10:29:04 +02002213 u8 config3;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002214
Russell King17b50012013-11-03 11:23:34 +00002215 hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002216 if (!hdmi)
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002217 return ERR_PTR(-ENOMEM);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002218
Andy Yan3d1b35a2014-12-05 14:25:05 +08002219 hdmi->plat_data = plat_data;
Russell King17b50012013-11-03 11:23:34 +00002220 hdmi->dev = dev;
Russell King40678382013-11-07 15:35:06 +00002221 hdmi->sample_rate = 48000;
Russell Kingb872a8e2015-06-05 12:22:46 +01002222 hdmi->disabled = true;
Russell Kingaeac23b2015-06-05 13:46:22 +01002223 hdmi->rxsense = true;
2224 hdmi->phy_mask = (u8)~(HDMI_PHY_HPD | HDMI_PHY_RX_SENSE);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002225
Russell Kingb872a8e2015-06-05 12:22:46 +01002226 mutex_init(&hdmi->mutex);
Russell King6bcf4952015-02-02 11:01:08 +00002227 mutex_init(&hdmi->audio_mutex);
Russell Kingb90120a2015-03-27 12:59:58 +00002228 spin_lock_init(&hdmi->audio_lock);
Russell King6bcf4952015-02-02 11:01:08 +00002229
Philipp Zabelb5d45902014-03-05 10:20:56 +01002230 ddc_node = of_parse_phandle(np, "ddc-i2c-bus", 0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002231 if (ddc_node) {
Vladimir Zapolskiy9f04a1f2016-08-16 23:26:43 +03002232 hdmi->ddc = of_get_i2c_adapter_by_node(ddc_node);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002233 of_node_put(ddc_node);
Andy Yanc2c38482014-12-05 14:24:28 +08002234 if (!hdmi->ddc) {
2235 dev_dbg(hdmi->dev, "failed to read ddc node\n");
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002236 return ERR_PTR(-EPROBE_DEFER);
Andy Yanc2c38482014-12-05 14:24:28 +08002237 }
2238
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002239 } else {
2240 dev_dbg(hdmi->dev, "no ddc property found\n");
2241 }
2242
Neil Armstrong80e2f972017-03-03 19:20:06 +02002243 if (!plat_data->regm) {
2244 const struct regmap_config *reg_config;
2245
2246 of_property_read_u32(np, "reg-io-width", &val);
2247 switch (val) {
2248 case 4:
2249 reg_config = &hdmi_regmap_32bit_config;
2250 hdmi->reg_shift = 2;
2251 break;
2252 case 1:
2253 reg_config = &hdmi_regmap_8bit_config;
2254 break;
2255 default:
2256 dev_err(dev, "reg-io-width must be 1 or 4\n");
2257 return ERR_PTR(-EINVAL);
2258 }
2259
2260 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2261 hdmi->regs = devm_ioremap_resource(dev, iores);
2262 if (IS_ERR(hdmi->regs)) {
2263 ret = PTR_ERR(hdmi->regs);
2264 goto err_res;
2265 }
2266
2267 hdmi->regm = devm_regmap_init_mmio(dev, hdmi->regs, reg_config);
2268 if (IS_ERR(hdmi->regm)) {
2269 dev_err(dev, "Failed to configure regmap\n");
2270 ret = PTR_ERR(hdmi->regm);
2271 goto err_res;
2272 }
2273 } else {
2274 hdmi->regm = plat_data->regm;
Vladimir Zapolskiy9f04a1f2016-08-16 23:26:43 +03002275 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002276
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002277 hdmi->isfr_clk = devm_clk_get(hdmi->dev, "isfr");
2278 if (IS_ERR(hdmi->isfr_clk)) {
2279 ret = PTR_ERR(hdmi->isfr_clk);
Andy Yanb5878332014-12-05 14:23:52 +08002280 dev_err(hdmi->dev, "Unable to get HDMI isfr clk: %d\n", ret);
Vladimir Zapolskiy9f04a1f2016-08-16 23:26:43 +03002281 goto err_res;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002282 }
2283
2284 ret = clk_prepare_enable(hdmi->isfr_clk);
2285 if (ret) {
Andy Yanb5878332014-12-05 14:23:52 +08002286 dev_err(hdmi->dev, "Cannot enable HDMI isfr clock: %d\n", ret);
Vladimir Zapolskiy9f04a1f2016-08-16 23:26:43 +03002287 goto err_res;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002288 }
2289
2290 hdmi->iahb_clk = devm_clk_get(hdmi->dev, "iahb");
2291 if (IS_ERR(hdmi->iahb_clk)) {
2292 ret = PTR_ERR(hdmi->iahb_clk);
Andy Yanb5878332014-12-05 14:23:52 +08002293 dev_err(hdmi->dev, "Unable to get HDMI iahb clk: %d\n", ret);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002294 goto err_isfr;
2295 }
2296
2297 ret = clk_prepare_enable(hdmi->iahb_clk);
2298 if (ret) {
Andy Yanb5878332014-12-05 14:23:52 +08002299 dev_err(hdmi->dev, "Cannot enable HDMI iahb clock: %d\n", ret);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002300 goto err_isfr;
2301 }
2302
2303 /* Product and revision IDs */
Laurent Pinchartbe41fc52017-01-17 10:29:05 +02002304 hdmi->version = (hdmi_readb(hdmi, HDMI_DESIGN_ID) << 8)
2305 | (hdmi_readb(hdmi, HDMI_REVISION_ID) << 0);
Laurent Pinchart0527e122017-01-17 10:29:03 +02002306 prod_id0 = hdmi_readb(hdmi, HDMI_PRODUCT_ID0);
2307 prod_id1 = hdmi_readb(hdmi, HDMI_PRODUCT_ID1);
2308
2309 if (prod_id0 != HDMI_PRODUCT_ID0_HDMI_TX ||
2310 (prod_id1 & ~HDMI_PRODUCT_ID1_HDCP) != HDMI_PRODUCT_ID1_HDMI_TX) {
2311 dev_err(dev, "Unsupported HDMI controller (%04x:%02x:%02x)\n",
Laurent Pinchartbe41fc52017-01-17 10:29:05 +02002312 hdmi->version, prod_id0, prod_id1);
Laurent Pinchart0527e122017-01-17 10:29:03 +02002313 ret = -ENODEV;
2314 goto err_iahb;
2315 }
2316
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002317 ret = dw_hdmi_detect_phy(hdmi);
2318 if (ret < 0)
2319 goto err_iahb;
2320
2321 dev_info(dev, "Detected HDMI TX controller v%x.%03x %s HDCP (%s)\n",
Laurent Pinchartbe41fc52017-01-17 10:29:05 +02002322 hdmi->version >> 12, hdmi->version & 0xfff,
Laurent Pinchartfaba6c32017-01-17 10:29:06 +02002323 prod_id1 & HDMI_PRODUCT_ID1_HDCP ? "with" : "without",
Laurent Pinchartf1585f62017-03-06 01:36:15 +02002324 hdmi->phy.name);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002325
2326 initialize_hdmi_ih_mutes(hdmi);
2327
Laurent Pinchartc6081192017-01-17 10:28:57 +02002328 irq = platform_get_irq(pdev, 0);
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002329 if (irq < 0) {
2330 ret = irq;
Laurent Pinchartc6081192017-01-17 10:28:57 +02002331 goto err_iahb;
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002332 }
Laurent Pinchartc6081192017-01-17 10:28:57 +02002333
Philipp Zabel639a2022015-01-07 13:43:50 +01002334 ret = devm_request_threaded_irq(dev, irq, dw_hdmi_hardirq,
2335 dw_hdmi_irq, IRQF_SHARED,
2336 dev_name(dev), hdmi);
2337 if (ret)
Fabio Estevamb33ef612015-01-27 10:54:12 -02002338 goto err_iahb;
Philipp Zabel639a2022015-01-07 13:43:50 +01002339
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002340 /*
2341 * To prevent overflows in HDMI_IH_FC_STAT2, set the clk regenerator
2342 * N and cts values before enabling phy
2343 */
2344 hdmi_init_clk_regenerator(hdmi);
2345
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002346 /* If DDC bus is not specified, try to register HDMI I2C bus */
2347 if (!hdmi->ddc) {
2348 hdmi->ddc = dw_hdmi_i2c_adapter(hdmi);
2349 if (IS_ERR(hdmi->ddc))
2350 hdmi->ddc = NULL;
2351 }
2352
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002353 hdmi->bridge.driver_private = hdmi;
2354 hdmi->bridge.funcs = &dw_hdmi_bridge_funcs;
Arnd Bergmannd5ad7842017-01-23 13:20:38 +01002355#ifdef CONFIG_OF
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002356 hdmi->bridge.of_node = pdev->dev.of_node;
Arnd Bergmannd5ad7842017-01-23 13:20:38 +01002357#endif
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002358
Laurent Pincharta23d6262017-04-04 14:31:56 +02002359 dw_hdmi_setup_i2c(hdmi);
2360 dw_hdmi_phy_setup_hpd(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002361
Russell King7ed6c662013-11-07 16:01:45 +00002362 memset(&pdevinfo, 0, sizeof(pdevinfo));
2363 pdevinfo.parent = dev;
2364 pdevinfo.id = PLATFORM_DEVID_AUTO;
2365
Kuninori Morimoto2761ba62016-11-08 01:00:57 +00002366 config0 = hdmi_readb(hdmi, HDMI_CONFIG0_ID);
Laurent Pinchart0c674942017-01-17 10:29:04 +02002367 config3 = hdmi_readb(hdmi, HDMI_CONFIG3_ID);
Kuninori Morimoto2761ba62016-11-08 01:00:57 +00002368
Neil Armstrong80e2f972017-03-03 19:20:06 +02002369 if (iores && config3 & HDMI_CONFIG3_AHBAUDDMA) {
Kuninori Morimoto2761ba62016-11-08 01:00:57 +00002370 struct dw_hdmi_audio_data audio;
2371
Russell King7ed6c662013-11-07 16:01:45 +00002372 audio.phys = iores->start;
2373 audio.base = hdmi->regs;
2374 audio.irq = irq;
2375 audio.hdmi = hdmi;
Russell Kingf5ce4052013-11-07 16:06:01 +00002376 audio.eld = hdmi->connector.eld;
Russell King7ed6c662013-11-07 16:01:45 +00002377
2378 pdevinfo.name = "dw-hdmi-ahb-audio";
2379 pdevinfo.data = &audio;
2380 pdevinfo.size_data = sizeof(audio);
2381 pdevinfo.dma_mask = DMA_BIT_MASK(32);
2382 hdmi->audio = platform_device_register_full(&pdevinfo);
Kuninori Morimoto2761ba62016-11-08 01:00:57 +00002383 } else if (config0 & HDMI_CONFIG0_I2S) {
2384 struct dw_hdmi_i2s_audio_data audio;
2385
2386 audio.hdmi = hdmi;
2387 audio.write = hdmi_writeb;
2388 audio.read = hdmi_readb;
2389
2390 pdevinfo.name = "dw-hdmi-i2s-audio";
2391 pdevinfo.data = &audio;
2392 pdevinfo.size_data = sizeof(audio);
2393 pdevinfo.dma_mask = DMA_BIT_MASK(32);
2394 hdmi->audio = platform_device_register_full(&pdevinfo);
Russell King7ed6c662013-11-07 16:01:45 +00002395 }
2396
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002397 /* Reset HDMI DDC I2C master controller and mute I2CM interrupts */
2398 if (hdmi->i2c)
2399 dw_hdmi_i2c_init(hdmi);
2400
Laurent Pinchartc6081192017-01-17 10:28:57 +02002401 platform_set_drvdata(pdev, hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002402
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002403 return hdmi;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002404
2405err_iahb:
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002406 if (hdmi->i2c) {
2407 i2c_del_adapter(&hdmi->i2c->adap);
2408 hdmi->ddc = NULL;
2409 }
2410
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002411 clk_disable_unprepare(hdmi->iahb_clk);
2412err_isfr:
2413 clk_disable_unprepare(hdmi->isfr_clk);
Vladimir Zapolskiy9f04a1f2016-08-16 23:26:43 +03002414err_res:
2415 i2c_put_adapter(hdmi->ddc);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002416
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002417 return ERR_PTR(ret);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002418}
2419
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002420static void __dw_hdmi_remove(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002421{
Russell King7ed6c662013-11-07 16:01:45 +00002422 if (hdmi->audio && !IS_ERR(hdmi->audio))
2423 platform_device_unregister(hdmi->audio);
2424
Russell Kingd94905e2013-11-03 22:23:24 +00002425 /* Disable all interrupts */
2426 hdmi_writeb(hdmi, ~0, HDMI_IH_MUTE_PHY_STAT0);
2427
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002428 clk_disable_unprepare(hdmi->iahb_clk);
2429 clk_disable_unprepare(hdmi->isfr_clk);
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002430
2431 if (hdmi->i2c)
2432 i2c_del_adapter(&hdmi->i2c->adap);
2433 else
2434 i2c_put_adapter(hdmi->ddc);
Russell King17b50012013-11-03 11:23:34 +00002435}
Laurent Pinchart69497eb2017-01-17 10:29:00 +02002436
2437/* -----------------------------------------------------------------------------
2438 * Probe/remove API, used from platforms based on the DRM bridge API.
2439 */
2440int dw_hdmi_probe(struct platform_device *pdev,
2441 const struct dw_hdmi_plat_data *plat_data)
2442{
2443 struct dw_hdmi *hdmi;
2444 int ret;
2445
2446 hdmi = __dw_hdmi_probe(pdev, plat_data);
2447 if (IS_ERR(hdmi))
2448 return PTR_ERR(hdmi);
2449
2450 ret = drm_bridge_add(&hdmi->bridge);
2451 if (ret < 0) {
2452 __dw_hdmi_remove(hdmi);
2453 return ret;
2454 }
2455
2456 return 0;
2457}
2458EXPORT_SYMBOL_GPL(dw_hdmi_probe);
2459
2460void dw_hdmi_remove(struct platform_device *pdev)
2461{
2462 struct dw_hdmi *hdmi = platform_get_drvdata(pdev);
2463
2464 drm_bridge_remove(&hdmi->bridge);
2465
2466 __dw_hdmi_remove(hdmi);
2467}
2468EXPORT_SYMBOL_GPL(dw_hdmi_remove);
2469
2470/* -----------------------------------------------------------------------------
2471 * Bind/unbind API, used from platforms based on the component framework.
2472 */
2473int dw_hdmi_bind(struct platform_device *pdev, struct drm_encoder *encoder,
2474 const struct dw_hdmi_plat_data *plat_data)
2475{
2476 struct dw_hdmi *hdmi;
2477 int ret;
2478
2479 hdmi = __dw_hdmi_probe(pdev, plat_data);
2480 if (IS_ERR(hdmi))
2481 return PTR_ERR(hdmi);
2482
2483 ret = drm_bridge_attach(encoder, &hdmi->bridge, NULL);
2484 if (ret) {
2485 dw_hdmi_remove(pdev);
2486 DRM_ERROR("Failed to initialize bridge with drm\n");
2487 return ret;
2488 }
2489
2490 return 0;
2491}
2492EXPORT_SYMBOL_GPL(dw_hdmi_bind);
2493
2494void dw_hdmi_unbind(struct device *dev)
2495{
2496 struct dw_hdmi *hdmi = dev_get_drvdata(dev);
2497
2498 __dw_hdmi_remove(hdmi);
2499}
Andy Yanb21f4b62014-12-05 14:26:31 +08002500EXPORT_SYMBOL_GPL(dw_hdmi_unbind);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002501
2502MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
Andy Yan3d1b35a2014-12-05 14:25:05 +08002503MODULE_AUTHOR("Andy Yan <andy.yan@rock-chips.com>");
2504MODULE_AUTHOR("Yakir Yang <ykk@rock-chips.com>");
Vladimir Zapolskiy3efc2fa2016-08-24 08:46:37 +03002505MODULE_AUTHOR("Vladimir Zapolskiy <vladimir_zapolskiy@mentor.com>");
Andy Yanb21f4b62014-12-05 14:26:31 +08002506MODULE_DESCRIPTION("DW HDMI transmitter driver");
Fabio Estevam9aaf8802013-11-29 08:46:32 -02002507MODULE_LICENSE("GPL");
Andy Yanb21f4b62014-12-05 14:26:31 +08002508MODULE_ALIAS("platform:dw-hdmi");